EnrichmentArchiveFiles.java

package com.sintia.ffl.admin.optique.catalogue.batch.tasklet.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.FabricantService;
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.core.step.tasklet.Tasklet;
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.nio.file.Paths;

@Component
@StepScope
public class EnrichmentArchiveFiles implements Tasklet {

	@Autowired
	private FabricantService fabricantService;
	@Autowired
	private FileUtil fileUtil;
	@Value("${local.resources.directory}")
	private String localResourcesDirectory;
	@Value("${catalog.to.enrich.directory}")
	private String toEnrichDirectory;
	@Value("${catalog.enriched.directory}")
	private String targetDirectoryWithAgreement;
	@Value("${catalog.unlist.directory}")
	private String targetDirectoryWithoutAgreement;

	private final String associationsFileName;
	private final String glassesFileName;
	private final String extrasFileName;
	private final String cOptoCodeFabricant;

	public EnrichmentArchiveFiles(
		@Value("#{jobParameters[associationFileName]}") String associationFileName, @Value("#{jobParameters[glassesFileName]}") String glassesFileName,
		@Value("#{jobParameters[extrasFileName]}") String extrasFileName, @Autowired FileUtil fileUtil)
		throws FileNameNotMatchException {
		this.associationsFileName = associationFileName;
		this.glassesFileName = glassesFileName;
		this.extrasFileName = extrasFileName;

		this.cOptoCodeFabricant = fileUtil.extractMakerFromAssociationFileName(associationsFileName, Constants.INITAL_FILE);
	}

	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
		throws Exception {

		// Archive files to enriched
		String filePath = Paths.get(localResourcesDirectory).resolve(toEnrichDirectory).resolve(associationsFileName).toString();
		fileUtil.archiveByMove(filePath);
		filePath = Paths.get(localResourcesDirectory).resolve(toEnrichDirectory).resolve(extrasFileName).toString();
		fileUtil.archiveByMove(filePath);
		filePath = Paths.get(localResourcesDirectory).resolve(toEnrichDirectory).resolve(glassesFileName).toString();
		fileUtil.archiveByMove(filePath);

		// Archive files enriched
		String directoryToArchive = targetDirectoryWithAgreement;
		if (!fabricantService.isMakerWithAgreement(cOptoCodeFabricant)) {
			directoryToArchive = targetDirectoryWithoutAgreement;
		}

		filePath = Paths.get(localResourcesDirectory).resolve(directoryToArchive).resolve(associationsFileName).toString();
		fileUtil.archiveByMove(filePath);
		filePath = Paths.get(localResourcesDirectory).resolve(directoryToArchive).resolve(fileUtil.convertToCorrectedGlassessFileName(
			glassesFileName)).toString();
		fileUtil.archiveByMove(filePath);
		filePath = Paths.get(localResourcesDirectory).resolve(directoryToArchive).resolve(fileUtil.convertToCorrectedExtrasFileName(
			extrasFileName)).toString();
		fileUtil.archiveByMove(filePath);

		return RepeatStatus.FINISHED;
	}

}