Example usage for org.springframework.jdbc.core BatchPreparedStatementSetter BatchPreparedStatementSetter

List of usage examples for org.springframework.jdbc.core BatchPreparedStatementSetter BatchPreparedStatementSetter

Introduction

In this page you can find the example usage for org.springframework.jdbc.core BatchPreparedStatementSetter BatchPreparedStatementSetter.

Prototype

BatchPreparedStatementSetter

Source Link

Usage

From source file:org.hyperic.hq.measurement.server.session.MeasurementTemplateDAO.java

public void createTemplates(final String pluginName,
        final Map<MonitorableType, List<MonitorableMeasurementInfo>> toAdd) {
    final IdentifierGenerator tmplIdGenerator = ((SessionFactoryImpl) sessionFactory)
            .getEntityPersister(MeasurementTemplate.class.getName()).getIdentifierGenerator();

    final String templatesql = "INSERT INTO EAM_MEASUREMENT_TEMPL "
            + "(id, name, alias, units, collection_type, default_on, "
            + "default_interval, designate, monitorable_type_id, "
            + "category_id, template, plugin, ctime, mtime) "
            + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    final List<MonitorableMeasurementInfo> combinedInfos = new ArrayList<MonitorableMeasurementInfo>();
    for (List<MonitorableMeasurementInfo> info : toAdd.values()) {
        combinedInfos.addAll(info);//from   w ww. ja va  2s.  c  o  m
    }
    final long current = System.currentTimeMillis();
    //We need JdbcTemplate to throw runtime Exception to roll back tx if batch update fails, else we'll get partial write
    jdbcTemplate.batchUpdate(templatesql, new BatchPreparedStatementSetter() {
        HashMap<String, Category> cats = new HashMap<String, Category>();

        public void setValues(PreparedStatement stmt, int i) throws SQLException {

            MeasurementInfo info = combinedInfos.get(i).getMeasurementInfo();
            Category cat = (Category) cats.get(info.getCategory());
            if (cat == null) {
                cat = catDAO.findByName(info.getCategory());
                if (cat == null) {
                    cat = catDAO.create(info.getCategory());
                }
                cats.put(info.getCategory(), cat);
            }
            Integer rawid = (Integer) tmplIdGenerator.generate((SessionImpl) getSession(),
                    new MeasurementTemplate());
            stmt.setInt(1, rawid.intValue());
            stmt.setString(2, info.getName());
            String alias = info.getAlias();
            if (alias.length() > ALIAS_LIMIT) {
                alias = alias.substring(0, ALIAS_LIMIT);
                log.warn("ALIAS field of EAM_MEASUREMENT_TEMPLATE truncated: original value was "
                        + info.getAlias() + ", truncated value is " + alias);
            }
            stmt.setString(3, alias);
            stmt.setString(4, info.getUnits());
            stmt.setInt(5, info.getCollectionType());
            stmt.setBoolean(6, info.isDefaultOn());
            stmt.setLong(7, info.getInterval());
            stmt.setBoolean(8, info.isIndicator());
            stmt.setInt(9, combinedInfos.get(i).getMonitorableType().getId().intValue());
            stmt.setInt(10, cat.getId().intValue());
            stmt.setString(11, info.getTemplate());
            stmt.setString(12, pluginName);
            stmt.setLong(13, current);
            stmt.setLong(14, current);
        }

        public int getBatchSize() {
            return combinedInfos.size();
        }
    });
}

From source file:org.ojbc.adapters.analyticaldatastore.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void savePretrialServiceNeedAssociations(final List<Integer> assessedNeedIds,
        final int pretrialServiceParticipationId) {
    jdbcTemplate.batchUpdate(PRETRIAL_SERVICE_NEED_ASSOCIATION_INSERT, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setInt(1, assessedNeedIds.get(i));
            ps.setLong(2, pretrialServiceParticipationId);
        }//from   w w w.  jav a2  s  . c o m

        public int getBatchSize() {
            return assessedNeedIds.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticaldatastore.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void savePretrialServiceAssociations(final List<Integer> pretrialServiceIds,
        final int pretrialServiceParticipationPkId) {

    jdbcTemplate.batchUpdate(PRETRIAL_SERVICE_ASSOCIATION_INSERT, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setInt(1, pretrialServiceIds.get(i));
            ps.setLong(2, pretrialServiceParticipationPkId);
        }/*from   www  .j  a v  a 2 s.co  m*/

        public int getBatchSize() {
            return pretrialServiceIds.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void saveBehavioralHealthAssessments(List<BehavioralHealthAssessment> behavioralHealthAssessments) {
    log.debug("Inserting row into BehavioralHealthAssessment table: " + behavioralHealthAssessments);
    final String sqlString = "INSERT INTO BehavioralHealthAssessment (PersonID, seriousMentalIllnessIndicator,"
            + "CareEpisodeStartDate, CareEpisodeEndDate, "
            + "MedicaidStatusTypeId, EnrolledProviderName ) values (?,?,?,?,?,?,?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            BehavioralHealthAssessment behavioralHealthAssessment = behavioralHealthAssessments.get(i);
            ps.setInt(1, behavioralHealthAssessment.getPersonId());
            setPreparedStatementVariable(behavioralHealthAssessment.getSeriousMentalIllness(), ps, 2);
            setPreparedStatementVariable(behavioralHealthAssessment.getCareEpisodeStartDate(), ps, 3);
            setPreparedStatementVariable(behavioralHealthAssessment.getCareEpisodeEndDate(), ps, 4);
            setPreparedStatementVariable(behavioralHealthAssessment.getMedicaidStatusTypeId(), ps, 5);
            setPreparedStatementVariable(behavioralHealthAssessment.getEnrolledProviderName(), ps, 6);
        }//  ww w .  j a v  a 2  s. c  om

        public int getBatchSize() {
            return behavioralHealthAssessments.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void saveBookingCharges(List<BookingCharge> bookingCharges) {
    log.info("Inserting row into BookingCharge table: " + bookingCharges);
    final String sqlString = "INSERT INTO BookingCharge (BookingArrestID, ChargeCode, AgencyID, "
            + "BondAmount, BondTypeID, ChargeClassTypeID, BondStatusTypeId, ChargeJurisdictionTypeId, ChargeDisposition) "
            + "values (?,?,?,?,?,?,?,?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            BookingCharge bookingCharge = bookingCharges.get(i);
            ps.setInt(1, bookingCharge.getBookingArrestId());
            setPreparedStatementVariable(bookingCharge.getChargeCode(), ps, 2);
            setPreparedStatementVariable(bookingCharge.getAgencyId(), ps, 3);
            setPreparedStatementVariable(bookingCharge.getBondAmount(), ps, 4);

            if (bookingCharge.getBondType() != null) {
                setPreparedStatementVariable(bookingCharge.getBondType().getKey(), ps, 5);
            } else {
                ps.setNull(5, java.sql.Types.NULL);
            }//  w  ww.j  ava  2s  .  co  m

            setPreparedStatementVariable(bookingCharge.getChargeClassTypeId(), ps, 6);
            setPreparedStatementVariable(bookingCharge.getBondStatusTypeId(), ps, 7);
            setPreparedStatementVariable(bookingCharge.getChargeJurisdictionTypeId(), ps, 8);
            setPreparedStatementVariable(bookingCharge.getChargeDisposition(), ps, 9);
        }

        public int getBatchSize() {
            return bookingCharges.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void saveCustodyStatusChangeCharges(List<CustodyStatusChangeCharge> custodyStatusChangeCharges) {
    log.info("Inserting row into CustodyStatusChangeCharge table: " + custodyStatusChangeCharges);
    final String sqlString = "INSERT INTO CustodyStatusChangeCharge (CustodyStatusChangeArrestID, ChargeCode, AgencyID, "
            + "BondAmount, BondTypeID, ChargeClassTypeId, BondStatusTypeId, ChargeJurisdictionTypeId, "
            + "ChargeDisposition ) " + "values (?,?,?,?,?,?,?,?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            CustodyStatusChangeCharge custodyStatusChangeCharge = custodyStatusChangeCharges.get(i);
            ps.setInt(1, custodyStatusChangeCharge.getCustodyStatusChangeArrestId());
            setPreparedStatementVariable(custodyStatusChangeCharge.getChargeCode(), ps, 2);
            setPreparedStatementVariable(custodyStatusChangeCharge.getAgencyId(), ps, 3);
            setPreparedStatementVariable(custodyStatusChangeCharge.getBondAmount(), ps, 4);

            if (custodyStatusChangeCharge.getBondType() != null) {
                setPreparedStatementVariable(custodyStatusChangeCharge.getBondType().getKey(), ps, 5);
            } else {
                ps.setNull(5, java.sql.Types.NULL);
            }//from   w ww. ja  va  2s.c  o m
            setPreparedStatementVariable(custodyStatusChangeCharge.getChargeClassTypeId(), ps, 6);
            setPreparedStatementVariable(custodyStatusChangeCharge.getBondStatusTypeId(), ps, 7);
            setPreparedStatementVariable(custodyStatusChangeCharge.getChargeJurisdictionTypeId(), ps, 8);
            setPreparedStatementVariable(custodyStatusChangeCharge.getChargeDisposition(), ps, 9);
        }

        public int getBatchSize() {
            return custodyStatusChangeCharges.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void saveTreatments(final List<Treatment> treatments) {
    log.info("Inserting row into Treatment table: " + treatments);
    final String sqlString = "INSERT INTO Treatment (BehavioralHealthAssessmentID, TreatmentStartDate, "
            + "TreatmentAdmissionReasonTypeID, TreatmentProviderName, TreatmentStatusTypeID) "
            + "values (?,?,?,?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Treatment treatment = treatments.get(i);
            ps.setInt(1, treatment.getBehavioralHealthAssessmentID());
            setPreparedStatementVariable(treatment.getTreatmentStartDate(), ps, 2);
            setPreparedStatementVariable(treatment.getTreatmentAdmissionReasonTypeId(), ps, 3);
            setPreparedStatementVariable(treatment.getTreatmentProviderName(), ps, 4);
            setPreparedStatementVariable(treatment.getTreatmentStatusTypeId(), ps, 5);
        }/*from  www .j a va  2  s .  c  om*/

        public int getBatchSize() {
            return treatments.size();
        }
    });
}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void saveBehavioralHealthEvaluations(Integer behavioralHealthAssessmentId,
        List<String> behavioralHealthDiagnoses) {
    log.info("Inserting row into BehavioralHealthEvaluation table: " + behavioralHealthDiagnoses
            + " to BehavioralHealthAssessment " + behavioralHealthAssessmentId);
    final String sqlString = "INSERT INTO BehavioralHealthEvaluation (BehavioralHealthAssessmentID, BehavioralHealthDiagnosisDescription) "
            + "values (?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            String behavioralHealthDiagnosis = behavioralHealthDiagnoses.get(i);
            ps.setInt(1, behavioralHealthAssessmentId);
            ps.setString(2, behavioralHealthDiagnosis);
        }//ww w  .j a  v  a2 s .c  om

        public int getBatchSize() {
            return behavioralHealthDiagnoses.size();
        }
    });

}

From source file:org.ojbc.adapters.analyticsstaging.custody.dao.AnalyticalDatastoreDAOImpl.java

@Override
public void savePrescribedMedications(List<PrescribedMedication> prescribedMedications) {
    log.info("Inserting row into PrescribedMedication table: " + prescribedMedications);
    final String sqlString = "INSERT INTO PrescribedMedication (BehavioralHealthAssessmentID, MedicationDescription, MedicationDispensingDate, MedicationDoseMeasure) "
            + "values (?,?,?,?)";

    jdbcTemplate.batchUpdate(sqlString, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            PrescribedMedication prescribedMedication = prescribedMedications.get(i);
            ps.setInt(1, prescribedMedication.getBehavioralHealthAssessmentID());
            setPreparedStatementVariable(prescribedMedication.getMedicationDescription(), ps, 2);
            setPreparedStatementVariable(prescribedMedication.getMedicationDispensingDate(), ps, 3);
            setPreparedStatementVariable(prescribedMedication.getMedicationDoseMeasure(), ps, 4);
        }/*from  w  ww .j a v a 2  s  .c  o m*/

        public int getBatchSize() {
            return prescribedMedications.size();
        }
    });
}

From source file:org.ojbc.policyacknowledgement.dao.PolicyDAOImpl.java

@Transactional
public void insertUserPolicyAcknowledgement(final List<Policy> policies, final Long userId) {

    jdbcTemplate.batchUpdate(/*from  w  w w  .  j av  a2  s .  co  m*/
            "INSERT INTO user_policy_acknowledgement "
                    + "(user_id, policy_id, acknowledge_date) VALUES (?, ?, CURRENT_TIMESTAMP())",
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    Policy policy = policies.get(i);
                    ps.setLong(1, userId);
                    ps.setLong(2, policy.getId());
                }

                public int getBatchSize() {
                    return policies.size();
                }
            });

}