| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.fineract.portfolio.loanaccount.loanschedule.service; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.apache.fineract.infrastructure.core.api.JsonCommand; |
| import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; |
| import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; |
| import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; |
| import org.apache.fineract.portfolio.loanaccount.data.LoanTermVariationsData; |
| import org.apache.fineract.portfolio.loanaccount.data.ScheduleGeneratorDTO; |
| import org.apache.fineract.portfolio.loanaccount.domain.Loan; |
| import org.apache.fineract.portfolio.loanaccount.domain.LoanAccountDomainService; |
| import org.apache.fineract.portfolio.loanaccount.domain.LoanTermVariations; |
| import org.apache.fineract.portfolio.loanaccount.service.LoanAssembler; |
| import org.apache.fineract.portfolio.loanaccount.service.LoanUtilService; |
| import org.apache.fineract.useradministration.domain.AppUser; |
| import org.joda.time.LocalDate; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| |
| @Service |
| public class LoanScheduleWritePlatformServiceImpl implements LoanScheduleWritePlatformService { |
| |
| private final LoanAccountDomainService loanAccountDomainService; |
| private final LoanAssembler loanAssembler; |
| private final LoanScheduleAssembler loanScheduleAssembler; |
| private final PlatformSecurityContext context; |
| private final LoanUtilService loanUtilService; |
| |
| @Autowired |
| public LoanScheduleWritePlatformServiceImpl(final LoanAccountDomainService loanAccountDomainService, |
| final LoanScheduleAssembler loanScheduleAssembler, final LoanAssembler loanAssembler, final PlatformSecurityContext context, |
| final LoanUtilService loanUtilService) { |
| this.loanAccountDomainService = loanAccountDomainService; |
| this.loanScheduleAssembler = loanScheduleAssembler; |
| this.loanAssembler = loanAssembler; |
| this.context = context; |
| this.loanUtilService = loanUtilService; |
| } |
| |
| @Override |
| public CommandProcessingResult addLoanScheduleVariations(final Long loanId, final JsonCommand command) { |
| final Loan loan = this.loanAssembler.assembleFrom(loanId); |
| Map<Long, LoanTermVariations> loanTermVariations = new HashMap<>(); |
| for (LoanTermVariations termVariations : loan.getLoanTermVariations()) { |
| loanTermVariations.put(termVariations.getId(), termVariations); |
| } |
| this.loanScheduleAssembler.assempleVariableScheduleFrom(loan, command.json()); |
| |
| this.loanAccountDomainService.saveLoanWithDataIntegrityViolationChecks(loan); |
| final Map<String, Object> changes = new HashMap<>(); |
| List<LoanTermVariationsData> newVariationsData = new ArrayList<>(); |
| List<LoanTermVariations> modifiedVariations = loan.getLoanTermVariations(); |
| for (LoanTermVariations termVariations : modifiedVariations) { |
| if (loanTermVariations.containsKey(termVariations.getId())) { |
| loanTermVariations.remove(termVariations.getId()); |
| } else { |
| newVariationsData.add(termVariations.toData()); |
| } |
| } |
| if (!loanTermVariations.isEmpty()) { |
| changes.put("removedVariations", loanTermVariations.keySet()); |
| } |
| changes.put("loanTermVariations", newVariationsData); |
| return new CommandProcessingResultBuilder() // |
| .withCommandId(command.commandId()) // |
| .withLoanId(loanId) // |
| .with(changes) // |
| .build(); |
| } |
| |
| @Override |
| public CommandProcessingResult deleteLoanScheduleVariations(final Long loanId) { |
| final Loan loan = this.loanAssembler.assembleFrom(loanId); |
| List<LoanTermVariations> variations = loan.getLoanTermVariations(); |
| List<Long> deletedVariations = new ArrayList<>(variations.size()); |
| for (LoanTermVariations loanTermVariations : variations) { |
| deletedVariations.add(loanTermVariations.getId()); |
| } |
| final Map<String, Object> changes = new HashMap<>(); |
| changes.put("removedEntityIds", deletedVariations); |
| loan.getLoanTermVariations().clear(); |
| final LocalDate recalculateFrom = null; |
| ScheduleGeneratorDTO scheduleGeneratorDTO = this.loanUtilService.buildScheduleGeneratorDTO(loan, recalculateFrom); |
| AppUser currentUser = this.context.getAuthenticatedUserIfPresent(); |
| loan.regenerateRepaymentSchedule(scheduleGeneratorDTO, currentUser); |
| this.loanAccountDomainService.saveLoanWithDataIntegrityViolationChecks(loan); |
| return new CommandProcessingResultBuilder() // |
| .withLoanId(loanId) // |
| .with(changes) // |
| .build(); |
| } |
| |
| } |