forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-2178: Ability to retrieve a loan's balance at a certain poin…
…t in time in the past
- Loading branch information
Showing
29 changed files
with
1,523 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...n/java/org/apache/fineract/portfolio/loanaccount/api/pointintime/LoansPointInTimeApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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.api.pointintime; | ||
|
||
import java.util.List; | ||
import org.apache.fineract.infrastructure.core.api.DateParam; | ||
import org.apache.fineract.portfolio.loanaccount.api.pointintime.data.RetrieveLoansPointInTimeExternalIdsRequest; | ||
import org.apache.fineract.portfolio.loanaccount.api.pointintime.data.RetrieveLoansPointInTimeRequest; | ||
import org.apache.fineract.portfolio.loanaccount.data.LoanPointInTimeData; | ||
|
||
public interface LoansPointInTimeApi { | ||
|
||
LoanPointInTimeData retrieveLoanPointInTime(Long loanId, DateParam dateParam, String dateFormat, String locale); | ||
|
||
LoanPointInTimeData retrieveLoanPointInTimeByExternalId(String loanExternalId, DateParam dateParam, String dateFormat, String locale); | ||
|
||
List<LoanPointInTimeData> retrieveLoansPointInTime(RetrieveLoansPointInTimeRequest request); | ||
|
||
List<LoanPointInTimeData> retrieveLoansPointInTimeByExternalIds(RetrieveLoansPointInTimeExternalIdsRequest request); | ||
} |
114 changes: 114 additions & 0 deletions
114
...rg/apache/fineract/portfolio/loanaccount/api/pointintime/LoansPointInTimeApiDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* 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.api.pointintime; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.fineract.infrastructure.core.api.DateParam; | ||
import org.apache.fineract.infrastructure.core.data.DateFormat; | ||
import org.apache.fineract.infrastructure.core.domain.ExternalId; | ||
import org.apache.fineract.infrastructure.core.service.ExternalIdFactory; | ||
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; | ||
import org.apache.fineract.portfolio.loanaccount.api.pointintime.data.RetrieveLoansPointInTimeExternalIdsRequest; | ||
import org.apache.fineract.portfolio.loanaccount.api.pointintime.data.RetrieveLoansPointInTimeRequest; | ||
import org.apache.fineract.portfolio.loanaccount.data.LoanPointInTimeData; | ||
import org.apache.fineract.portfolio.loanaccount.exception.LoanNotFoundException; | ||
import org.apache.fineract.portfolio.loanaccount.service.LoanPointInTimeService; | ||
import org.apache.fineract.portfolio.loanaccount.service.LoanReadPlatformService; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class LoansPointInTimeApiDelegate implements LoansPointInTimeApi { | ||
|
||
private static final String RESOURCE_NAME_FOR_PERMISSIONS = "LOAN"; | ||
|
||
private final LoanPointInTimeService loanPointInTimeService; | ||
private final LoanReadPlatformService loanReadPlatformService; | ||
private final PlatformSecurityContext context; | ||
|
||
@Override | ||
public LoanPointInTimeData retrieveLoanPointInTime(Long loanId, DateParam dateParam, String dateFormat, String locale) { | ||
context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS); | ||
return getLoanPointInTime(loanId, dateParam, dateFormat, locale); | ||
} | ||
|
||
@Override | ||
public LoanPointInTimeData retrieveLoanPointInTimeByExternalId(String loanExternalIdStr, DateParam dateParam, String dateFormat, | ||
String locale) { | ||
context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS); | ||
ExternalId loanExternalId = ExternalIdFactory.produce(loanExternalIdStr); | ||
Long loanId = resolveExternalId(loanExternalId); | ||
|
||
return getLoanPointInTime(loanId, dateParam, dateFormat, locale); | ||
} | ||
|
||
@Override | ||
public List<LoanPointInTimeData> retrieveLoansPointInTime(RetrieveLoansPointInTimeRequest request) { | ||
context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS); | ||
List<Long> loanIds = request.getLoanIds(); | ||
DateParam dateParam = request.getDate(); | ||
String dateFormat = request.getDateFormat(); | ||
String locale = request.getLocale(); | ||
|
||
return getLoansPointInTime(loanIds, dateParam, dateFormat, locale); | ||
} | ||
|
||
@Override | ||
public List<LoanPointInTimeData> retrieveLoansPointInTimeByExternalIds(RetrieveLoansPointInTimeExternalIdsRequest request) { | ||
context.authenticatedUser().validateHasReadPermission(RESOURCE_NAME_FOR_PERMISSIONS); | ||
List<String> loanExternalIds = request.getExternalIds(); | ||
DateParam dateParam = request.getDate(); | ||
String dateFormat = request.getDateFormat(); | ||
String locale = request.getLocale(); | ||
|
||
List<ExternalId> externalIds = ExternalIdFactory.produce(loanExternalIds); | ||
List<Long> loanIds = resolveExternalIds(externalIds); | ||
|
||
return getLoansPointInTime(loanIds, dateParam, dateFormat, locale); | ||
} | ||
|
||
private List<LoanPointInTimeData> getLoansPointInTime(List<Long> loanIds, DateParam dateParam, String dateFormat, String locale) { | ||
DateFormat df = StringUtils.isBlank(dateFormat) ? null : new DateFormat(dateFormat); | ||
LocalDate date = dateParam.getDate("date", df, locale); | ||
return loanPointInTimeService.retrieveAt(loanIds, date); | ||
} | ||
|
||
private LoanPointInTimeData getLoanPointInTime(Long loanId, DateParam dateParam, String dateFormat, String locale) { | ||
DateFormat df = StringUtils.isBlank(dateFormat) ? null : new DateFormat(dateFormat); | ||
LocalDate date = dateParam.getDate("date", df, locale); | ||
return loanPointInTimeService.retrieveAt(loanId, date); | ||
} | ||
|
||
private List<Long> resolveExternalIds(List<ExternalId> loanExternalIds) { | ||
loanExternalIds.forEach(ExternalId::throwExceptionIfEmpty); | ||
return loanReadPlatformService.retrieveLoanIdsByExternalIds(loanExternalIds); | ||
} | ||
|
||
private Long resolveExternalId(ExternalId loanExternalId) { | ||
loanExternalId.throwExceptionIfEmpty(); | ||
Long resolvedLoanId = loanReadPlatformService.retrieveLoanIdByExternalId(loanExternalId); | ||
if (resolvedLoanId == null) { | ||
throw new LoanNotFoundException(loanExternalId); | ||
} | ||
return resolvedLoanId; | ||
} | ||
} |
Oops, something went wrong.