Specify query parameters for LoanTransactionsApi retrieveTransaction

This commit is contained in:
Jose Alberto Hernandez
2022-06-22 14:43:58 +02:00
committed by Arnold Gálovics
parent fb0556c20e
commit 470ff9a745
5 changed files with 126 additions and 19 deletions
@@ -0,0 +1,64 @@
/**
* 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.batch.command;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.api.MutableUriInfo;
public final class CommandStrategyUtils {
private CommandStrategyUtils() {
}
/**
* Get query parameters from relative URL.
*
* @param relativeUrl
* the relative URL
* @return the query parameters in a map
*/
public static Map<String, String> getQueryParameters(final String relativeUrl) {
final String queryParameterStr = StringUtils.substringAfter(relativeUrl, "?");
final String[] queryParametersArray = StringUtils.split(queryParameterStr, "&");
final Map<String, String> queryParametersMap = new HashMap<>();
for (String parameterStr : queryParametersArray) {
String[] keyValue = StringUtils.split(parameterStr, "=");
queryParametersMap.put(keyValue[0], keyValue[1]);
}
return queryParametersMap;
}
/**
* Add query parameters(received in the relative URL) to URI info query parameters.
*
* @param uriInfo
* the URI info
* @param queryParameters
* the query parameters
*/
public static void addQueryParametersToUriInfo(final MutableUriInfo uriInfo, final Map<String, String> queryParameters) {
for (Map.Entry<String, String> entry : queryParameters.entrySet()) {
uriInfo.addAdditionalQueryParameter(entry.getKey(), entry.getValue());
}
}
}
@@ -19,14 +19,19 @@
package org.apache.fineract.batch.command.internal;
import com.google.common.base.Splitter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.UriInfo;
import liquibase.repackaged.org.apache.commons.lang3.StringUtils;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.batch.command.CommandStrategy;
import org.apache.fineract.batch.command.CommandStrategyUtils;
import org.apache.fineract.batch.domain.BatchRequest;
import org.apache.fineract.batch.domain.BatchResponse;
import org.apache.fineract.batch.exception.ErrorHandler;
import org.apache.fineract.batch.exception.ErrorInfo;
import org.apache.fineract.infrastructure.core.api.MutableUriInfo;
import org.apache.fineract.portfolio.loanaccount.api.LoanTransactionsApiResource;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
@@ -48,6 +53,7 @@ public class GetTransactionByIdCommandStrategy implements CommandStrategy {
@Override
public BatchResponse execute(final BatchRequest request, UriInfo uriInfo) {
final MutableUriInfo parameterizedUriInfo = new MutableUriInfo(uriInfo);
final BatchResponse response = new BatchResponse();
final String responseBody;
@@ -55,16 +61,37 @@ public class GetTransactionByIdCommandStrategy implements CommandStrategy {
response.setRequestId(request.getRequestId());
response.setHeaders(request.getHeaders());
final String relativeUrl = request.getRelativeUrl();
// Get the loan and transaction ids for use in loanTransactionsApiResource
final List<String> pathParameters = Splitter.on('/').splitToList(request.getRelativeUrl());
final Long loanId = Long.parseLong(pathParameters.get(1));
final Long transactionId = Long.parseLong(pathParameters.get(3));
final List<String> pathParameters = Splitter.on('/').splitToList(relativeUrl);
Long loanId = Long.parseLong(pathParameters.get(1));
Long transactionId;
if (relativeUrl.indexOf('?') > 0) {
transactionId = Long.parseLong(StringUtils.substringBeforeLast(pathParameters.get(3), "?"));
} else {
transactionId = Long.parseLong(pathParameters.get(3));
}
Map<String, String> queryParameters = new HashMap<>();
if (relativeUrl.indexOf('?') > 0) {
queryParameters = CommandStrategyUtils.getQueryParameters(relativeUrl);
// Add the query parameters sent in the relative URL to UriInfo
CommandStrategyUtils.addQueryParametersToUriInfo(parameterizedUriInfo, queryParameters);
}
String fields = null;
if (!queryParameters.isEmpty()) {
if (queryParameters.containsKey("fields")) {
fields = queryParameters.get("fields");
}
}
// Try-catch blocks to map exceptions to appropriate status codes
try {
// Calls 'retrieveTransaction' function from 'loanTransactionsApiResource'
responseBody = loanTransactionsApiResource.retrieveTransaction(loanId, transactionId, uriInfo);
responseBody = loanTransactionsApiResource.retrieveTransaction(loanId, transactionId, fields, uriInfo);
response.setStatusCode(HttpStatus.SC_OK);
@@ -83,5 +110,4 @@ public class GetTransactionByIdCommandStrategy implements CommandStrategy {
return response;
}
}
@@ -20,6 +20,7 @@ package org.apache.fineract.portfolio.loanaccount.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
@@ -206,6 +207,7 @@ public class LoanTransactionsApiResource {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = LoanTransactionsApiResourceSwagger.GetLoansLoanIdTransactionsTransactionIdResponse.class))) })
public String retrieveTransaction(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@PathParam("transactionId") @Parameter(description = "transactionId") final Long transactionId,
@QueryParam("fields") @Parameter(in = ParameterIn.QUERY, name = "fields", description = "Optional Loan Transaction attribute list to be in the response", required = false, example = "id,date,amount") final String fields,
@Context final UriInfo uriInfo) {
this.context.authenticatedUser().validateHasReadPermission(this.resourceNameForPermissions);
@@ -20,6 +20,7 @@ package org.apache.fineract.portfolio.self.loanaccount.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -62,7 +63,6 @@ import org.springframework.stereotype.Component;
@Path("/self/loans")
@Component
@Scope("singleton")
@Tag(name = "Self Loans", description = "")
public class SelfLoansApiResource {
@@ -123,13 +123,14 @@ public class SelfLoansApiResource {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = SelfLoansApiResourceSwagger.GetSelfLoansLoanIdTransactionsTransactionIdResponse.class))) })
public String retrieveTransaction(@PathParam("loanId") @Parameter(description = "loanId") final Long loanId,
@PathParam("transactionId") @Parameter(description = "transactionId") final Long transactionId,
@QueryParam("fields") @Parameter(in = ParameterIn.QUERY, name = "fields", description = "Optional Loan Transaction attribute list to be in the response", required = false, example = "id,date,amount") final String fields,
@Context final UriInfo uriInfo) {
this.dataValidator.validateRetrieveTransaction(uriInfo);
validateAppuserLoanMapping(loanId);
return this.loanTransactionsApiResource.retrieveTransaction(loanId, transactionId, uriInfo);
return this.loanTransactionsApiResource.retrieveTransaction(loanId, transactionId, fields, uriInfo);
}
@GET