Advertisement
Guest User

BT AddAuditReport

a guest
Nov 11th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.72 KB | None | 0 0
  1. package org.jleaf.common.bo.audit;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.persistence.Query;
  6.  
  7. import org.jleaf.common.ComboIdConstants;
  8. import org.jleaf.common.CommonConstants;
  9. import org.jleaf.common.CommonExceptionConstants;
  10. import org.jleaf.common.dao.audit.AuditReportDao;
  11. import org.jleaf.common.dao.audit.ReportFormCounterDao;
  12. import org.jleaf.common.dao.audit.ReprintReportFormDao;
  13. import org.jleaf.common.entity.audit.AuditReport;
  14. import org.jleaf.common.entity.audit.ReportFormCounter;
  15. import org.jleaf.common.entity.audit.ReprintReportForm;
  16. import org.jleaf.core.BusinessFunction;
  17. import org.jleaf.core.BusinessTransaction;
  18. import org.jleaf.core.CoreException;
  19. import org.jleaf.core.DefaultBusinessTransaction;
  20. import org.jleaf.core.Dto;
  21. import org.jleaf.core.GeneralConstants;
  22. import org.jleaf.core.annotation.Info;
  23. import org.jleaf.core.annotation.InfoIn;
  24. import org.jleaf.core.annotation.InfoOut;
  25. import org.jleaf.util.DateUtil;
  26. import org.jleaf.util.DtoUtil;
  27. import org.jleaf.util.GsonUtil;
  28. import org.jleaf.util.ValidationUtil;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Qualifier;
  33. import org.springframework.stereotype.Service;
  34.  
  35. /**
  36.  * Add user audit <br/>
  37.  *
  38.  *
  39.  * @author Fredi, 2 Juli 2015
  40.  * @version 1.0
  41.  */
  42. @Service
  43. @InfoIn(value = {
  44.         @Info(name = "userId", description = "User Id", type = Long.class, required = true),
  45.         @Info(name = "tenantLoginId", description = "User login id", type = Long.class, required = true),
  46.         @Info(name = "roleId", description = "Role id", type = String.class, required = true),
  47.         @Info(name = "activityDatetime", description = "activity Datetime", type = String.class, required = true),
  48.         @Info(name = "reportConfigName", description = "Report config name", type = String.class, required = true),
  49.         @Info(name = "reportParameter", description = "Report Parameter", type = String.class),
  50.         @Info(name = "ipAddress", description = "IP Address", type = String.class),
  51.        
  52.         @Info(name = "userLoginId", description = "User login Id", type = Long.class, required = true),
  53.         @Info(name = "datetime", description = "Datetime", type = String.class, required = true)       
  54. })
  55. @InfoOut(value = {
  56.         })
  57. public class AddAuditReport extends DefaultBusinessTransaction implements BusinessTransaction {
  58.     private static final Logger log = LoggerFactory.getLogger(AddAuditReport.class);
  59.  
  60.     @Autowired
  61.     @Qualifier("findUserById")
  62.     BusinessFunction findUserById;
  63.    
  64.     @Autowired
  65.     @Qualifier("findRoleById")
  66.     BusinessFunction findRoleById;
  67.    
  68.     @Autowired
  69.     @Qualifier("findReportFormByIndex")
  70.     BusinessFunction findReportFormByIndex;
  71.  
  72.     @Autowired
  73.     @Qualifier("isReportFormExistsByIndex")
  74.     BusinessFunction isReportFormExistsByIndex;
  75.  
  76.     @Autowired
  77.     @Qualifier("isComboValueExistsByCode")
  78.     BusinessFunction isComboValueExistsByCode;
  79.  
  80.     @Autowired
  81.     @Qualifier("isReportFormConfigExists")
  82.     BusinessFunction isReportFormConfigExists;
  83.  
  84.     @Autowired
  85.     @Qualifier("isReportFormCounterExistsByIndex")
  86.     BusinessFunction isReportFormCounterExistsByIndex;
  87.  
  88.     @Autowired
  89.     @Qualifier("isSystemConfigExistsByParameterCode")
  90.     BusinessFunction isSystemConfigExistsByParameterCode;
  91.    
  92.     @Autowired
  93.     @Qualifier("isReprintReportFormExistsByFormCounter")
  94.     BusinessFunction isReprintReportFormExistsByFormCounter;
  95.    
  96.     @Autowired
  97.     @Qualifier("findLastAuditReportByReportCodeAndDoc")
  98.     BusinessFunction findLastAuditReportByReportCodeAndDoc;
  99.    
  100.     @Autowired
  101.     AuditReportDao auditReportDao;
  102.  
  103.     @Autowired
  104.     ReportFormCounterDao reportFormCounterDao;
  105.    
  106.     @Autowired
  107.     ReprintReportFormDao reprintReportFormDao;
  108.    
  109.     @Override
  110.     public String getDescription() {
  111.         return "Add description";
  112.     }
  113.  
  114.     @SuppressWarnings("unchecked")
  115.     @Override
  116.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  117.         log.info("inputDto BF : "+inputDto);
  118.        
  119.         // val key
  120.         ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
  121.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  122.        
  123.         ValidationUtil.valDtoContainsKey(inputDto, "userId");
  124.         ValidationUtil.valDtoContainsKey(inputDto, "roleId");
  125.         ValidationUtil.valDtoContainsKey(inputDto, "activityDatetime");
  126.         ValidationUtil.valDtoContainsKey(inputDto, "reportConfigName");
  127.         ValidationUtil.valDtoContainsKey(inputDto, "reportParameter");
  128.         ValidationUtil.valDtoContainsKey(inputDto, "ipAddress");
  129.        
  130.         Long tenantLoginId = inputDto.getLong("tenantLoginId");
  131.         Long userLoginId = inputDto.getLong("userLoginId");
  132.         Long userId = inputDto.getLong("userId");
  133.         Long roleId = inputDto.getLong("roleId");
  134.         String activityDatetime = inputDto.getString("activityDatetime");
  135.         String reportParameter = inputDto.getString("reportParameter");
  136.         String reportConfigName = inputDto.getString("reportConfigName");
  137.         String ipAddress = inputDto.getString("ipAddress");
  138.         String datetime = inputDto.getString("datetime");
  139.        
  140.         // val format
  141.         ValidationUtil.valDatetime(activityDatetime, "activityDatetime");
  142.        
  143.         // validation existensi data
  144.         findUserById.execute(new Dto().put("id", userId));
  145.         findRoleById.execute(new Dto().put("id", roleId));
  146.        
  147.         /**
  148.          * Add report audit
  149.          * # find reportCode from combo by findComboValueByCode
  150.          * # Check if report reprintable by findReportFormConfig with key reprintable
  151.          * # get last report form counter
  152.          * # prepare insert into t_audit_report
  153.          * # prepare insert / update report form counter --> update lastCounter + 1
  154.          * # insert into t_audit_report
  155.          * # insert / update report form counter
  156.          */
  157.         // chek report parameter
  158.         Dto outputIsSystemConfigExistsByParameterCode = isSystemConfigExistsByParameterCode.execute(new Dto()
  159.                 .put("tenantId", tenantLoginId)
  160.                 .put("parameterCode", CommonConstants.PARAMETER_REPORT_AUDIT_ACTIVE));
  161.        
  162.         log.info("outputIsSystemConfigExistsByParameterCode : "+outputIsSystemConfigExistsByParameterCode);
  163.         if(outputIsSystemConfigExistsByParameterCode.getBoolean("exists") ){
  164.             Dto systemConfigDto = outputIsSystemConfigExistsByParameterCode.getDto("systemConfigDto");
  165.             if(GeneralConstants.YES.equals(systemConfigDto.get("value"))){
  166.                 // continue process
  167.             } else{
  168.                 inputDto.put("skip", true);
  169.                 return null;   
  170.             }
  171.         } else{
  172.             inputDto.put("skip", true);
  173.             return null;
  174.         }
  175.        
  176.         // find from combo
  177.         Dto outputIsComboValueExistsByCode = isComboValueExistsByCode.execute(new Dto()
  178.                 .put("comboId", ComboIdConstants.COMBO_REPORT_FORM)
  179.                 .put("code", reportConfigName)
  180.                 .put("varName", "reportConfig"));
  181.        
  182.         log.info("outputIsComboValueExistsByCode : "+outputIsComboValueExistsByCode);
  183.         if(!outputIsComboValueExistsByCode.getBoolean("exists")){
  184.             inputDto.put("skip", true);
  185.             return null;
  186.         }
  187.        
  188.         Dto comboReportDto = outputIsComboValueExistsByCode.getDto("comboValueDto");
  189.        
  190.         String reportCode = comboReportDto.getString("codeGroup");
  191.        
  192.         // find report form
  193.         Dto outputIsReportFormExistsByIndex = isReportFormExistsByIndex.execute(new Dto()
  194.                 .put("reportCode", reportCode));
  195.        
  196.         String reportDocStatement = GeneralConstants.EMPTY_VALUE; //reportFormDto.getString("reportDocStatement");
  197.         String docNo = GeneralConstants.EMPTY_VALUE;
  198.         String docDate = GeneralConstants.EMPTY_VALUE;
  199.         String keyDocId = GeneralConstants.EMPTY_VALUE;
  200.         Long docTypeId = GeneralConstants.NULL_REF_VALUE_LONG; //reportFormDto.getLong("docTypeId");
  201.         Long ouId = GeneralConstants.NULL_REF_VALUE_LONG;
  202.         Long printCounter = 1l;
  203.         Long docId = GeneralConstants.NULL_REF_VALUE_LONG;
  204.         String[] paramsKey = null;
  205.        
  206.        
  207.         Dto reportParameterDto = new Dto(reportParameter);
  208.         if(outputIsReportFormExistsByIndex.getBoolean("exists")){
  209.             Dto reportFormDto = outputIsReportFormExistsByIndex.getDto("reportFormDto");
  210.            
  211.             // validate report parameter must contain key doc id
  212.             keyDocId = reportFormDto.getString("keyDocId");
  213.             if(!keyDocId.isEmpty()){
  214.                 paramsKey = keyDocId.split(CommonConstants.SEPARATOR_KEY_DOC);
  215.             }
  216.            
  217.             // validation reportParameter must contain all parameter key
  218.             for (String paramKey : paramsKey) {
  219.                 ValidationUtil.valDtoContainsKey(reportParameterDto, paramKey.trim());
  220.             }
  221.            
  222.             //docId = Long.valueOf(reportParameterDto.getLong( keyDocId ).toString());
  223.             reportDocStatement = reportFormDto.getString("reportDocStatement");
  224.             docTypeId = reportFormDto.getLong("docTypeId");
  225.         }
  226.        
  227.         // get document information
  228.         Dto docInfoDto = new Dto();
  229.        
  230.         if(!reportDocStatement.trim().isEmpty()){
  231.             Query queryDocInfo = auditReportDao.createNativeQuery(reportDocStatement);
  232.  
  233.             if(paramsKey!=null){
  234.                 for (String paramKey : paramsKey) {
  235.                     queryDocInfo.setParameter(paramKey, reportParameterDto.get(paramKey));
  236.                 }
  237.             }
  238.            
  239.             List<Object[]> result = queryDocInfo.getResultList();
  240.            
  241.             try{
  242.                 // query must result
  243.                 // 0 docId 1 docTypeId 2 docNo 3 docDate 4 ouId
  244.                 List<Dto> resultDtoList = DtoUtil.createDtoListFromArray(result, "docId", "docTypeId", "docNo", "docDate", "ouId");
  245.                 docInfoDto = resultDtoList.get(0);
  246.                
  247.                 docId = Long.valueOf(docInfoDto.get("docId").toString());
  248.                 docNo = docInfoDto.getString("docNo");
  249.                 docDate = docInfoDto.getString("docDate");
  250.                 docTypeId = docInfoDto.getLong("docTypeId");
  251.                 ouId = docInfoDto.getLong("ouId");
  252.                
  253.             } catch(Exception ex){
  254.                 log.warn("Unsuccesfully execute query from reportDocStatement : "+reportDocStatement);
  255.             }
  256.         }
  257.        
  258.         // find config reprintable or not
  259.         String reprintable = GeneralConstants.YES;
  260.        
  261.         Dto outputIsReportFormConfigExists = isReportFormConfigExists.execute(new Dto()
  262.                 .put("tenantId", tenantLoginId)
  263.                 .put("configKey", CommonConstants.CONFIG_KEY_REPRINTABLE)
  264.                 .put("reportCode", reportCode));
  265.        
  266.         log.info("outputIsReportFormConfigExists : "+outputIsReportFormConfigExists);
  267.         if(outputIsReportFormConfigExists.getBoolean("exists")){
  268.             Dto reportFormConfigDto = outputIsReportFormConfigExists.getDto("reportFormConfigDto");
  269.             reprintable = reportFormConfigDto.getString("configValue");
  270.         }
  271.  
  272.         // find last counter
  273.         Dto resultIsReportFormCounterExistsByIndex = isReportFormCounterExistsByIndex.execute(new Dto()
  274.                 .put("tenantId", tenantLoginId)
  275.                 .put("reportCode", reportCode)
  276.                 .put("docId", docId)
  277.                 .put("docTypeId", docTypeId)
  278.                 );
  279.         log.info("resultIsReportFormCounterExistsByIndex : "+resultIsReportFormCounterExistsByIndex);
  280.                
  281.         // chreck if can print
  282.         Dto reportFormCounterDto = new Dto();
  283.         Dto reprintReportFormDto = new Dto();
  284.         boolean isUpdateReprintReportForm = false;
  285.         if(resultIsReportFormCounterExistsByIndex.getBoolean("exists")){
  286.             reportFormCounterDto = resultIsReportFormCounterExistsByIndex.getDto("reportFormCounterDto");
  287.             printCounter = new Long(reportFormCounterDto.getLong("lastCounter") + 1);
  288.             if(GeneralConstants.NO.equals(reprintable)){
  289.                 if(reportFormCounterDto.getLong("lastCounter") > 0){
  290.                     // find request form
  291.                     Dto outputIsReprintReportFormExistsByFormCounter = isReprintReportFormExistsByFormCounter.execute(new Dto()
  292.                             .put("tenantId", tenantLoginId)
  293.                             .put("reportFormCounterId", reportFormCounterDto.get("id"))
  294.                             .put("lastCounter", reportFormCounterDto.get("lastCounter")) );
  295.  
  296.                     log.info("outputIsReprintReportFormExistsByFormCounter : "+outputIsReprintReportFormExistsByFormCounter);                  
  297.                     if(outputIsReprintReportFormExistsByFormCounter.getBoolean("exists")){
  298.                         reprintReportFormDto = outputIsReprintReportFormExistsByFormCounter.getDto("reprintReportFormDto");
  299.                        
  300.                         if(CommonConstants.RELEASED_TRANSACTION.equals(reprintReportFormDto.get("statusDoc"))
  301.                                 && userId.equals(reprintReportFormDto.get("reqUserId")) ){
  302.                             isUpdateReprintReportForm = true;
  303.                            
  304.                             reprintReportFormDto.put("reprintDatetime", activityDatetime);
  305.                             reprintReportFormDto.put("statusDoc", CommonConstants.FINAL_TRANSACTION);
  306.                            
  307.                             this.prepareUpdateAudit(reprintReportFormDto, userLoginId, datetime);
  308.                            
  309.                             // update lastCounter
  310.                             reportFormCounterDto.put("lastCounter", printCounter);
  311.                         }else{
  312.                             Dto outputFindLastAuditReportByReportCodeAndDoc = findLastAuditReportByReportCodeAndDoc.execute(new Dto()
  313.                                     .put("tenantId", tenantLoginId)
  314.                                     .put("reportCode", reportCode)
  315.                                     .put("docId", docId)
  316.                                     .put("docTypeId", docTypeId)
  317.                                     );
  318.                             if(!userId.equals(reprintReportFormDto.get("reqUserId"))){
  319.                                 //
  320.                                 Dto userRequestReprintDto = findUserById.execute(new Dto().put("id", reprintReportFormDto.get("reqUserId")));
  321.                                
  322.                                 throw new CoreException(CommonExceptionConstants.REPRINT_REPORT_ALREADY_REQUESTED, docNo, userRequestReprintDto.get("name"), DateUtil.displayDateTime(reprintReportFormDto.getString("createDateTime")));
  323.                             }
  324.                            
  325.                             throw new CoreException(CommonExceptionConstants.REPORT_ALREADY_PRINTED, docNo, outputFindLastAuditReportByReportCodeAndDoc.get("name"), DateUtil.displayDateTime(outputFindLastAuditReportByReportCodeAndDoc.getString("activityDatetime")));
  326.                         }
  327.                     } else {
  328.                         Dto outputFindLastAuditReportByReportCodeAndDoc = findLastAuditReportByReportCodeAndDoc.execute(new Dto()
  329.                             .put("tenantId", tenantLoginId)
  330.                             .put("reportCode", reportCode)
  331.                             .put("docId", docId)
  332.                             .put("docTypeId", docTypeId)
  333.                         );
  334.                        
  335.                         throw new CoreException(CommonExceptionConstants.REPORT_ALREADY_PRINTED, docNo, outputFindLastAuditReportByReportCodeAndDoc.get("name"), DateUtil.displayDateTime(outputFindLastAuditReportByReportCodeAndDoc.getString("activityDatetime")));
  336.                     }
  337.                 }
  338.             } else{
  339.                 reportFormCounterDto.put("lastCounter", printCounter);
  340.             }
  341.         } else{
  342.             ReportFormCounter reportFormCounter = new ReportFormCounter();
  343.             reportFormCounter.setTenantId(tenantLoginId);
  344.             reportFormCounter.setReportCode(reportCode);
  345.             reportFormCounter.setDocId(docId);
  346.             reportFormCounter.setDocTypeId(docTypeId);
  347.             reportFormCounter.setDocNo(docNo);
  348.             reportFormCounter.setDocDate(docDate);
  349.             reportFormCounter.setDocTypeId(docTypeId);
  350.             reportFormCounter.setLastCounter(1l);
  351.             reportFormCounter.setOuId(ouId);
  352.             reportFormCounter.setVersion(0);
  353.            
  354.             reportFormCounterDto = new Dto(reportFormCounter);
  355.             this.prepareInsertAudit(reportFormCounterDto, userLoginId, datetime);
  356.         }
  357.         this.prepareUpdateAudit(reportFormCounterDto, userLoginId, datetime);
  358.        
  359.         // prepare insert audit
  360.         AuditReport auditReport = new AuditReport();
  361.         auditReport.setTenantId(tenantLoginId);
  362.         auditReport.setReportCode(reportCode);
  363.         auditReport.setReportConfigName(reportConfigName);
  364.         auditReport.setDocId(docId);
  365.         auditReport.setDocTypeId(docTypeId);
  366.         auditReport.setOuId(ouId);
  367.         auditReport.setDocNo(docNo);
  368.         auditReport.setDocDate(docDate);
  369.         auditReport.setUserId(userId);
  370.         auditReport.setRoleId(roleId);
  371.         auditReport.setActivityDatetime(activityDatetime);
  372.         auditReport.setReportParameter(reportParameter);
  373.         auditReport.setPrintCounter(printCounter);
  374.         auditReport.setIpAddress(ipAddress);
  375.         auditReport.setVersion(0);
  376.        
  377.         Dto auditReportDto = new Dto(auditReport);
  378.         this.prepareInsertAudit(auditReportDto, userLoginId, datetime);
  379.         this.prepareUpdateAudit(auditReportDto, userLoginId, datetime);
  380.        
  381.         inputDto.put("skip", false);
  382.         inputDto.put("isUpdateReprintReportForm", isUpdateReprintReportForm);
  383.         inputDto.put("auditReportDto", auditReportDto);
  384.         inputDto.put("reportFormCounterDto", reportFormCounterDto);
  385.         inputDto.put("reprintReportFormDto", reprintReportFormDto);
  386.        
  387.         return null;
  388.     }
  389.  
  390.     @Override
  391.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  392.         // insert audit report
  393.         if(!inputDto.getBoolean("skip")){
  394.             Dto auditReportDto = inputDto.getDto("auditReportDto");
  395.             AuditReport auditReport = GsonUtil.fromDto(auditReportDto, AuditReport.class);
  396.             auditReportDao.persist(auditReport);
  397.            
  398.             Dto reportFormCounterDto = inputDto.getDto("reportFormCounterDto");
  399.             ReportFormCounter reportFormCounter = GsonUtil.fromDto(reportFormCounterDto, ReportFormCounter.class);
  400.             if(reportFormCounterDto.get("id")==null){
  401.                 reportFormCounterDao.persist(reportFormCounter);
  402.             } else{
  403.                 reportFormCounter = reportFormCounterDao.merge(reportFormCounter.getId(), reportFormCounter);
  404.             }
  405.            
  406.             boolean isUpdateReprintReportForm = inputDto.getBoolean("isUpdateReprintReportForm");
  407.             if(isUpdateReprintReportForm){
  408.                 Dto reprintReportFormDto = inputDto.getDto("reprintReportFormDto");
  409.                 ReprintReportForm reprintReportForm = GsonUtil.fromDto(reprintReportFormDto, ReprintReportForm.class);
  410.                 reprintReportForm = reprintReportFormDao.merge(reprintReportForm.getId(), reprintReportForm);
  411.                
  412.                 log.debug("Updated reprint report form : "+new Dto(reprintReportForm));
  413.             }
  414.            
  415.             return new Dto(auditReport)
  416.                 .put("reportFormCounterId", reportFormCounter.getId());
  417.         }
  418.        
  419.         return new Dto();
  420.     }
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement