Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.erp.scheduler;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.UUID;
- import org.jleaf.core.BusinessFunction;
- import org.jleaf.core.Dto;
- import org.jleaf.erp.AppConstants;
- import org.jleaf.util.DateUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.dao.DataAccessException;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.PreparedStatementCallback;
- import org.springframework.jdbc.core.PreparedStatementCreator;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- /**
- * @author Widana, 18 Apr 2017
- *
- */
- @Service
- public class UpdateBalanceStockScheduler {
- private static final Logger log = LoggerFactory.getLogger(UpdateBalanceStockScheduler.class);
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Autowired
- @Qualifier("getTenantList")
- BusinessFunction getTenantList;
- @Autowired
- @Qualifier("getWarehouseListAdvance")
- BusinessFunction getWarehouseListAdvance;
- @SuppressWarnings("unchecked")
- @Scheduled(cron = "${cron.config}")
- public void run() throws Exception {
- Dto tenantListDto = getTenantList.execute(new Dto());
- List<Dto> tenantList = tenantListDto.getList("tenantList");
- Connection conn = jdbcTemplate.getDataSource().getConnection();
- conn.setAutoCommit(false);
- if (tenantList != null && !tenantList.isEmpty()){
- for(Dto tenantDto : tenantList){
- Dto input = new Dto();
- input.put("flagWarehouse", AppConstants.DEFAULT_EMPTY_ID);
- input.put("active", "");
- input.put("code", "");
- input.put("name", "");
- input.put("tenantId", Long.parseLong(tenantDto.get("id").toString()));
- Dto warehouseListDto = getWarehouseListAdvance.execute(input);
- List<Dto> warehouseList = warehouseListDto.getList("warehouseList");
- if (warehouseList != null && !warehouseList.isEmpty()){
- for(Dto warehouseDto : warehouseList){
- StringBuilder builder = new StringBuilder();
- builder.append("SELECT ").append(AppConstants.FUNCTION_UPDATE_PRODUCT_BALANCE_STOCK_FOR_INDOCOM).append(" ( ");
- builder.append("'").append(UUID.randomUUID().toString()).append("',"); // sessionId
- builder.append(warehouseDto.getLong("tenantId")).append(","); // tenantId
- builder.append(AppConstants.EMPTY_ID).append(","); // userId
- builder.append(AppConstants.EMPTY_ID).append(","); // roleId
- builder.append("'").append(DateUtil.dateTimeNow()).append("',"); // datetime
- builder.append(warehouseDto.getLong("id")).append(","); // warehouseId
- builder.append("'").append(new StringBuilder(8).append(DateUtil.getYearString(DateUtil.dateNow()))
- .append(DateUtil.getMonthString(DateUtil.dateNow())).append(DateUtil.getDay(DateUtil.dateNow())).toString())
- .append("',"); // datefrom
- builder.append("'").append(new StringBuilder(8).append(DateUtil.getEndOfMonth(DateUtil.dateNow())).toString()); // date to
- builder.append("')");
- final String sqlFunction = builder.toString();
- log.info(sqlFunction);
- PreparedStatementCreator psc = new PreparedStatementCreator() {
- public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
- log.debug(" <<<<<<<<<<");
- PreparedStatement ps = con.prepareStatement(sqlFunction);
- return ps;
- }
- };
- PreparedStatementCallback<Boolean> action = new PreparedStatementCallback<Boolean>() {
- public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
- return ps.execute();
- }
- };
- jdbcTemplate.execute(psc, action);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement