Advertisement
henikseptiana15

Untitled

Dec 22nd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.59 KB | None | 0 0
  1. package org.jleaf.erp.inv.bo.inquiryexchange;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.persistence.Query;
  6.  
  7. import org.jleaf.core.AbstractBusinessFunction;
  8. import org.jleaf.core.BusinessFunction;
  9. import org.jleaf.core.Dto;
  10. import org.jleaf.core.GeneralConstants;
  11. import org.jleaf.core.annotation.Info;
  12. import org.jleaf.core.annotation.InfoIn;
  13. import org.jleaf.core.annotation.InfoOut;
  14. import org.jleaf.core.dao.CriteriaHelper;
  15. import org.jleaf.core.dao.QueryBuilder;
  16. import org.jleaf.erp.inv.InventoryConstantsForKtmt;
  17. import org.jleaf.erp.inv.dao.InOutExchangeDao;
  18. import org.jleaf.erp.inv.entity.InOutExchange;
  19. import org.jleaf.util.DtoUtil;
  20. import org.jleaf.util.ValidationUtil;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25.  
  26. /**
  27.  *
  28.  * @author Sarah, Jun 14, 2016
  29.  *
  30.  * @modified by Henik, 21 Des 2017
  31.  * - Merapikan querynya, terutam bagian filter beberapa saya ubah pakai StringBuilder
  32.  *
  33.  **/
  34.  
  35. @Service
  36. @InfoIn(value = {
  37.         @Info(name = "tenantId", description = "tenant Id", type = Long.class),
  38.         @Info(name = "ouId", description = "ou Id", type = Long.class),
  39.         @Info(name = "dateFrom", description = "date from", type = String.class),
  40.         @Info(name = "dateTo", description = "date to", type = String.class),
  41.         @Info(name = "customer", description = "partner / customer", type = Long.class),
  42.         @Info(name = "docNo", description = "doc no", type = String.class),
  43.         @Info(name = "statusDocReq", description = "status Doc request exchange", type = String.class),
  44.         @Info(name = "statusDocExchangeOut", description = "status Doc exchange out", type = String.class),
  45.         @Info(name = "statusDocExchangeIn", description = "status Doc exchange in", type = String.class),
  46.         @Info(name = "brandId", description = "brand Id", type = Long.class),
  47.         @Info(name = "limit", description = "limit", type = Long.class, required = false),
  48.         @Info(name = "offset", description = "offset", type = Long.class, required = false)
  49. })
  50. @InfoOut(value = {
  51.         @Info(name = "inquiryExchangeList", description = "inquiryExchangeList")
  52. })
  53. public class GetInquiryExchangeList extends AbstractBusinessFunction implements BusinessFunction {
  54.     private static final Logger log = LoggerFactory.getLogger(GetInquiryExchangeList.class);
  55.    
  56.     @Autowired
  57.     private InOutExchangeDao inOutExchangeDao;
  58.    
  59.     private String statusDocExchangeIn;
  60.     private String statusDocExchangeOut;
  61.     private String kondisiWhere;
  62.    
  63.     @Override
  64.     public String getDescription() {
  65.         return "Get Inquiry Exchange List";
  66.     }
  67.  
  68.     @SuppressWarnings({ "unchecked" })
  69.     @Override
  70.     public Dto execute(Dto inputDto) throws Exception {
  71.         log.info("paramInput GET =:"+inputDto);
  72.         ValidationUtil.valDtoContainsKey(inputDto, "tenantId");
  73.         ValidationUtil.valDtoContainsKey(inputDto, "ouId");
  74.         ValidationUtil.valDtoContainsKey(inputDto, "dateFrom");
  75.         ValidationUtil.valDtoContainsKey(inputDto, "dateTo");
  76.         ValidationUtil.valDtoContainsKey(inputDto, "customer");
  77.         ValidationUtil.valDtoContainsKey(inputDto, "docNo");
  78.         ValidationUtil.valDtoContainsKey(inputDto, "statusDocReq");
  79.         ValidationUtil.valDtoContainsKey(inputDto, "statusDocExchangeOut");
  80.         ValidationUtil.valDtoContainsKey(inputDto, "statusDocExchangeIn");
  81.         ValidationUtil.valDtoContainsKey(inputDto, "brandId");
  82.  
  83.         Long tenantId = inputDto.getLong("tenantId");
  84.         Long ouId = inputDto.getLong("ouId");
  85.         String dateFrom = inputDto.getString("dateFrom");
  86.         String dateTo = inputDto.getString("dateTo");
  87.         String customer = inputDto.getString("customer");
  88.         String docNo = inputDto.getString("docNo");
  89.         String statusDocReq = inputDto.getString("statusDocReq");
  90.         Long brandId = inputDto.getLong("brandId");
  91.        
  92.         if(inputDto.getString("statusDocExchangeOut").equals(GeneralConstants.YES)) {
  93.             statusDocExchangeOut = " AND D.status_doc IN('R', 'F') ";
  94.         } else if(inputDto.getString("statusDocExchangeOut").equals(InventoryConstantsForKtmt.IN_PROGRESS_TRANSACTION)) {
  95.             statusDocExchangeOut = " AND D.status_doc IN('D', 'I') ";
  96.         } else if(inputDto.getString("statusDocExchangeOut").equals(GeneralConstants.NO)) {
  97.             statusDocExchangeOut = " AND D.status_doc IS NULL ";
  98.         }
  99.        
  100.         if(inputDto.getString("statusDocExchangeIn").equals(GeneralConstants.YES)) {
  101.             statusDocExchangeIn = " AND E.status_doc IN('R', 'F') ";
  102.         } else if(inputDto.getString("statusDocExchangeIn").equals(InventoryConstantsForKtmt.IN_PROGRESS_TRANSACTION)) {
  103.             statusDocExchangeIn = " AND E.status_doc IN('D', 'I') ";
  104.         } if(inputDto.getString("statusDocExchangeIn").equals(GeneralConstants.NO)) {
  105.             statusDocExchangeIn = " AND E.status_doc IS NULL ";
  106.         }
  107.            
  108.         kondisiWhere = GeneralConstants.EMPTY_VALUE;
  109.         if(!GeneralConstants.EMPTY_VALUE.equals(inputDto.getString("statusDocExchangeOut")) ||
  110.            !GeneralConstants.EMPTY_VALUE.equals(inputDto.getString("statusDocExchangeIn"))){
  111.             kondisiWhere = " WHERE ";
  112.         }
  113.        
  114.         List<Object[]> result = null;
  115.         // D untuk exchange out, E untuk exchange In
  116.         //@formatter:off
  117.         StringBuilder filterDocNo = new StringBuilder();
  118.         filterDocNo.append(" AND ")
  119.                    .append(CriteriaHelper.likeExpressionIgnoreCase(docNo, " A.doc_no "));
  120.        
  121.         StringBuilder filterCustomer = new StringBuilder();
  122.         filterCustomer.append(" AND ( ")
  123.                       .append(CriteriaHelper.likeExpressionIgnoreCase(customer, " B.partner_code "))
  124.                       .append(" OR ")
  125.                       .append(CriteriaHelper.likeExpressionIgnoreCase(customer, " B.partner_name "))
  126.                       .append(" ) ");
  127.        
  128.         QueryBuilder builder = new QueryBuilder();
  129.         builder.add(" WITH data_req_exchange AS( ")
  130.                .add("      SELECT A.in_out_exchange_id, A.tenant_id, A.ou_id, A.doc_type_id, ")
  131.                .add("             A.doc_no AS doc_no_req, A.doc_date AS doc_date_req, A.partner_id, ")
  132.                .add("             f_get_partner_code(A.partner_id) AS partner_code, ")
  133.                .add("             f_get_partner_name(A.partner_id) AS partner_name,  ")
  134.                .add("             A.brand_id, f_get_brand_code(A.brand_id) AS brand_code, ")
  135.                .add("             f_get_brand_name(A.brand_id) AS brand_name, ")
  136.                .add("             CASE WHEN A.status_doc = 'D' THEN 'Draft' ")
  137.                .add("                  WHEN A.status_doc = 'I' THEN 'In progress' ")
  138.                .add("                  WHEN A.status_doc = 'R' THEN 'Approved' ")
  139.                .add("                  END AS status_req, ")
  140.                .add("             A.remark, A.version, ")
  141.                .add("             f_get_status_cetak_report(A.in_out_exchange_id, A.doc_type_id, '").add(InventoryConstantsForKtmt.FORM_REQUEST_EXCHANGE_OUT).add("' ) AS status_print_req_exchange_out ")
  142.                .add("      FROM ").add(InOutExchange.TABLE_NAME).add(" A ")
  143.                .add("      WHERE A.tenant_id = :tenantId ")
  144.                .add("            AND A.doc_type_id = :docTypeIdReqExchange ")
  145.                .add("            AND A.ou_id = :ouId ")
  146.                .add("            AND A.doc_date BETWEEN :dateFrom AND :dateTo ")
  147.                .addIfNotEquals(brandId, GeneralConstants.NULL_REF_VALUE_LONG, " AND A.brand_id = :brandId ")
  148.                .addIfNotEmpty(docNo, filterDocNo.toString())
  149.                .addIfNotEmpty(customer, filterCustomer.toString() )
  150.                .addIfNotEmpty(statusDocReq, " AND A.status_doc = :statusDocReq ")
  151.                .add(" ) ")
  152.                .add(" SELECT A.in_out_exchange_id, COALESCE(D.in_out_exchange_id, -99) AS exchange_out_id, ")
  153.                .add("        COALESCE(E.in_out_exchange_id, -99) AS exchange_in_id, A.tenant_id, A.ou_id, A.doc_type_id, ")
  154.                .add("        doc_no_req, doc_date_req, A.partner_id, A.partner_code, ")
  155.                .add("        A.partner_name, A.brand_id, A.brand_code, A.brand_name,  ")
  156.                .add("        COALESCE(D.doc_no, :emptyString) AS doc_no_out, COALESCE(D.doc_date, :emptyString) AS doc_date_out, ")
  157.                .add("        COALESCE(E.doc_no, :emptyString) AS doc_no_in, COALESCE(E.doc_date, :emptyString) AS doc_date_in, ")
  158.                .add("        A.status_req, ")
  159.                .add("        CASE WHEN D.status_doc = 'D' OR D.status_doc = 'I' THEN 'In progress' ")
  160.                .add("             WHEN D.status_doc = 'R' OR D.status_doc = 'F' THEN 'Yes' ELSE 'No' ")
  161.                .add("             END AS status_out, ")
  162.                .add("        CASE WHEN E.status_doc = 'D' OR E.status_doc = 'I' THEN 'In progress' ")
  163.                .add("             WHEN E.status_doc = 'R' OR E.status_doc = 'F' THEN 'Yes' ELSE 'No' ")
  164.                .add("             END AS status_in, ")
  165.                .add("        A.remark, A.version, ")
  166.                .add("        A.status_print_req_exchange_out, ")
  167.                .add("        f_get_status_cetak_report(COALESCE(D.in_out_exchange_id, -99), COALESCE(D.doc_type_id, -99), '").add(InventoryConstantsForKtmt.FORM_EXCHANGE_OUT).add("' ) AS status_print_exchange_out, ")
  168.                .add("        f_get_status_cetak_report(COALESCE(E.in_out_exchange_id, -99), COALESCE(E.doc_type_id, -99), '").add(InventoryConstantsForKtmt.FORM_EXCHANGE_IN).add("' ) AS status_print_exchange_in ")
  169.                .add(" FROM data_req_exchange A ")
  170.                .add(" LEFT JOIN ").add(InOutExchange.TABLE_NAME).add(" D ON A.in_out_exchange_id = D.req_id AND A.doc_type_id = D.req_doc_type_id")
  171.                .add(" LEFT JOIN ").add(InOutExchange.TABLE_NAME).add(" E ON D.in_out_exchange_id = E.ref_id AND D.doc_type_id = E.ref_doc_type_id")
  172.                .add(  kondisiWhere )
  173.                .addIfNotEmpty(inputDto.getString("statusDocExchangeOut"), statusDocExchangeOut )
  174.                .addIfNotEmpty(inputDto.getString("statusDocExchangeIn"), statusDocExchangeIn )
  175.                .add(" ORDER BY A.doc_date_req, A.doc_no_req ");
  176.         //@formatter:on    
  177.         if(inputDto.get("limit")!=null) builder.add(" LIMIT " + inputDto.getLong("limit"));
  178.         if(inputDto.get("offset")!=null) builder.add(" OFFSET " + inputDto.getLong("offset"));
  179.        
  180.         log.info("builder GET -->:"+builder.toString());
  181.        
  182.         Query q = inOutExchangeDao.createNativeQuery(builder.toString());
  183.         q.setParameter("tenantId", tenantId);
  184.         q.setParameter("docTypeIdReqExchange", InventoryConstantsForKtmt.DOCUMENT_REQ_EXCHANGE_OUT);
  185.         q.setParameter("dateFrom", dateFrom);
  186.         q.setParameter("dateTo", dateTo);
  187.         q.setParameter("ouId", ouId);
  188.         q.setParameter("emptyString", GeneralConstants.EMPTY_VALUE);
  189.         if(!GeneralConstants.NULL_REF_VALUE_LONG.equals(brandId)){
  190.             q.setParameter("brandId", brandId);
  191.         }
  192.         if(!GeneralConstants.EMPTY_VALUE.equals(statusDocReq)){
  193.             q.setParameter("statusDocReq", statusDocReq);
  194.         }
  195.        
  196.         result = q.getResultList();
  197.         return new Dto().putList("inquiryExchangeList", DtoUtil.createDtoListFromArray(result, "reqExchangeId", "exchangeOutId",
  198.                 "exchangeInId", "tenantId", "ouId", "docTypeId", "docNoReq", "docDateReq", "partnerId", "partnerCode", "partnerName",
  199.                 "brandId", "brandCode", "brandName", "docNoOut", "docDateOut", "docNoIn", "docDateIn", "statusReq", "statusOut",
  200.                 "statusIn", "remark", "version", "statusPrintRexo", "statusPrintExo", "statusPrintExi"));
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement