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:nl.ordina.bag.etl.dao.AbstractBAGDAO.java

protected void insertGerelateerdePanden(final Verblijfsobject verblijfsobject) {
    jdbcTemplate.batchUpdate(/* w  w  w.j a  v  a  2 s .  c  om*/
            "insert into bag_gerelateerd_pand (" + "bag_verblijfsobject_id," + "aanduiding_record_correctie,"
                    + "begindatum_tijdvak_geldigheid," + "bag_pand_id" + ") values (?,?,?,?)",
            new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setLong(1, verblijfsobject.getIdentificatie());
                    ps.setLong(2, verblijfsobject.getAanduidingRecordCorrectie());
                    ps.setTimestamp(3,
                            new Timestamp(verblijfsobject.getBegindatumTijdvakGeldigheid().getTime()));
                    ps.setLong(4, verblijfsobject.getGerelateerdPanden().get(i));
                }

                @Override
                public int getBatchSize() {
                    return verblijfsobject.getGerelateerdPanden().size();
                }
            });
}

From source file:nl.ordina.bag.etl.dao.AbstractBAGDAO.java

protected void insertGerelateerdePanden(final List<Verblijfsobject> verblijfsobjecten) {
    final List<GerelateerdPand> batch = new ArrayList<GerelateerdPand>();
    for (Verblijfsobject verblijfsobject : verblijfsobjecten)
        for (Long gerelateerdPand : verblijfsobject.getGerelateerdPanden())
            batch.add(new GerelateerdPand(verblijfsobject, gerelateerdPand));
    jdbcTemplate.batchUpdate(/*from   w w  w  . jav a  2s. co m*/
            "insert into bag_gerelateerd_pand (" + "bag_verblijfsobject_id," + "aanduiding_record_correctie,"
                    + "begindatum_tijdvak_geldigheid," + "bag_pand_id" + ") values (?,?,?,?)",
            new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setLong(1, batch.get(i).getIdentificatie());
                    ps.setLong(2, batch.get(i).getAanduidingRecordCorrectie());
                    ps.setTimestamp(3, new Timestamp(batch.get(i).getBegindatumTijdvakGeldigheid().getTime()));
                    ps.setLong(4, batch.get(i).getGerelateerdPand());
                }

                @Override
                public int getBatchSize() {
                    return batch.size();
                }
            });
}

From source file:org.agnitas.dao.impl.ImportRecipientsDaoImpl.java

@Override
public void createRecipients(final Map<ProfileRecipientFields, ValidatorResults> recipientBeansMap,
        final Integer adminID, final ImportProfile profile, final Integer type, int datasource_id,
        CSVColumnState[] columns) {/*from w ww.  j a  v a 2  s.co  m*/
    if (recipientBeansMap.isEmpty()) {
        return;
    }
    final JdbcTemplate template = getJdbcTemplateForTemporaryTable();
    final String prefix = "cust_" + adminID + "_tmp_";
    final String tableName = prefix + datasource_id + "_tbl";
    final ProfileRecipientFields[] recipients = recipientBeansMap.keySet()
            .toArray(new ProfileRecipientFields[recipientBeansMap.keySet().size()]);
    String keyColumn = profile.getKeyColumn();
    List<String> keyColumns = profile.getKeyColumns();
    String duplicateSql = "";
    String duplicateSqlParams = "";
    if (keyColumns.isEmpty()) {
        duplicateSql += " column_duplicate_check_0 ";
        duplicateSqlParams = "?";
    } else {
        for (int i = 0; i < keyColumns.size(); i++) {
            duplicateSql += "column_duplicate_check_" + i;
            duplicateSqlParams += "?";
            if (i != keyColumns.size() - 1) {
                duplicateSql += ",";
                duplicateSqlParams += ",";
            }
        }
    }
    final List<CSVColumnState> temporaryKeyColumns = new ArrayList<CSVColumnState>();
    for (CSVColumnState column : columns) {
        if (keyColumns.isEmpty()) {
            if (column.getColName().equals(keyColumn) && column.getImportedColumn()) {
                temporaryKeyColumns.add(column);
            }
        } else {
            for (String columnName : keyColumns) {
                if (column.getColName().equals(columnName) && column.getImportedColumn()) {
                    temporaryKeyColumns.add(column);
                    break;
                }
            }
        }
    }
    final String query = "INSERT INTO " + tableName
            + " (recipient, validator_result, temporary_id, status_type, " + duplicateSql + ") VALUES (?,?,?,?,"
            + duplicateSqlParams + ")";
    final BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {

        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setBytes(1, ImportUtils.getObjectAsBytes(recipients[i]));
            ps.setBytes(2, ImportUtils.getObjectAsBytes(recipientBeansMap.get(recipients[i])));
            ps.setString(3, recipients[i].getTemporaryId());
            ps.setInt(4, type);
            for (int j = 0; j < temporaryKeyColumns.size(); j++) {
                setPreparedStatmentForCurrentColumn(ps, 5 + j, temporaryKeyColumns.get(j), recipients[i],
                        profile, recipientBeansMap.get(recipients[i]));
            }

            if (logger.isInfoEnabled()) {
                logger.info("Import ID: " + profile.getImportId() + " Adding recipient to temp-table: "
                        + Toolkit.getValueFromBean(recipients[i], profile.getKeyColumn()));
            }
        }

        public int getBatchSize() {
            return recipientBeansMap.size();
        }
    };
    template.batchUpdate(query, setter);
}

From source file:org.agnitas.dao.impl.ImportRecipientsDaoImpl.java

@Override
public void updateRecipients(final Map<ProfileRecipientFields, ValidatorResults> recipientBeans,
        Integer adminID, final int type, final ImportProfile profile, int datasource_id,
        CSVColumnState[] columns) {// ww w  .j  a  v  a 2 s. c  o m
    if (recipientBeans.isEmpty()) {
        return;
    }
    final JdbcTemplate template = getJdbcTemplateForTemporaryTable();
    final String prefix = "cust_" + adminID + "_tmp_";
    final String tableName = prefix + datasource_id + "_tbl";
    final ProfileRecipientFields[] recipients = recipientBeans.keySet()
            .toArray(new ProfileRecipientFields[recipientBeans.keySet().size()]);
    String keyColumn = profile.getKeyColumn();
    List<String> keyColumns = profile.getKeyColumns();
    String duplicateSql = "";
    if (keyColumns.isEmpty()) {
        duplicateSql += " column_duplicate_check_0=? ";
    } else {
        for (int i = 0; i < keyColumns.size(); i++) {
            duplicateSql += " column_duplicate_check_" + i + "=? ";
            if (i != keyColumns.size() - 1) {
                duplicateSql += ", ";
            }
        }
    }
    final String query = "UPDATE  " + tableName + " SET recipient=?, validator_result=?, status_type=?, "
            + duplicateSql + " WHERE temporary_id=?";
    final List<CSVColumnState> temporaryKeyColumns = new ArrayList<CSVColumnState>();
    for (CSVColumnState column : columns) {
        if (keyColumns.isEmpty()) {
            if (column.getColName().equals(keyColumn) && column.getImportedColumn()) {
                temporaryKeyColumns.add(column);
            }
        } else {
            for (String columnName : keyColumns) {
                if (column.getColName().equals(columnName) && column.getImportedColumn()) {
                    temporaryKeyColumns.add(column);
                    break;
                }
            }
        }
    }
    final BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {

        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setBytes(1, ImportUtils.getObjectAsBytes(recipients[i]));
            ps.setBytes(2, ImportUtils.getObjectAsBytes(recipientBeans.get(recipients[i])));
            ps.setInt(3, type);
            for (int j = 0; j < temporaryKeyColumns.size(); j++) {
                setPreparedStatmentForCurrentColumn(ps, 4 + j, temporaryKeyColumns.get(j), recipients[i],
                        profile, recipientBeans.get(recipients[i]));
            }
            ps.setString(4 + temporaryKeyColumns.size(), recipients[i].getTemporaryId());

            if (logger.isInfoEnabled()) {
                logger.info("Import ID: " + profile.getImportId() + " Updating recipient in temp-table: "
                        + Toolkit.getValueFromBean(recipients[i], profile.getKeyColumn()));
            }
        }

        public int getBatchSize() {
            return recipientBeans.size();
        }
    };
    template.batchUpdate(query, setter);
}

