[FINERACT-1678] business step configuration fix

This commit is contained in:
taskain7
2022-10-04 09:48:12 +02:00
committed by Aleksandar Vidakovic
parent 44cab78cbb
commit f4979d2c9b
7 changed files with 146 additions and 29 deletions
@@ -30,16 +30,15 @@ import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
@Table(name = "m_batch_business_steps")
@NoArgsConstructor
@Getter
@Setter
public class BatchBusinessStep extends AbstractPersistableCustom {
@Column(name = "job_name", nullable = false)
private String jobName;
@Setter
@Column(name = "step_name", nullable = false)
private String stepName;
@Setter
@Column(name = "step_order", nullable = false)
private Long stepOrder;
@@ -30,4 +30,6 @@ public interface BatchBusinessStepRepository extends JpaRepository<BatchBusiness
@Query("SELECT DISTINCT bbs.jobName FROM BatchBusinessStep bbs")
List<String> findConfiguredJobNames();
void deleteAllByJobName(String jobName);
}
@@ -21,6 +21,7 @@ package org.apache.fineract.cob.service;
import com.google.common.base.Splitter;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.cob.exceptions.BusinessStepException;
import org.apache.fineract.cob.exceptions.BusinessStepNotBelongsToJobException;
import org.apache.fineract.commands.annotation.CommandType;
import org.apache.fineract.commands.handler.NewCommandSourceHandler;
@@ -38,7 +39,7 @@ public class BusinessStepConfigUpdateHandler implements NewCommandSourceHandler
@Override
@Transactional
public CommandProcessingResult processCommand(JsonCommand command) throws BusinessStepNotBelongsToJobException {
public CommandProcessingResult processCommand(JsonCommand command) throws BusinessStepNotBelongsToJobException, BusinessStepException {
List<String> split = Splitter.on("/").splitToList(command.getUrl());
String jobName = split.get(split.size() - 2);
if ("null".equals(jobName)) {
@@ -29,22 +29,30 @@ import org.apache.fineract.cob.data.JobBusinessStepConfigData;
import org.apache.fineract.cob.data.JobBusinessStepDetail;
import org.apache.fineract.cob.domain.BatchBusinessStep;
import org.apache.fineract.cob.domain.BatchBusinessStepRepository;
import org.apache.fineract.cob.exceptions.BusinessStepException;
import org.apache.fineract.cob.exceptions.BusinessStepNotBelongsToJobException;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class ConfigJobParameterServiceImpl implements ConfigJobParameterService {
public class ConfigJobParameterServiceImpl implements ConfigJobParameterService, InitializingBean {
private final BatchBusinessStepRepository batchBusinessStepRepository;
private final BusinessStepConfigDataParser dataParser;
private final BusinessStepCategoryService businessStepCategoryService;
private final ApplicationContext applicationContext;
private final BusinessStepMapper mapper;
private JobBusinessStepDetail availableBusinessStepsForLoan;
@Override
public void afterPropertiesSet() throws Exception {
availableBusinessStepsForLoan = getAvailableBusinessStepsByJobName(BusinessStepCategory.LOAN.name());
}
@Override
public JobBusinessStepConfigData getBusinessStepConfigByJobName(String jobName) {
@@ -59,15 +67,25 @@ public class ConfigJobParameterServiceImpl implements ConfigJobParameterService
public CommandProcessingResult updateStepConfigByJobName(JsonCommand command, String jobName)
throws BusinessStepNotBelongsToJobException {
List<BusinessStep> businessSteps = dataParser.parseUpdate(command);
List<BatchBusinessStep> batchBusinessSteps = batchBusinessStepRepository.findAllByJobName(jobName);
businessSteps.forEach(newBusinessStepConfig -> {
BatchBusinessStep batchBusinessStep = batchBusinessSteps.stream()
.filter(oldBusinessStepConfig -> oldBusinessStepConfig.getStepName().equals(newBusinessStepConfig.getStepName()))
.findFirst().orElseThrow(BusinessStepNotBelongsToJobException::new);
batchBusinessStep.setStepName(newBusinessStepConfig.getStepName());
batchBusinessStep.setStepOrder(newBusinessStepConfig.getOrder());
batchBusinessStepRepository.save(batchBusinessStep);
});
if (businessSteps.isEmpty()) {
throw new BusinessStepException("A job needs to have 1 business step at least.");
}
List<String> availableBusinessStepNames = availableBusinessStepsForLoan.getAvailableBusinessSteps().stream()
.map(BusinessStepDetail::getStepName).toList();
List<String> notValidBusinessStepNames = businessSteps.stream().map(BusinessStep::getStepName)
.filter(businessStepName -> !availableBusinessStepNames.contains(businessStepName)).toList();
if (notValidBusinessStepNames.isEmpty()) {
batchBusinessStepRepository.deleteAllByJobName(jobName);
businessSteps.forEach(newBusinessStepConfig -> {
BatchBusinessStep batchBusinessStep = new BatchBusinessStep();
batchBusinessStep.setJobName(jobName);
batchBusinessStep.setStepName(newBusinessStepConfig.getStepName());
batchBusinessStep.setStepOrder(newBusinessStepConfig.getOrder());
batchBusinessStepRepository.save(batchBusinessStep);
});
} else {
throw new BusinessStepException(notValidBusinessStepNames + " Business steps are not configurable for this job.");
}
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).build();
}
@@ -96,5 +114,4 @@ public class ConfigJobParameterServiceImpl implements ConfigJobParameterService
public List<String> getAllConfiguredJobNames() {
return batchBusinessStepRepository.findConfiguredJobNames();
}
}
@@ -0,0 +1,45 @@
/**
* 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.infrastructure.core.exceptionmapper;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.cob.exceptions.BusinessStepException;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.springframework.stereotype.Component;
@Provider
@Component
@Slf4j
public class BusinessStepExceptionMapper implements ExceptionMapper<BusinessStepException> {
@Override
public Response toResponse(BusinessStepException exception) {
final String globalisationMessageCode = "error.msg.invalid.request.body";
final String defaultUserMessage = exception.getMessage();
log.warn("Exception: {}, Message: {}", exception.getClass().getName(), defaultUserMessage);
final ApiParameterError error = ApiParameterError.parameterError(globalisationMessageCode, defaultUserMessage, "stepName");
return Response.status(Response.Status.BAD_REQUEST).entity(error).type(MediaType.APPLICATION_JSON).build();
}
}