CatalogsDecider.java

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

import com.sintia.ffl.admin.optique.catalogue.models.Catalog;
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.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Decider use to know what's the next step when processing the catalogs list.
 * <ul>
 * <li>{@see CatalogsDecider.END} : Stop the process</li>
 * <li>{@see CatalogsDecider.NEXT_VALID} : process a valid catalog</li>
 * <li>{@see CatalogsDecider.NEXT_INVALID} : process an invalid catalog</li>
 * </ul>
 * 
 * @author jumazet
 */
@Component
public class CatalogsDecider implements JobExecutionDecider {

	public static final String END = "END";
	public static final String NEXT_VALID = "NEXT_VALID";
	public static final String NEXT_INVALID = "NEXT_INVALID";

	@Override
	@NonNull
	public FlowExecutionStatus decide(@NonNull JobExecution jobExecution, StepExecution stepExecution) {

		@SuppressWarnings("unchecked")
		List<Catalog> validCatalogs = (List<Catalog>) stepExecution.getJobExecution()
				.getExecutionContext()
				.get("validCatalogs");

		if (validCatalogs == null || validCatalogs.isEmpty()) {
			// we already process all the valid catalogs, so we must now check the invalid
			// ones
			@SuppressWarnings("unchecked")
			List<Catalog> invalidCatalogs = (List<Catalog>) stepExecution.getJobExecution()
					.getExecutionContext()
					.get("invalidCatalogs");
			if (invalidCatalogs == null || invalidCatalogs.isEmpty()) {
				return new FlowExecutionStatus(END);
			} else {
				return new FlowExecutionStatus(NEXT_INVALID);
			}
		} else {
			return new FlowExecutionStatus(NEXT_VALID);
		}
	}

}