From source file:org.agnitas.dao.impl.ImportRecipientsDaoImpl.java

@Override
public void addNewRecipients(final Map<ProfileRecipientFields, ValidatorResults> validRecipients,
        Integer adminId, final ImportProfile importProfile, final CSVColumnState[] columns,
        final int datasourceID) throws Exception {
    if (validRecipients.isEmpty()) {
        return;/*from   www.ja v  a 2 s .com*/
    }

    String currentTimestamp = AgnUtils.getHibernateDialect().getCurrentTimestampSQLFunctionName();

    final JdbcTemplate template = createJdbcTemplate();
    final ProfileRecipientFields[] recipientsBean = validRecipients.keySet()
            .toArray(new ProfileRecipientFields[validRecipients.size()]);

    final int[] newcustomerIDs = getNextCustomerSequences(importProfile.getCompanyId(), recipientsBean.length);

    final String tableName = "customer_" + importProfile.getCompanyId() + "_tbl";

    String query = "INSERT INTO " + tableName + " (";

    if (AgnUtils.isOracleDB()) {
        query = query + "customer_id,";
    }
    boolean isGenderMapped = false;
    query = query + "mailtype, datasource_id, ";
    for (CSVColumnState column : columns) {
        if (column.getColName().equals("creation_date")) {
            throw new Exception(" creation_date column is not allowed to be imported");
        }
        if (column.getImportedColumn() && !column.getColName().equals("mailtype")) {
            query = query + column.getColName() + ", ";
        }
        if (column.getImportedColumn() && column.getColName().equals("gender")) {
            isGenderMapped = true;
        }
    }
    if (!isGenderMapped) {
        if (AgnUtils.isOracleDB()) {
            query = query + "gender, ";
        }
    }
    query = query.substring(0, query.length() - 2);
    query = query + ", creation_date) VALUES (";

    if (AgnUtils.isOracleDB()) {
        query = query + "?, ";
    }

    for (CSVColumnState column : columns) {
        if (column.getImportedColumn() && !column.getColName().equals("mailtype")) {
            query = query + "?, ";
        }
    }
    if (!isGenderMapped) {
        if (AgnUtils.isOracleDB()) {
            query = query + "?, ";
        }
    }
    query = query + "?, ?, ";
    query = query.substring(0, query.length() - 2);
    query = query + ", " + currentTimestamp + ")";
    final Boolean finalIsGenderMapped = isGenderMapped;
    final BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ProfileRecipientFields profileRecipientFields = recipientsBean[i];
            Integer mailtype = Integer.valueOf(profileRecipientFields.getMailtype());

            int index = 0;
            if (AgnUtils.isOracleDB()) {
                ps.setInt(1, newcustomerIDs[i]);
                ps.setInt(2, mailtype);
                ps.setInt(3, datasourceID);
                index = 4;
            }
            if (AgnUtils.isMySQLDB()) {
                ps.setInt(1, mailtype);
                ps.setInt(2, datasourceID);
                index = 3;
            }
            for (CSVColumnState column : columns) {
                if (column.getImportedColumn() && !column.getColName().equals("mailtype")) {
                    setPreparedStatmentForCurrentColumn(ps, index, column, profileRecipientFields,
                            importProfile, null);
                    index++;
                }
            }
            if (!finalIsGenderMapped) {
                if (AgnUtils.isOracleDB()) {
                    ps.setInt(index, 2);
                }
            }

            if (logger.isInfoEnabled()) {
                logger.info("Import ID: " + importProfile.getImportId()
                        + " Adding recipient to recipient-table: "
                        + Toolkit.getValueFromBean(profileRecipientFields, importProfile.getKeyColumn()));
            }
        }

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

    template.batchUpdate(query, setter);
}

From source file:org.agnitas.dao.impl.ImportRecipientsDaoImpl.java

