EnrichCatalogDecider.java

package com.sintia.ffl.admin.optique.catalogue.batch.config.enrichcatalog;

import com.sintia.ffl.admin.optique.catalogue.util.Constants;
import com.sintia.ffl.admin.optique.catalogue.util.FileNameNotMatchException;
import com.sintia.ffl.admin.optique.catalogue.util.FileUtil;
import com.sintia.ffl.admin.optique.services.services.DistributeurService;
import com.sintia.ffl.admin.optique.services.services.FabricantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Check that all the job parameters are presents.
 * <ul>
 * <li>{@see Constants.INCOMPLETE_JOB_PARAMETERS} : at leas one of the 3 files composing a catalog is missing</li>
 * <li>{@see Constants.BAD_MAKER_PROVIDER} : the maker and/or the provider associated to the files does not exist in the database</li>
 * <li>{@see Constants.INVALID_JOB_PARAMETERS} : error processing the parameter</li>
 * <li>{@see Constants.NEXT} : exerything is OK, the process can go on</li>
 * </ul>
 *
 * @author jumazet
 */
@Component
public class EnrichCatalogDecider implements JobExecutionDecider {

	private static final Logger LOGGER = LoggerFactory.getLogger(EnrichCatalogDecider.class);

	@Autowired
	private FileUtil fileUtil;

	@Autowired
	private FabricantService fabricantService;

	@Autowired
	private DistributeurService distributeurService;

	@Override
	public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
		if (jobExecution.getJobParameters().getString("associationFileName") == null ||
			jobExecution.getJobParameters().getString("glassesFileName") == null ||
			jobExecution.getJobParameters().getString("extrasFileName") == null) {
			return new FlowExecutionStatus(Constants.INCOMPLETE_JOB_PARAMETERS);
		} else {
			String associationsFileName = jobExecution.getJobParameters().getString("associationFileName");
			try {
				String maker = fileUtil.extractMakerFromAssociationFileName(associationsFileName, Constants.INITAL_FILE);
				String provider = fileUtil.extractProviderFromAssociationFileName(associationsFileName, Constants.INITAL_FILE);
				if (fabricantService.exist(maker) && distributeurService.exist(provider)) {
					return new FlowExecutionStatus(Constants.NEXT);
				} else {
					LOGGER.error("Maker {} and Provider {} not valid ", maker, provider);
					return new FlowExecutionStatus(Constants.BAD_MAKER_PROVIDER);
				}
			}
			catch (FileNameNotMatchException e) {
				LOGGER.error("The file name {} does not match the expected pattern", associationsFileName, e);
				return new FlowExecutionStatus(Constants.INVALID_JOB_PARAMETERS);
			}
		}
	}

}