Advertisement
KumaranathFernando

Untitled

Jun 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. @Component
  2.  
  3. @Scope("prototype")
  4.  
  5. public class AccrualPoster implements Runnable {
  6.  
  7.  
  8.  
  9.     private final static Logger logger = LoggerFactory.getLogger("accrual");
  10.  
  11.  
  12.  
  13.     private List<LoanScheduleAccrualData> loanScheduleAccrualDatas;
  14.  
  15.     private ConfigurationDomainService configurationDomainService;
  16.  
  17.     private LoanAccrualWritePlatformService loanAccrualWritePlatformService;
  18.  
  19.     private LocalDate tillDate;
  20.  
  21.     private MifosPlatformTenant tenant;
  22.  
  23.  
  24.  
  25.     public void setAccrualPoster(final List<LoanScheduleAccrualData> loanScheduleAccrualDatas) {
  26.  
  27.         this.loanScheduleAccrualDatas = loanScheduleAccrualDatas;
  28.  
  29.     }
  30.  
  31.  
  32.  
  33.     public void setConfigurationDomainService(final ConfigurationDomainService configurationDomainService) {
  34.  
  35.         this.configurationDomainService = configurationDomainService;
  36.  
  37.     }
  38.  
  39.  
  40.  
  41.     public void setLoanAccrualWritePlatformService(final LoanAccrualWritePlatformService loanAccrualWritePlatformService) {
  42.  
  43.         this.loanAccrualWritePlatformService = loanAccrualWritePlatformService;
  44.  
  45.     }
  46.  
  47.  
  48.  
  49.     public void setLocalDate(final LocalDate tillDate) {
  50.  
  51.         this.tillDate = tillDate;
  52.  
  53.     }
  54.  
  55.  
  56.  
  57.     public void setTenant(final MifosPlatformTenant tenant) {
  58.  
  59.         this.tenant = tenant;
  60.  
  61.     }
  62.  
  63.  
  64.  
  65.     @Override
  66.  
  67.     public void run() {
  68.         //set the tenant id
  69.         ThreadLocalContextUtil.setTenant(tenant);
  70.  
  71.         StringBuilder sb = new StringBuilder();
  72.  
  73.           Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>();
  74.        
  75.         // if invoked updated list of loan schedule is not empty
  76.           if(loanScheduleAccrualDatas.size() > 0)
  77.  
  78.               logger.info("Inside Thread - Pre Parse - " + loanScheduleAccrualDatas.size());
  79.  
  80.         //Iterate through the list of accruals to get the matching loan_id
  81.         //if found loan_id on map, update new accuruals for that loan
  82.         //else add a new entry on the map with updated accurals for that loan
  83.  
  84.           for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) {
  85.  
  86.               if (loanDataMap.containsKey(accrualData.getLoanId())) {
  87.  
  88.                       Collection<LoanScheduleAccrualData> accrualDatas =  loanDataMap.get(accrualData.getLoanId());
  89.  
  90.                       accrualDatas.add(accrualData);
  91.  
  92.                       loanDataMap.put(accrualData.getLoanId(), accrualDatas);
  93.  
  94.               } else {
  95.  
  96.                   Collection<LoanScheduleAccrualData> accrualDatas = new ArrayList<>();
  97.  
  98.                   accrualDatas.add(accrualData);
  99.  
  100.                   loanDataMap.put(accrualData.getLoanId(), accrualDatas);
  101.  
  102.               }
  103.  
  104.           }
  105.  
  106.  
  107.         // why  tax component? what for ?
  108.           Map<String, BigDecimal> taxComponents = this.configurationDomainService.retriveTaxComponents();
  109.  
  110.           logger.info("Inside Thread - Post Parse - " + loanDataMap.size());
  111.  
  112.         // if current date time is supplied calculate accuruls until current date for each loan
  113.         //else calculate accuruals until due date of job execution
  114.  
  115.           for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) {
  116.  
  117.               if(tillDate != null) {
  118.  
  119.                   try {
  120.  
  121.                           logger.info("loan id - " +  mapEntry.getKey());
  122.  
  123.                       this.loanAccrualWritePlatformService.addPeriodicAccruals(tillDate, mapEntry.getKey(), mapEntry.getValue(), taxComponents);
  124.  
  125.                   } catch (PlatformDataIntegrityException e) {
  126.  
  127.                           if(e.getDefaultUserMessage() == null)
  128.  
  129.                                e.printStackTrace();
  130.  
  131.                       sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + e.getDefaultUserMessage());
  132.  
  133.                   } catch (AbstractPlatformDomainRuleException e) {
  134.  
  135.                           if(e.getDefaultUserMessage() == null)
  136.  
  137.                                e.printStackTrace();
  138.  
  139.                       sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + e.getDefaultUserMessage());
  140.  
  141.                   } catch (AbstractPlatformResourceNotFoundException e) {
  142.  
  143.                           if(e.getDefaultUserMessage() == null)
  144.  
  145.                                e.printStackTrace();
  146.  
  147.                       sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + e.getDefaultUserMessage());
  148.  
  149.                   } catch (Exception e) {
  150.  
  151.                           if(e.getMessage() == null)
  152.  
  153.                                e.printStackTrace();
  154.  
  155.                       Throwable realCause = e;
  156.  
  157.                       if (e.getCause() != null) {
  158.  
  159.                           realCause = e.getCause();
  160.  
  161.                       }
  162.  
  163.                       if(realCause.getMessage() == null)
  164.  
  165.                               realCause.printStackTrace();
  166.  
  167.                       sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage());
  168.  
  169.                   }
  170.  
  171.               } else {
  172.  
  173.                   try {
  174.  
  175.                       this.loanAccrualWritePlatformService.addAccrualAccounting(mapEntry.getKey(), mapEntry.getValue(), taxComponents);
  176.  
  177.                   } catch (Exception e) {
  178.  
  179.                       Throwable realCause = e;
  180.  
  181.                       if (e.getCause() != null) {
  182.  
  183.                           realCause = e.getCause();
  184.  
  185.                       }
  186.  
  187.                       sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage());
  188.  
  189.                   }
  190.  
  191.               }
  192.  
  193.               if(sb.length() > 0)
  194.  
  195.                   logger.info(sb.toString());
  196.  
  197.           }
  198.  
  199.  
  200.  
  201.     }
  202.  
  203.  
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement