LoadCatalogArchiveFiles.java

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

import com.sintia.ffl.admin.optique.catalogue.util.FileUtil;
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;

/**
 * Archives the files used to load the catalog.<br>
 * The files are moved from 05_catalogues_a_charger to 00_archives/05_catalogues_a_charger
 *
 * @author jumazet
 */
@Component
@StepScope
public class LoadCatalogArchiveFiles implements Tasklet {

	@Autowired
	private FileUtil fileUtil;
	@Value("${local.resources.directory}")
	private String	localResourcesDirectory;
	@Value("${catalog.corrected.directory}")
	private String	toLoadDirectory;

	private String	associationsFileName;
	private String	glassesFileName;
	private String	extrasFileName;

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

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

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

		return RepeatStatus.FINISHED;
	}

}