/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mifosplatform.portfolio.loanaccount.service; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.joda.time.LocalDate; import org.mifosplatform.infrastructure.jobs.annotation.CronTarget; import org.mifosplatform.infrastructure.jobs.exception.JobExecutionException; import org.mifosplatform.infrastructure.jobs.service.JobName; import org.mifosplatform.portfolio.loanaccount.data.LoanScheduleAccrualData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class LoanAccrualPlatformServiceImpl implements LoanAccrualPlatformService { private final LoanReadPlatformService loanReadPlatformService; private final LoanAccrualWritePlatformService loanAccrualWritePlatformService; @Autowired public LoanAccrualPlatformServiceImpl(final LoanReadPlatformService loanReadPlatformService, final LoanAccrualWritePlatformService loanAccrualWritePlatformService) { this.loanReadPlatformService = loanReadPlatformService; this.loanAccrualWritePlatformService = loanAccrualWritePlatformService; } @Override @CronTarget(jobName = JobName.ADD_ACCRUAL_ENTRIES) public void addAccrualAccounting() throws JobExecutionException { Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retriveScheduleAccrualData(); StringBuilder sb = new StringBuilder(); Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>(); for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) { if (loanDataMap.containsKey(accrualData.getLoanId())) { loanDataMap.get(accrualData.getLoanId()).add(accrualData); } else { Collection<LoanScheduleAccrualData> accrualDatas = new ArrayList<>(); accrualDatas.add(accrualData); loanDataMap.put(accrualData.getLoanId(), accrualDatas); } } for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) { try { this.loanAccrualWritePlatformService.addAccrualAccounting(mapEntry.getKey(), mapEntry.getValue()); } catch (Exception e) { Throwable realCause = e; if (e.getCause() != null) { realCause = e.getCause(); } sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage()); } } if (sb.length() > 0) { throw new JobExecutionException(sb.toString()); } } @Override @CronTarget(jobName = JobName.ADD_PERIODIC_ACCRUAL_ENTRIES) public void addPeriodicAccruals() throws JobExecutionException { String errors = addPeriodicAccruals(LocalDate.now()); if (errors.length() > 0) { throw new JobExecutionException(errors); } } @Override public String addPeriodicAccruals(final LocalDate tilldate) { Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas = this.loanReadPlatformService.retrivePeriodicAccrualData(tilldate); return addPeriodicAccruals(tilldate, loanScheduleAccrualDatas); } @Override public String addPeriodicAccruals(final LocalDate tilldate, Collection<LoanScheduleAccrualData> loanScheduleAccrualDatas) { StringBuilder sb = new StringBuilder(); Map<Long, Collection<LoanScheduleAccrualData>> loanDataMap = new HashMap<>(); for (final LoanScheduleAccrualData accrualData : loanScheduleAccrualDatas) { if (loanDataMap.containsKey(accrualData.getLoanId())) { loanDataMap.get(accrualData.getLoanId()).add(accrualData); } else { Collection<LoanScheduleAccrualData> accrualDatas = new ArrayList<>(); accrualDatas.add(accrualData); loanDataMap.put(accrualData.getLoanId(), accrualDatas); } } for (Map.Entry<Long, Collection<LoanScheduleAccrualData>> mapEntry : loanDataMap.entrySet()) { try { this.loanAccrualWritePlatformService.addPeriodicAccruals(tilldate, mapEntry.getKey(), mapEntry.getValue()); } catch (Exception e) { Throwable realCause = e; if (e.getCause() != null) { realCause = e.getCause(); } sb.append("failed to add accural transaction for loan " + mapEntry.getKey() + " with message " + realCause.getMessage()); } } return sb.toString(); } }