Advertisement
aadddrr

Untitled

Dec 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.jleaf.erp.scheduler;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6. import java.util.List;
  7. import java.util.UUID;
  8.  
  9. import org.jleaf.core.BusinessFunction;
  10. import org.jleaf.core.Dto;
  11. import org.jleaf.erp.AppConstants;
  12. import org.jleaf.util.DateUtil;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Qualifier;
  17. import org.springframework.dao.DataAccessException;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. import org.springframework.jdbc.core.PreparedStatementCallback;
  20. import org.springframework.jdbc.core.PreparedStatementCreator;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.stereotype.Service;
  23.  
  24. /**
  25. * @author Widana, 18 Apr 2017
  26. *
  27. */
  28.  
  29. @Service
  30. public class UpdateBalanceStockScheduler {
  31.    
  32.     private static final Logger log = LoggerFactory.getLogger(UpdateBalanceStockScheduler.class);
  33.  
  34.     @Autowired
  35.     private JdbcTemplate jdbcTemplate;
  36.    
  37.     @Autowired
  38.     @Qualifier("getTenantList")
  39.     BusinessFunction getTenantList;
  40.  
  41.     @Autowired
  42.     @Qualifier("getWarehouseListAdvance")
  43.     BusinessFunction getWarehouseListAdvance;  
  44.    
  45.    
  46.     @SuppressWarnings("unchecked")
  47.     @Scheduled(cron = "${cron.config}")
  48.     public void run() throws Exception {
  49.         Dto tenantListDto = getTenantList.execute(new Dto());      
  50.         List<Dto> tenantList = tenantListDto.getList("tenantList");
  51.  
  52.         Connection conn = jdbcTemplate.getDataSource().getConnection();
  53.         conn.setAutoCommit(false);
  54.        
  55.         if (tenantList != null && !tenantList.isEmpty()){
  56.             for(Dto tenantDto : tenantList){
  57.  
  58.                 Dto input = new Dto();
  59.                 input.put("flagWarehouse", AppConstants.DEFAULT_EMPTY_ID);
  60.                 input.put("active", "");
  61.                 input.put("code", "");
  62.                 input.put("name", "");
  63.                 input.put("tenantId", Long.parseLong(tenantDto.get("id").toString()));
  64.                
  65.                 Dto warehouseListDto = getWarehouseListAdvance.execute(input);     
  66.                 List<Dto> warehouseList = warehouseListDto.getList("warehouseList");
  67.                
  68.                 if (warehouseList != null && !warehouseList.isEmpty()){
  69.                     for(Dto warehouseDto : warehouseList){
  70.  
  71.                         StringBuilder builder = new StringBuilder();
  72.                         builder.append("SELECT ").append(AppConstants.FUNCTION_UPDATE_PRODUCT_BALANCE_STOCK_FOR_INDOCOM).append(" ( ");
  73.                         builder.append("'").append(UUID.randomUUID().toString()).append("',"); // sessionId
  74.                         builder.append(warehouseDto.getLong("tenantId")).append(","); // tenantId
  75.                         builder.append(AppConstants.EMPTY_ID).append(","); // userId
  76.                         builder.append(AppConstants.EMPTY_ID).append(","); // roleId
  77.                         builder.append("'").append(DateUtil.dateTimeNow()).append("',"); // datetime
  78.                         builder.append(warehouseDto.getLong("id")).append(","); // warehouseId
  79.                         builder.append("'").append(new StringBuilder(8).append(DateUtil.getYearString(DateUtil.dateNow()))
  80.                                 .append(DateUtil.getMonthString(DateUtil.dateNow())).append(DateUtil.getDay(DateUtil.dateNow())).toString())
  81.                                 .append("',"); // datefrom
  82.                         builder.append("'").append(new StringBuilder(8).append(DateUtil.getEndOfMonth(DateUtil.dateNow())).toString()); // date to
  83.                         builder.append("')");
  84.                          
  85.                         final String sqlFunction = builder.toString();
  86.                                
  87.                         log.info(sqlFunction);
  88.                          
  89.                         PreparedStatementCreator psc = new PreparedStatementCreator() {
  90.                             public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
  91.                                 log.debug(" <<<<<<<<<<");
  92.                                PreparedStatement ps = con.prepareStatement(sqlFunction);
  93.                    
  94.                                return ps;
  95.                             }
  96.                         };
  97.                          
  98.                         PreparedStatementCallback<Boolean> action = new PreparedStatementCallback<Boolean>() {
  99.                             public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
  100.                                return ps.execute();
  101.                             }
  102.                         };
  103.                          
  104.                         jdbcTemplate.execute(psc, action);
  105.                     }
  106.                 }
  107.                
  108.             }
  109.         }
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement