Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FINCN-252 Machine Learning Scorecard for Credit Risk Assessment Phase 4 #1811

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3442,4 +3442,28 @@ public CommandWrapperBuilder updateRate(final Long rateId) {
return this;
}

public CommandWrapperBuilder createCreditScorecardFeature() {
this.actionName = "CREATE";
this.entityName = "CREDIT_SCORECARD_FEATURE";
this.entityId = null;
this.href = "/scorecard/features/template";
return this;
}

public CommandWrapperBuilder updateCreditScorecardFeature(Long featureId) {
this.actionName = "UPDATE";
this.entityName = "CREDIT_SCORECARD_FEATURE";
this.entityId = featureId;
this.href = "/scorecard/features/" + featureId;
return this;
}

public CommandWrapperBuilder deleteCreditScorecardFeature(Long featureId) {
this.actionName = "DELETE";
this.entityName = "CREDIT_SCORECARD_FEATURE";
this.entityId = featureId;
this.href = "/scorecard/features/" + featureId;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ReportingProcessServiceProvider(List<ReportingProcessService> reportingPr
mapBuilder.put(type, s);
}
LOGGER.info("Registered report service '{}' for type/s '{}'", s, reportTypes);
LOGGER.warn("Registered report service '{}' for type/s '{}'", s, reportTypes);
}
this.reportingProcessServices = mapBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
Expand All @@ -40,23 +41,23 @@
@Table(name = "m_account_transfer_transaction")
public class AccountTransferTransaction extends AbstractPersistableCustom {

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_transfer_details_id", nullable = true)
private AccountTransferDetails accountTransferDetails;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_savings_transaction_id", nullable = true)
private SavingsAccountTransaction fromSavingsTransaction;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "to_savings_transaction_id", nullable = true)
private SavingsAccountTransaction toSavingsTransaction;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "to_loan_transaction_id", nullable = true)
private LoanTransaction toLoanTransaction;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_loan_transaction_id", nullable = true)
private LoanTransaction fromLoanTransaction;

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.portfolio.creditscorecard.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Service;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Service
public @interface ScorecardService {

/**
* @return the name of the service
*/
String name();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/**
* 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.creditscorecard.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.apache.fineract.commands.domain.CommandWrapper;
import org.apache.fineract.commands.service.CommandWrapperBuilder;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.infrastructure.core.api.ApiRequestParameterHelper;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.exception.PlatformServiceUnavailableException;
import org.apache.fineract.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.creditscorecard.data.CreditScorecardFeatureData;
import org.apache.fineract.portfolio.creditscorecard.domain.CreditScorecardFeature;
import org.apache.fineract.portfolio.creditscorecard.provider.ScorecardServiceProvider;
import org.apache.fineract.portfolio.creditscorecard.service.CreditScorecardReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Path("/creditScorecard")
@Component
@Scope("singleton")
@Tag(name = "CreditScorecard", description = "")
public class CreditScorecardApiResource {

private final Set<String> featuresDataParameters = new HashSet<>(
Arrays.asList("id", "name", "valueType", "dataType", "category", "valueTypeOptions", "dataTypeOptions", "categoryOptions"));

private final String resourceNameForPermissions = "CREDIT_SCORECARD";

private final PlatformSecurityContext context;
private final ScorecardServiceProvider scorecardServiceProvider;
private final ScorecardServiceProvider serviceProvider;
private final DefaultToApiJsonSerializer<CreditScorecardFeatureData> toApiJsonSerializer;
private final ApiRequestParameterHelper apiRequestParameterHelper;
private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService;

@Autowired
public CreditScorecardApiResource(final PlatformSecurityContext context, final ScorecardServiceProvider scorecardServiceProvider,
final ScorecardServiceProvider serviceProvider,
final DefaultToApiJsonSerializer<CreditScorecardFeatureData> toApiJsonSerializer,
final ApiRequestParameterHelper apiRequestParameterHelper,
final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) {
this.context = context;
this.scorecardServiceProvider = scorecardServiceProvider;
this.serviceProvider = serviceProvider;
this.toApiJsonSerializer = toApiJsonSerializer;
this.apiRequestParameterHelper = apiRequestParameterHelper;
this.commandsSourceWritePlatformService = commandsSourceWritePlatformService;
}

@GET
@Path("features")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Scorecard Features", description = "Returns the list of defined scorecard features.\n" + "\n"
+ "Example Requests:\n" + "\n" + "features")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.GetScorecardFeatureResponse.class)))) })
public String retrieveAllScoringFeatures(@Context final UriInfo uriInfo) {

this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);

final String serviceName = "CreditScorecardReadPlatformService";
final CreditScorecardReadPlatformService scorecardService = (CreditScorecardReadPlatformService) scorecardServiceProvider
.getScorecardService(serviceName);

if (scorecardService == null) {
throw new PlatformServiceUnavailableException("err.msg.credit.scorecard.service.implementation.missing",
ScorecardServiceProvider.SERVICE_MISSING + serviceName, serviceName);
}

final Collection<CreditScorecardFeatureData> scoringFeatures = scorecardService.findAllFeaturesWithNotFoundDetection().stream()
.map(CreditScorecardFeature::toData).collect(Collectors.toList());

final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, scoringFeatures, this.featuresDataParameters);
}

@GET
@Path("features/{featureId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve a Scorecard Feature", description = "Returns the details of a defined Scorecard Feature.\n" + "\n"
+ "Example Requests:\n" + "\n" + "scorecard/features/1")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.GetScorecardFeatureResponse.class))) })
public String retrieveScoringFeature(@PathParam("featureId") @Parameter(description = "featureId") final Long featureId,
@Context final UriInfo uriInfo) {

this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);

final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());

CreditScorecardFeatureData scorecardFeature = null;
if (settings.isTemplate()) {

final String serviceName = "CreditScorecardReadPlatformService";
final CreditScorecardReadPlatformService scorecardService = (CreditScorecardReadPlatformService) scorecardServiceProvider
.getScorecardService(serviceName);

if (scorecardService == null) {
throw new PlatformServiceUnavailableException("err.msg.credit.scorecard.service.implementation.missing",
ScorecardServiceProvider.SERVICE_MISSING + serviceName, serviceName);
}

scorecardFeature = scorecardService.findOneFeatureWithNotFoundDetection(featureId).toData();
final CreditScorecardFeatureData templateData = scorecardService.retrieveNewScorecardFeatureDetails();
scorecardFeature = CreditScorecardFeatureData.withTemplate(scorecardFeature, templateData);
}

return this.toApiJsonSerializer.serialize(settings, scorecardFeature, this.featuresDataParameters);
}

@GET
@Path("features/template")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Scorecard Feature Template", description = "This is a convenience resource. It can be useful when building maintenance user interface screens for client applications. The template data returned consists of any or all of:\n"
+ "\n" + "Field Defaults\n" + "Allowed description Lists\n" + "Example Request:\n" + "\n" + "scorecard/features/template\n")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.GetScorecardFeaturesTemplateResponse.class))) })
public String retrieveNewScorecardFeatureDetails(@Context final UriInfo uriInfo) {

this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);

final String serviceName = "CreditScorecardReadPlatformService";
final CreditScorecardReadPlatformService scorecardService = (CreditScorecardReadPlatformService) scorecardServiceProvider
.getScorecardService(serviceName);

if (scorecardService == null) {
throw new PlatformServiceUnavailableException("err.msg.credit.scorecard.service.implementation.missing",
ScorecardServiceProvider.SERVICE_MISSING + serviceName, serviceName);
}

final CreditScorecardFeatureData scorecardFeature = scorecardService.retrieveNewScorecardFeatureDetails();

final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.toApiJsonSerializer.serialize(settings, scorecardFeature, this.featuresDataParameters);
}

@POST
@Path("features")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Create/Define a Scorecard Feature", description = "Define a new scorecard feature that can later be associated with loans and savings through their respective product definitions or directly on each account instance.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.PostScorecardFeatureRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.PostScorecardFeatureResponse.class))) })
public String createScoringFeature(@Parameter(hidden = true) final String apiRequestBodyAsJson) {

final CommandWrapper commandRequest = new CommandWrapperBuilder().createCreditScorecardFeature().withJson(apiRequestBodyAsJson)
.build();

final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);

return this.toApiJsonSerializer.serialize(result);
}

@PUT
@Path("features/{featureId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update a Scorecard Feature", description = "Updates the details of a Scorecard Feature.")
@RequestBody(required = true, content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.PutScorecardFeaturesFeatureIdRequest.class)))
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.PutScorecardFeaturesFeatureIdResponse.class))) })
public String updateScoringFeature(@PathParam("featureId") @Parameter(description = "featureId") final Long featureId,
@Parameter(hidden = true) final String apiRequestBodyAsJson) {

final CommandWrapper commandRequest = new CommandWrapperBuilder().updateCreditScorecardFeature(featureId)
.withJson(apiRequestBodyAsJson).build();

final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);

return this.toApiJsonSerializer.serialize(result);
}

@DELETE
@Path("features/{featureId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Delete a Scorecard Feature", description = "Deletes a Scorecard Feature.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = CreditScorecardApiResourceSwagger.DeleteScorecardFeaturesFeatureIdResponse.class))) })
public String deleteScoringFeature(@PathParam("featureId") @Parameter(description = "featureId") final Long featureId) {

final CommandWrapper commandRequest = new CommandWrapperBuilder().deleteCreditScorecardFeature(featureId).build();

final CommandProcessingResult result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest);

return this.toApiJsonSerializer.serialize(result);
}
}