| /** |
| * 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.savings.api; |
| |
| import java.util.Collection; |
| |
| import javax.ws.rs.Consumes; |
| import javax.ws.rs.GET; |
| import javax.ws.rs.POST; |
| import javax.ws.rs.Path; |
| import javax.ws.rs.PathParam; |
| import javax.ws.rs.Produces; |
| import javax.ws.rs.QueryParam; |
| import javax.ws.rs.core.Context; |
| import javax.ws.rs.core.MediaType; |
| import javax.ws.rs.core.UriInfo; |
| |
| import org.apache.commons.lang.StringUtils; |
| 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.UnrecognizedQueryParamException; |
| 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.paymenttype.data.PaymentTypeData; |
| import org.apache.fineract.portfolio.paymenttype.service.PaymentTypeReadPlatformService; |
| import org.apache.fineract.portfolio.savings.DepositAccountType; |
| import org.apache.fineract.portfolio.savings.DepositsApiConstants; |
| import org.apache.fineract.portfolio.savings.SavingsApiConstants; |
| import org.apache.fineract.portfolio.savings.data.SavingsAccountTransactionData; |
| import org.apache.fineract.portfolio.savings.service.SavingsAccountReadPlatformService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.context.annotation.Scope; |
| import org.springframework.stereotype.Component; |
| |
| @Path("/fixeddepositaccounts/{fixedDepositAccountId}/transactions") |
| @Component |
| @Scope("singleton") |
| public class FixedDepositAccountTransactionsApiResource { |
| |
| private final PlatformSecurityContext context; |
| private final DefaultToApiJsonSerializer<SavingsAccountTransactionData> toApiJsonSerializer; |
| private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; |
| private final ApiRequestParameterHelper apiRequestParameterHelper; |
| private final SavingsAccountReadPlatformService savingsAccountReadPlatformService; |
| private final PaymentTypeReadPlatformService paymentTypeReadPlatformService; |
| |
| @Autowired |
| public FixedDepositAccountTransactionsApiResource(final PlatformSecurityContext context, |
| final DefaultToApiJsonSerializer<SavingsAccountTransactionData> toApiJsonSerializer, |
| final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService, |
| final ApiRequestParameterHelper apiRequestParameterHelper, |
| final SavingsAccountReadPlatformService savingsAccountReadPlatformService, |
| PaymentTypeReadPlatformService paymentTypeReadPlatformService) { |
| this.context = context; |
| this.toApiJsonSerializer = toApiJsonSerializer; |
| this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; |
| this.apiRequestParameterHelper = apiRequestParameterHelper; |
| this.savingsAccountReadPlatformService = savingsAccountReadPlatformService; |
| this.paymentTypeReadPlatformService = paymentTypeReadPlatformService; |
| } |
| |
| private boolean is(final String commandParam, final String commandValue) { |
| return StringUtils.isNotBlank(commandParam) && commandParam.trim().equalsIgnoreCase(commandValue); |
| } |
| |
| @GET |
| @Path("template") |
| @Consumes({ MediaType.APPLICATION_JSON }) |
| @Produces({ MediaType.APPLICATION_JSON }) |
| public String retrieveTemplate(@PathParam("fixedDepositAccountId") final Long fixedDepositAccountId, |
| // @QueryParam("command") final String commandParam, |
| @Context final UriInfo uriInfo) { |
| |
| this.context.authenticatedUser().validateHasReadPermission(DepositsApiConstants.FIXED_DEPOSIT_ACCOUNT_RESOURCE_NAME); |
| |
| SavingsAccountTransactionData savingsAccount = this.savingsAccountReadPlatformService.retrieveDepositTransactionTemplate( |
| fixedDepositAccountId, DepositAccountType.FIXED_DEPOSIT); |
| final Collection<PaymentTypeData> paymentTypeOptions = this.paymentTypeReadPlatformService.retrieveAllPaymentTypes(); |
| |
| savingsAccount = SavingsAccountTransactionData.templateOnTop(savingsAccount, paymentTypeOptions); |
| |
| final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); |
| return this.toApiJsonSerializer.serialize(settings, savingsAccount, |
| SavingsApiConstants.SAVINGS_TRANSACTION_RESPONSE_DATA_PARAMETERS); |
| } |
| |
| @GET |
| @Path("{transactionId}") |
| @Consumes({ MediaType.APPLICATION_JSON }) |
| @Produces({ MediaType.APPLICATION_JSON }) |
| public String retrieveOne(@PathParam("fixedDepositAccountId") final Long fixedDepositAccountId, |
| @PathParam("transactionId") final Long transactionId, @Context final UriInfo uriInfo) { |
| |
| this.context.authenticatedUser().validateHasReadPermission(DepositsApiConstants.FIXED_DEPOSIT_ACCOUNT_RESOURCE_NAME); |
| SavingsAccountTransactionData transactionData = this.savingsAccountReadPlatformService.retrieveSavingsTransaction( |
| fixedDepositAccountId, transactionId, DepositAccountType.FIXED_DEPOSIT); |
| final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); |
| if (settings.isTemplate()) { |
| final Collection<PaymentTypeData> paymentTypeOptions = this.paymentTypeReadPlatformService.retrieveAllPaymentTypes(); |
| transactionData = SavingsAccountTransactionData.templateOnTop(transactionData, paymentTypeOptions); |
| } |
| |
| return this.toApiJsonSerializer.serialize(settings, transactionData, |
| DepositsApiConstants.FIXED_DEPOSIT_TRANSACTION_RESPONSE_DATA_PARAMETERS); |
| } |
| |
| @POST |
| @Consumes({ MediaType.APPLICATION_JSON }) |
| @Produces({ MediaType.APPLICATION_JSON }) |
| public String transaction(@PathParam("fixedDepositAccountId") final Long fixedDepositAccountId, |
| @QueryParam("command") final String commandParam, final String apiRequestBodyAsJson) { |
| |
| final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(apiRequestBodyAsJson); |
| |
| CommandProcessingResult result = null; |
| |
| if (is(commandParam, "deposit")) { |
| final CommandWrapper commandRequest = builder.fixedDepositAccountDeposit(fixedDepositAccountId).build(); |
| result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); |
| } else if (is(commandParam, "withdrawal")) { |
| final CommandWrapper commandRequest = builder.fixedDepositAccountWithdrawal(fixedDepositAccountId).build(); |
| result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); |
| } |
| |
| if (result == null) { |
| // |
| throw new UnrecognizedQueryParamException("command", commandParam, new Object[] { "deposit", "withdrawal" }); |
| } |
| |
| return this.toApiJsonSerializer.serialize(result); |
| } |
| |
| @POST |
| @Path("{transactionId}") |
| @Consumes({ MediaType.APPLICATION_JSON }) |
| @Produces({ MediaType.APPLICATION_JSON }) |
| public String adjustTransaction(@PathParam("fixedDepositAccountId") final Long fixedDepositAccountId, |
| @PathParam("transactionId") final Long transactionId, @QueryParam("command") final String commandParam, |
| final String apiRequestBodyAsJson) { |
| |
| String jsonApiRequest = apiRequestBodyAsJson; |
| if (StringUtils.isBlank(jsonApiRequest)) { |
| jsonApiRequest = "{}"; |
| } |
| |
| final CommandWrapperBuilder builder = new CommandWrapperBuilder().withJson(jsonApiRequest); |
| |
| CommandProcessingResult result = null; |
| if (is(commandParam, DepositsApiConstants.COMMAND_UNDO_TRANSACTION)) { |
| final CommandWrapper commandRequest = builder.undoSavingsAccountTransaction(fixedDepositAccountId, transactionId).build(); |
| result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); |
| } else if (is(commandParam, DepositsApiConstants.COMMAND_ADJUST_TRANSACTION)) { |
| final CommandWrapper commandRequest = builder.adjustSavingsAccountTransaction(fixedDepositAccountId, transactionId).build(); |
| result = this.commandsSourceWritePlatformService.logCommandSource(commandRequest); |
| } |
| |
| if (result == null) { |
| // |
| throw new UnrecognizedQueryParamException("command", commandParam, new Object[] { "undo", "modify" }); |
| } |
| |
| return this.toApiJsonSerializer.serialize(result); |
| } |
| } |