AbstractCheckCatalogFiles.java

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

import com.sintia.ffl.admin.optique.catalogue.batch.reporter.Reporter;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Verify that the the given catalog (ie the 3 files : glasses, extras and associations) are presents in the right directory
 *
 * @author jumazet
 */
public abstract class AbstractCheckCatalogFiles implements Tasklet {

	protected String	associationFileName;
	protected String	extrasFileName;
	protected String	glassesFileName;
	protected String	localResourcesDirectory;
	protected String	specificDirectory;

	protected AbstractCheckCatalogFiles(String associationFileName, String extrasFileName, String glassesFileName, String localResourcesDirectory, String specificDirectory) {
		this.associationFileName = associationFileName;
		this.extrasFileName = extrasFileName;
		this.glassesFileName = glassesFileName;
		this.localResourcesDirectory = localResourcesDirectory;
		this.specificDirectory = specificDirectory;
	}

	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
		throws Exception {
		String separator = System.getProperty("file.separator");
		String localPath = localResourcesDirectory + separator + specificDirectory;
		Path path = Paths.get(localPath);
		if (!Files.exists(path.resolve(this.associationFileName), LinkOption.NOFOLLOW_LINKS) || !Files
			.exists(path.resolve(this.extrasFileName), LinkOption.NOFOLLOW_LINKS) || !Files.exists(path.resolve(this.glassesFileName), LinkOption.NOFOLLOW_LINKS)) {
			this.getReporter().addError("Le catalogue n'est pas complet. Au moins 1 des fichiers suivants est absent : " + this.associationFileName + ", " +
				this.extrasFileName +
				", " + this.glassesFileName);
			throw new FileNotFoundException(
				"At least one of the catalog files is absent : " + this.associationFileName + ", " + this.extrasFileName + ", " + this.glassesFileName);

		}
		return RepeatStatus.FINISHED;
	}

	public abstract Reporter getReporter();

}