Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.UriInfo;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
Expand All @@ -45,6 +46,7 @@
import org.apache.fineract.infrastructure.security.service.PlatformUserRightsContext;
import org.apache.fineract.investor.config.InvestorModuleIsEnabledCondition;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesData;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesTemplateData;
import org.apache.fineract.investor.service.ExternalAssetOwnerLoanProductAttributesReadService;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
Expand All @@ -60,6 +62,16 @@ public class ExternalAssetOwnerLoanProductAttributesApiResource {
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;
private final ExternalAssetOwnerLoanProductAttributesReadService externalAssetOwnerLoanProductAttributesReadService;

@GET
@Path("/template")
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve External Asset Owner Loan Product Attributes Template", operationId = "retrieveTemplateExternalAssetOwnerLoanProductAttributes", description = "Retrieves all available external asset owner loan product attributes and the values each attribute can take.")
public List<ExternalTransferLoanProductAttributesTemplateData> getExternalAssetOwnerLoanProductAttributesTemplate() {
platformUserRightsContext.isAuthenticated();

return externalAssetOwnerLoanProductAttributesReadService.retrieveExternalAssetOwnerLoanProductAttributesTemplate();
}

@POST
@Path("/{loanProductId}/attributes")
@Consumes({ MediaType.APPLICATION_JSON })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

import java.io.Serializable;
import lombok.Data;
import lombok.Getter;

/**
* Data object representing an external transfer loan product attribute
*/

@Data
@Getter
public class ExternalTransferLoanProductAttributesData implements Serializable {

private Long attributeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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.investor.data;

import java.io.Serializable;
import java.util.List;
import lombok.Data;
import lombok.Getter;

/**
* Data object representing an external transfer loan product attribute
*/

@Data
@Getter
Comment thread
adamsaghy marked this conversation as resolved.
public class ExternalTransferLoanProductAttributesTemplateData implements Serializable {

private Long attributeId;
private Long loanProductId;
private String attributeKey;
private List<String> attributeValues;
private boolean isMultiValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
*/
package org.apache.fineract.investor.data.attribute;

import java.util.List;

public interface ExternalAssetOwnerLoanProductAttribute {

String getAttributeKey();

String getAttributeValue();

List<String> getAttributeValues();

boolean validate(String attributeValue);

boolean isMultiValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
*/
package org.apache.fineract.investor.data.attribute;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public enum SettlementModelExternalAssetOwnerLoanProductAttribute implements ExternalAssetOwnerLoanProductAttribute {

DEFAULT_SETTLEMENT("DEFAULT_SETTLEMENT"), //
DELAYED_SETTLEMENT("DELAYED_SETTLEMENT"); //
DEFAULT_SETTLEMENT, //
DELAYED_SETTLEMENT; //

private final String attributeKey;
private final String attributeValue;

SettlementModelExternalAssetOwnerLoanProductAttribute(String attributeValue) {
SettlementModelExternalAssetOwnerLoanProductAttribute() {
this.attributeKey = "SETTLEMENT_MODEL";
this.attributeValue = attributeValue;
}

@Override
Expand All @@ -38,6 +40,21 @@ public String getAttributeKey() {

@Override
public String getAttributeValue() {
return attributeValue;
return name();
}

@Override
public List<String> getAttributeValues() {
return Arrays.stream(values()).map(Enum::name).toList();
}

@Override
public boolean validate(String attributeValue) {
return this.getAttributeValue().equals(attributeValue.toUpperCase(Locale.ROOT));
}

@Override
public boolean isMultiValue() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/
package org.apache.fineract.investor.service;

import java.util.List;
import org.apache.fineract.infrastructure.core.service.Page;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesData;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesTemplateData;

public interface ExternalAssetOwnerLoanProductAttributesReadService {

List<ExternalTransferLoanProductAttributesTemplateData> retrieveExternalAssetOwnerLoanProductAttributesTemplate();

Page<ExternalTransferLoanProductAttributesData> retrieveAllLoanProductAttributesByLoanProductId(Long loanProductId,
String attributeKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@
*/
package org.apache.fineract.investor.service;

import static org.reflections.scanners.Scanners.SubTypes;

import jakarta.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.service.Page;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesData;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesTemplateData;
import org.apache.fineract.investor.data.attribute.ExternalAssetOwnerLoanProductAttribute;
import org.apache.fineract.investor.domain.ExternalAssetOwnerLoanProductAttributes;
import org.apache.fineract.investor.domain.ExternalAssetOwnerLoanProductAttributesRepository;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
import org.apache.fineract.portfolio.loanproduct.exception.LoanProductNotFoundException;
import org.reflections.Reflections;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
Expand All @@ -41,9 +48,29 @@
@Transactional(readOnly = true)
public class ExternalAssetOwnerLoanProductAttributesReadServiceImpl implements ExternalAssetOwnerLoanProductAttributesReadService {

private static final String INVESTOR_PATH = "org.apache.fineract.investor";

private final ExternalAssetOwnerLoanProductAttributesRepository externalAssetOwnerLoanProductAttributesRepository;
private final LoanProductRepository loanProductRepository;
private final ExternalAssetOwnerLoanProductAttributesMapper mapper;
private final Set<Class<?>> implementingClasses = new Reflections(INVESTOR_PATH)
.get(SubTypes.of(ExternalAssetOwnerLoanProductAttribute.class).asClass());

@Override
public List<ExternalTransferLoanProductAttributesTemplateData> retrieveExternalAssetOwnerLoanProductAttributesTemplate() {
List<ExternalTransferLoanProductAttributesTemplateData> result = new ArrayList<>();

for (Class<?> implementingClass : implementingClasses) {
if (implementingClass.isEnum()) {
Arrays.stream(implementingClass.getEnumConstants()).map(ExternalAssetOwnerLoanProductAttribute.class::cast)
.forEach(attribute -> addAttributeValues(result, attribute));
Comment thread
adamsaghy marked this conversation as resolved.
} else {
addAttributeValues(result, createAttribute(implementingClass));
}
}

return result;
}

@Override
@Cacheable(cacheNames = "externalAssetOwnerLoanProductAttributes", key = "T(org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat(#loanProductId.toString() + #attributeKey)", unless = "#attributeKey == null")
Expand All @@ -61,6 +88,28 @@ public Page<ExternalTransferLoanProductAttributesData> retrieveAllLoanProductAtt
pageOfAttributeData.getNumberOfElements());
}

private void addAttributeValues(List<ExternalTransferLoanProductAttributesTemplateData> result,
ExternalAssetOwnerLoanProductAttribute attribute) {
if (result.stream().anyMatch(data -> data.getAttributeKey().equals(attribute.getAttributeKey()))) {
return;
}

ExternalTransferLoanProductAttributesTemplateData data = new ExternalTransferLoanProductAttributesTemplateData();
data.setAttributeKey(attribute.getAttributeKey());
data.setAttributeValues(attribute.getAttributeValues());
data.setMultiValue(attribute.isMultiValue());
result.add(data);
}

private static ExternalAssetOwnerLoanProductAttribute createAttribute(final Class<?> implementingClass) {
try {
return (ExternalAssetOwnerLoanProductAttribute) implementingClass.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException | ClassCastException exception) {
throw new IllegalStateException("Unable to create external asset owner loan product attribute: " + implementingClass.getName(),
exception);
}
}

private void validateLoanProduct(final Long loanProductId) {
if (loanProductId == null) {
throw new IllegalArgumentException("At least one of the following parameters must be provided: loanProductId");
Expand All @@ -70,7 +119,7 @@ private void validateLoanProduct(final Long loanProductId) {
}
}

public static Specification<ExternalAssetOwnerLoanProductAttributes> retrieveLoanProductAttributesByLoanProductIdAndAttributeKeySpecification(
private static Specification<ExternalAssetOwnerLoanProductAttributes> retrieveLoanProductAttributesByLoanProductIdAndAttributeKeySpecification(
final Long loanProductId, final String attributeKey) {
return (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -154,8 +153,7 @@ private void validateExternalAssetOwnerLoanProductAttribute(String attributeKey,
if (implementingClass.isEnum()) {
for (Object obj : implementingClass.getEnumConstants()) {
ExternalAssetOwnerLoanProductAttribute objEnum = (ExternalAssetOwnerLoanProductAttribute) obj;
if (objEnum.getAttributeKey().equals(attributeKey)
&& objEnum.getAttributeValue().equals(attributeValue.toUpperCase(Locale.ROOT))) {
if (objEnum.getAttributeKey().equals(attributeKey) && objEnum.validate(attributeValue)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.fineract.investor.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -31,6 +32,7 @@
import java.util.stream.Stream;
import org.apache.fineract.infrastructure.core.service.Page;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesData;
import org.apache.fineract.investor.data.ExternalTransferLoanProductAttributesTemplateData;
import org.apache.fineract.investor.domain.ExternalAssetOwnerLoanProductAttributes;
import org.apache.fineract.investor.domain.ExternalAssetOwnerLoanProductAttributesRepository;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository;
Expand Down Expand Up @@ -101,6 +103,20 @@ public void testRetrieveAllLoanProductAttributesByLoanProductId(Long loanProduct
verify(mapper, times(1)).mapLoanProductAttributes(attributes);
}

@Test
public void testRetrieveExternalAssetOwnerLoanProductAttributesTemplate() {
// when
List<ExternalTransferLoanProductAttributesTemplateData> result = underTest
.retrieveExternalAssetOwnerLoanProductAttributesTemplate();

// then
ExternalTransferLoanProductAttributesTemplateData settlementModel = result.stream()
.filter(attribute -> "SETTLEMENT_MODEL".equals(attribute.getAttributeKey())).findFirst().orElseThrow();
assertEquals(1, result.size());
assertEquals(List.of("DEFAULT_SETTLEMENT", "DELAYED_SETTLEMENT"), settlementModel.getAttributeValues());
assertFalse(settlementModel.isMultiValue());
}

@Test
public void testRetrieveAllLoanProductAttributesByLoanProductIdAndInvalidAttributeKey() {
// given
Expand Down
Loading