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:gov.nih.nci.cabig.caaers.dao.MedDRADao.java

/**
 * This method loads the meddra_hlt_pt mapping table. New ids that were generated while loading data into the meddra_hlt and
 * meddra_pt tables are used. Earlier meddra_code were used as ids and supporting multiple versions was not possible in that case.
 * /* w  w w  .j a v a 2 s  . c o  m*/
 * @param llts
 * @param startIndex
 * @param hltCodeToIdMap
 * @param ptCodeToIdMap
 * @return
 */
public int[] insertHLTxPT(final List llts, final int startIndex, final int version_id,
        final Map<String, Integer> hltCodeToIdMap, final Map<String, Integer> ptCodeToIdMap) {

    String sql = "insert into meddra_hlt_pt (meddra_hlt_id, meddra_pt_id, version_id) values (?,?,?)";

    String dataBase = "";
    if (properties.getProperty(DB_NAME) != null) {
        dataBase = properties.getProperty(DB_NAME);
    }
    if (dataBase.equals(ORACLE_DB))
        sql = "insert into meddra_hlt_pt (id,meddra_hlt_id, meddra_pt_id, version_id) values (SEQ_MEDDRA_HLT_ID.NEXTVAL,?,?,?)";

    BatchPreparedStatementSetter setter = null;
    setter = new BatchPreparedStatementSetter() {

        int stIndex = startIndex;

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

        public void setValues(PreparedStatement ps, int index) throws SQLException {
            String[] llt = (String[]) llts.get(index);

            ps.setInt(1, (hltCodeToIdMap.get(llt[0]).intValue()));
            ps.setInt(2, (ptCodeToIdMap.get(llt[1]).intValue()));
            ps.setInt(3, version_id);
        }
    };

    return jdbcTemplate.batchUpdate(sql, setter);
}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaPlaceDao.java

/**
 * Populate the droplet places table.//w  w w .  j  a v a  2 s  .  co m
 * 
 * @param drops
 */
private void insertDropletPlaces(List<Drop> drops) {

    // List of drop IDs in the drops list
    List<Long> dropIds = new ArrayList<Long>();
    // List of places in a drop
    Map<Long, Set<Long>> dropletPlacesMap = new HashMap<Long, Set<Long>>();
    for (Drop drop : drops) {

        if (drop.getPlaces() == null)
            continue;

        dropIds.add(drop.getId());

        for (Place place : drop.getPlaces()) {
            Set<Long> places = null;
            if (dropletPlacesMap.containsKey(drop.getId())) {
                places = dropletPlacesMap.get(drop.getId());
            } else {
                places = new HashSet<Long>();
                dropletPlacesMap.put(drop.getId(), places);
            }

            places.add(place.getId());
        }
    }

    // Find droplet places that already exist in the db
    String sql = "SELECT droplet_id, place_id FROM droplets_places WHERE droplet_id in (:ids)";

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("ids", dropIds);

    List<Map<String, Object>> results = this.namedJdbcTemplate.queryForList(sql, params);

    // Remove already existing droplet_places from our Set
    for (Map<String, Object> result : results) {
        long dropletId = ((Number) result.get("droplet_id")).longValue();
        long placeId = ((Number) result.get("place_id")).longValue();

        Set<Long> placeSet = dropletPlacesMap.get(dropletId);
        if (placeSet != null) {
            placeSet.remove(placeId);
        }
    }

    // Insert the remaining items in the set into the db
    sql = "INSERT INTO droplets_places (droplet_id, place_id) VALUES (?,?)";

    final List<long[]> dropletPlacesList = new ArrayList<long[]>();
    for (Long dropletId : dropletPlacesMap.keySet()) {
        for (Long placeId : dropletPlacesMap.get(dropletId)) {
            long[] dropletPlace = { dropletId, placeId };
            dropletPlacesList.add(dropletPlace);
        }
    }
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            long[] dropletPlace = dropletPlacesList.get(i);
            ps.setLong(1, dropletPlace[0]);
            ps.setLong(2, dropletPlace[1]);
        }

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

From source file:info.raack.appliancedetection.evaluation.data.JDBCDatabase.java

@Transactional
public void saveSimulation(final Simulation simulation) {
    // save simulation / evaluation
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(insertForNewSimulationEvaluation,
            new Object[] { simulation.getId(), simulation.getStartTime().getTime(),
                    simulation.getDurationInSeconds(), simulation.getNumAppliances(),
                    simulation.getOnConcurrency(), simulation.getLabelsPerOnOff(),
                    simulation.getGroup() != null ? simulation.getGroup().getId() : null, false });

    // save simulated appliances
    for (final SimulatedAppliance appliance : simulation.getSimulatedAppliances()) {
        keyHolder = new GeneratedKeyHolder();

        if (appliance.getLabeledAppliance() != null) {
            jdbcTemplate.update(new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(insertForNewSimulatedAppliance,
                            new String[] { "id" });
                    ps.setString(1, simulation.getId());
                    ps.setInt(2, appliance.getLabeledAppliance().getId());
                    ps.setString(3, appliance.getClass().getName());
                    ps.setInt(4, appliance.getApplianceNum());
                    return ps;
                }//from w  w w  . j av a 2 s.c  o m
            }, keyHolder);
        } else {
            jdbcTemplate.update(new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(
                            insertForNewSimulatedApplianceWithoutLabelLink, new String[] { "id" });
                    ps.setString(1, simulation.getId());
                    ps.setString(2, appliance.getClass().getName());
                    ps.setInt(3, appliance.getApplianceNum());
                    return ps;
                }
            }, keyHolder);
        }

        final int simulatedApplianceDatabaseId = keyHolder.getKey().intValue();

        // save simulated appliance energy consumption
        jdbcTemplate.batchUpdate(insertForNewSimulatedApplianceEnergyConsumption,
                new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        EnergyTimestep energyTimestep = appliance.getEnergyTimesteps().get(i);

                        ps.setInt(1, simulatedApplianceDatabaseId);
                        ps.setLong(2, energyTimestep.getStartTime().getTime());
                        ps.setLong(3, energyTimestep.getEndTime().getTime());
                        ps.setInt(4, (int) energyTimestep.getEnergyConsumed());
                    }

                    public int getBatchSize() {
                        return appliance.getEnergyTimesteps().size();
                    }
                });

        // save simulated appliance state transitions
        jdbcTemplate.batchUpdate(insertForNewSimulatedApplianceStateTransitions,
                new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ApplianceStateTransition stateTransition = appliance.getAllApplianceStateTransitions()
                                .get(i);

                        ps.setInt(1, simulatedApplianceDatabaseId);
                        ps.setLong(2, stateTransition.getTime());
                        ps.setBoolean(3, stateTransition.isOn());
                    }

                    public int getBatchSize() {
                        return appliance.getAllApplianceStateTransitions().size();
                    }
                });
    }
}

From source file:shell.framework.organization.department.service.impl.TblSysDepartmentServiceI4JdbcImpl.java

public int assignPosition(final String departmentId, String[] positionIds) {
    String sql = "insert into TBL_SYS_DEPARTMENT_POSITION values (?,?)";
    final List<String> idList = new ArrayList<String>();
    for (String id : positionIds) {
        idList.add(id);//from  ww  w .  j ava 2  s  .c  o m
    }

    int[] deleteNumbers = jdbcBaseDao.batchUpdate(sql, idList, new BatchPreparedStatementSetter() {

        /*
         * (non-Javadoc)
         * @see org.springframework.jdbc.core.BatchPreparedStatementSetter#setValues(java.sql.PreparedStatement, int)
         */
        public void setValues(PreparedStatement ps, int index) throws SQLException {
            String position_id = idList.get(index);
            ps.setString(1, departmentId);
            ps.setString(2, position_id);
        }

        /*
         * (non-Javadoc)
         * @see org.springframework.jdbc.core.BatchPreparedStatementSetter#getBatchSize()
         */
        public int getBatchSize() {
            return idList.size();
        }
    });
    return deleteNumbers.length;
}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaMediaDao.java