@Override
public void importInToBlackList(final Collection<ProfileRecipientFields> recipients, final int companyId) {
    if (recipients.isEmpty()) {
        return;/*ww w.  ja  v  a  2 s .  co  m*/
    }
    final JdbcTemplate template = createJdbcTemplate();
    final ProfileRecipientFields[] recipientsArray = recipients
            .toArray(new ProfileRecipientFields[recipients.size()]);
    String query;
    if (AgnUtils.isOracleDB()) {
        query = "INSERT INTO cust" + companyId + "_ban_tbl (email) VALUES (?)";
    } else {
        query = "INSERT INTO cust_ban_tbl (company_id, email) VALUES (?,?)";
    }
    final BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            if (AgnUtils.isOracleDB()) {
                ps.setString(1, recipientsArray[i].getEmail().toLowerCase());
            } else {
                ps.setInt(1, companyId);
                ps.setString(2, recipientsArray[i].getEmail().toLowerCase());
            }
        }

        public int getBatchSize() {
            return recipients.size();
        }
    };
    template.batchUpdate(query, setter);
}

From source file:org.agnitas.dao.impl.ImportRecipientsDaoImpl.java

private void createRecipientBindTemporaryTable(int companyID, int datasourceId,
        final List<Integer> updatedRecipients, JdbcTemplate jdbc) {
    String sql = removeBindTemporaryTable(companyID, datasourceId, jdbc);
    if (AgnUtils.isMySQLDB()) {
        sql = "CREATE TEMPORARY TABLE cust_" + companyID + "_exist1_tmp" + datasourceId
                + "_tbl (`customer_id` int(10) unsigned NOT NULL)";
    } else if (AgnUtils.isOracleDB()) {
        sql = "CREATE TABLE cust_" + companyID + "_exist1_tmp" + datasourceId
                + "_tbl (customer_id NUMBER(10) NOT NULL)";
    }//from  www  .j a  v a 2 s  . com
    jdbc.execute(sql);
    if (updatedRecipients.isEmpty()) {
        return;
    }
    sql = "INSERT INTO cust_" + companyID + "_exist1_tmp" + datasourceId + "_tbl (customer_id) VALUES (?)";

    final BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setInt(1, updatedRecipients.get(i));
        }

        public int getBatchSize() {
            return updatedRecipients.size();
        }
    };
    jdbc.batchUpdate(sql, setter);
}

From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java

/**
 * for the list l, perform l.size()/batchSize batch updates. Avoid mysql
 * packet too large exceptions with large batch updates. Call spring
 * jdbcTemplate.batchUpdate internally with sublists of l with size
 * batchSize.//from w w  w.  j  a va  2s  . co  m
 * 
 * @param sql
 * @param l
 * @param cpss
 */
private <T> void chunkedBatchUpdate(String sql, List<T> l, final ChunkPreparedStatementSetter<T> cpss) {
    int chunks = (int) Math.ceil((double) l.size() / (double) this.batchSize);
    for (int i = 0; i < chunks; i++) {
        int start = i * this.batchSize;
        int end = (i + 1) * this.batchSize;
        if (end > l.size())
            end = l.size();
        final List<T> chunkList = l.subList(start, end);
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public int getBatchSize() {
                return chunkList.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int idx) throws SQLException {
                T record = chunkList.get(idx);
                cpss.setValues(ps, idx, record);
            }
        });
    }
}

From source file:org.apromore.dao.jpa.FragmentDistanceRepositoryCustomImpl.java

/**
 * Save a distance calculation into the DB.
 * @param distances the array of details for the distance inserts.
 *//*ww w  . j a  v a2  s.  com*/
@Override
public void persistDistance(final List<DistanceDO> distances) {
    String sql = "INSERT INTO fragment_distance (fragmentVersionId1, fragmentVersionId2, ged) VALUES (?,?,?)";

    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            DistanceDO distanceDO = distances.get(i);
            ps.setInt(1, distanceDO.getFragmentId1());
            ps.setInt(2, distanceDO.getFragmentId2());
            ps.setDouble(3, distanceDO.getGed());
        }

        @Override
        public int getBatchSize() {
            return distances.size();
        }
    });
}

From source file:org.fireflow.engine.persistence.springjdbc.PersistenceServiceSpringJdbcImpl.java

public void deleteTokensForNodes(final String processInstanceId, final List<String> nodeIdsList) {

    super.getJdbcTemplate().batchUpdate("delete from t_ff_rt_token where processinstance_id = ? and node_id=? ",
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setString(1, processInstanceId);
                    ps.setString(2, nodeIdsList.get(i));
                }/*from w w w.  j av a 2  s.c om*/

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