LoadCatalogCheckCatalogFiles.java

package com.sintia.ffl.admin.optique.catalogue.batch.tasklet.loadcatalog;

import com.sintia.ffl.admin.optique.catalogue.batch.config.loadcatalog.LoadCatalogBatchConfiguration;
import com.sintia.ffl.admin.optique.catalogue.batch.reporter.LoadCatalogReporter;
import com.sintia.ffl.admin.optique.catalogue.batch.reporter.Reporter;
import com.sintia.ffl.admin.optique.catalogue.batch.tasklet.AbstractCheckCatalogFiles;
import com.sintia.ffl.admin.optique.services.dto.MakerProvider;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Verify that the the given catalog (ie the 3 files : glasses, extras and
 * associations) are presents in the right directory for the catalog loading job
 *
 * @author jumazet
 */
@Component
@StepScope
public class LoadCatalogCheckCatalogFiles extends AbstractCheckCatalogFiles {

	@Autowired
	private LoadCatalogReporter reporter;

	@Value("${catalog.corrected.glasses.name.pattern}")
	private String pattern;

	public LoadCatalogCheckCatalogFiles(
			@Value("#{jobParameters[associationFileName]}") String associationFileName,
			@Value("#{jobParameters[extrasFileName]}") String extrasFileName,
			@Value("#{jobParameters[glassesFileName]}") String glassesFileName,
			@Value("${local.resources.directory}") String localDirectory,
			@Value("${catalog.corrected.directory}") String specificDirectory) {
		super(associationFileName, extrasFileName, glassesFileName, localDirectory, specificDirectory);

	}

	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
			throws Exception {
		RepeatStatus status = super.execute(contribution, chunkContext);

		// Memorize the couple maker/provider associated to the catalog file
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(glassesFileName);
		if (m.matches()) {
			if (null == LoadCatalogBatchConfiguration.makersProfilers) {
				LoadCatalogBatchConfiguration.makersProfilers = new ArrayList<>();
			}
			LoadCatalogBatchConfiguration.makersProfilers.add(new MakerProvider(m.group(2), m.group(1)));
		} else {
			throw new FileNotFoundException(
					"At least one of the catalog files is incorrectly named : " + this.extrasFileName + ", "
							+ this.glassesFileName);
		}
		return status;
	}

	@Override
	public Reporter getReporter() {
		return reporter;
	}

}