/**
 * Populate the droplet media table./*from w w  w. ja  v  a  2s  .  c om*/
 * 
 * @param drops
 */
private void insertDropletMedia(List<Drop> drops) {

    // List of drop IDs in the drops list
    List<Long> dropIds = new ArrayList<Long>();
    // List of media in a drop
    Map<Long, Set<Long>> dropletMediaMap = new HashMap<Long, Set<Long>>();
    // List of drops and the media that is the drop image
    final List<long[]> dropImages = new ArrayList<long[]>();
    for (Drop drop : drops) {

        if (drop.getMedia() == null)
            continue;

        dropIds.add(drop.getId());

        for (Media media : drop.getMedia()) {
            Set<Long> m = null;
            if (dropletMediaMap.containsKey(drop.getId())) {
                m = dropletMediaMap.get(drop.getId());
            } else {
                m = new HashSet<Long>();
                dropletMediaMap.put(drop.getId(), m);
            }

            // Is this the drop image?
            if (drop.getImage() != null && media.getUrl().equals(drop.getImage().getUrl())) {
                long[] dropImage = { drop.getId(), media.getId() };
                dropImages.add(dropImage);
            }

            m.add(media.getId());
        }
    }

    // Find droplet media that already exist in the db
    String sql = "SELECT droplet_id, media_id FROM droplets_media WHERE droplet_id in (:ids)";

    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("ids", dropIds);

    List<Map<String, Object>> results = this.namedJdbcTemplate.queryForList(sql, params);

    // Remove already existing droplet_media from our Set
    for (Map<String, Object> result : results) {
        long dropletId = ((Number) result.get("droplet_id")).longValue();
        long mediaId = ((Number) result.get("media_id")).longValue();

        Set<Long> mediaSet = dropletMediaMap.get(dropletId);
        if (mediaSet != null) {
            mediaSet.remove(mediaId);
        }
    }

    // Insert the remaining items in the set into the db
    sql = "INSERT INTO droplets_media (droplet_id, media_id) VALUES (?,?)";

    final List<long[]> dropletMediaList = new ArrayList<long[]>();
    for (Long dropletId : dropletMediaMap.keySet()) {
        for (Long mediaId : dropletMediaMap.get(dropletId)) {
            long[] dropletMedia = { dropletId, mediaId };
            dropletMediaList.add(dropletMedia);
        }
    }
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            long[] dropletMedia = dropletMediaList.get(i);
            ps.setLong(1, dropletMedia[0]);
            ps.setLong(2, dropletMedia[1]);
        }

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

    if (dropImages.size() > 0) {
        sql = "UPDATE droplets SET droplet_image = ? WHERE id = ?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                long[] update = dropImages.get(i);
                ps.setLong(1, update[1]);
                ps.setLong(2, update[0]);
            }

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

From source file:com.carfinance.module.peoplemanage.dao.PeopleManageDao.java

/**
 * ?????/*from w  ww  . ja v a2s .com*/
 * @param all_menu_ids
 * @param role_id
 */
public void roleMenuDoConfig(final String[] all_menu_ids, final long role_id) {
    this.deleteRoleMenu(role_id);
    String sql = "insert into sys_role_menu (role_id , menu_id) values (?,?)";
    try {
        this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setLong(1, role_id);
                ps.setLong(2, Long.valueOf(all_menu_ids[i]));
            }

            public int getBatchSize() {
                return all_menu_ids.length;//??labels.length
            }
        });
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:net.duckling.ddl.service.resource.dao.ResourceDAOImpl.java

@Override
public void updateTagMap(final List<Resource> resList) {
    this.getJdbcTemplate().batchUpdate(UPDATE_TAG_MAP, new BatchPreparedStatementSetter() {

        @Override//from  ww  w. j a va  2s .com
        public int getBatchSize() {
            return (null == resList || resList.isEmpty()) ? 0 : resList.size();
        }

        @Override
        public void setValues(PreparedStatement ps, int index) throws SQLException {
            Resource res = resList.get(index);
            String tagmap = JsonUtil.getJSONString(res.getTagMap());
            int i = 0;
            ps.setString(++i, tagmap);
            ps.setInt(++i, res.getTid());
            ps.setInt(++i, res.getRid());
        }

    });
}

From source file:gov.nih.nci.cabig.caaers.datamigrator.UserDataMigrator.java

/**
 * This method inserts records into CSM_USER_GROUP table
 * @param userId//from w  w w  . ja v  a2 s.c  o m
 * @param groups
 */
@SuppressWarnings("unchecked")
protected void insertIntoCsmUserGroup(final String userId, final List groups, boolean onOracleDB) {

    String sql = getInsertCsmUserGroupSql(onOracleDB);
    BatchPreparedStatementSetter setter = null;
    setter = new BatchPreparedStatementSetter() {

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

        public void setValues(PreparedStatement ps, int index) throws SQLException {
            ps.setInt(1, Integer.parseInt(userId));
            ps.setString(2, groups.get(index).toString());
        }
    };
    getJdbcTemplate().batchUpdate(sql, setter);
}

From source file:net.duckling.ddl.service.resource.dao.ResourceDAOImpl.java

@Override
public int updateMarkedUserSet(final List<Resource> resList) {
    this.getJdbcTemplate().batchUpdate(UPDATE_USER_SET, new BatchPreparedStatementSetter() {

        @Override/* w  ww . ja  va  2  s. c  o  m*/
        public int getBatchSize() {
            return (null == resList || resList.isEmpty()) ? 0 : resList.size();
        }

        @Override
        public void setValues(PreparedStatement ps, int index) throws SQLException {
            Resource res = resList.get(index);
            int i = 0;
            ps.setObject(++i, res.getMarkedUserSet());
            ps.setInt(++i, res.getRid());
        }

    });
    return 1;
}

From source file:gov.nih.nci.cabig.caaers.dao.MedDRADao.java

/**
 * This method loads the meddra_hlgt_hlt mapping table. New ids that were generated while loading data into meddra_hlgt and meddra_hlt
 * tables are used. Earlier meddra_code were used and supporting multiple versions was not possible in that case.
 *  /*  w  w w .  j a  v a  2 s .c  o  m*/
 * @param llts
 * @param startIndex
 * @param hlgtCodeToIdMap
 * @param hltCodeToIdMap
 * @return
 */
public int[] insertHLGTxHLT(final List llts, final int startIndex, final int version_id,
        final Map<String, Integer> hlgtCodeToIdMap, final Map<String, Integer> hltCodeToIdMap) {

    String sql = "insert into meddra_hlgt_hlt (meddra_hlgt_id, meddra_hlt_id, version_id) values (?,?,?)";

    String dataBase = "";
    if (properties.getProperty(DB_NAME) != null) {
        dataBase = properties.getProperty(DB_NAME);
    }
    if (dataBase.equals(ORACLE_DB))
        sql = "insert into meddra_hlgt_hlt (id,meddra_hlgt_id, meddra_hlt_id, version_id) values (SEQ_MEDDRA_HLGT_HLT_ID.NEXTVAL,?,?,?)";

    BatchPreparedStatementSetter setter = null;
    setter = new BatchPreparedStatementSetter() {

        int stIndex = startIndex;

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

        public void setValues(PreparedStatement ps, int index) throws SQLException {
            String[] llt = (String[]) llts.get(index);

            ps.setInt(1, (hlgtCodeToIdMap.get(llt[0]).intValue()));
            ps.setInt(2, (hltCodeToIdMap.get(llt[1]).intValue()));
            ps.setInt(3, version_id);
        }
    };

    return jdbcTemplate.batchUpdate(sql, setter);
}