package com.drumonii.loltrollbuild.batch;

import com.drumonii.loltrollbuild.riot.VersionsRetrieval;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * {@link Job} configuration for running all retrieval jobs sequentially starting with versions.
 */
@Configuration
public class AllRetrievalsJobConfig {

	@Autowired
	private JobBuilderFactory jobBuilderFactory;

	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Autowired
	private VersionsRetrieval versionsRetrieval;

	@Autowired
	@Qualifier("versionsRetrievalJob")
	private Job versionsRetrievalJob;

	@Autowired
	@Qualifier("mapsRetrievalJob")
	private Job mapsRetrievalJob;

	@Autowired
	@Qualifier("summonerSpellsRetrievalJob")
	private Job summonerSpellsRetrievalJob;

	@Autowired
	@Qualifier("championsRetrievalJob")
	private Job championsRetrievalJob;

	@Autowired
	@Qualifier("itemsRetrievalJob")
	private Job itemsRetrievalJob;

	@Bean
	public Job allRetrievalsJob() {
		return jobBuilderFactory.get("allRetrievalsJob")
				.incrementer(parameters -> {
					if (parameters == null || parameters.isEmpty()) {
						return new JobParametersBuilder()
								.addLong("run.id", 1L)
								.addString("latestRiotPatch",
										versionsRetrieval.latestVersion(versionsRetrieval.versionsFromResponse()).getPatch())
								.toJobParameters();
					}
					long id = parameters.getLong("run.id", 1L) + 1;
					return new JobParametersBuilder().addLong("run.id", id).toJobParameters();
				})
				.flow(versionsRetrievalJobStep())
				.next(mapsRetrievalJobStep())
				.next(summonerSpellsRetrievalJobStep())
				.next(championsRetrievalJobStep())
				.next(itemsRetrievalJobStep())
				.end()
				.build();
	}

	@Bean
	public Step versionsRetrievalJobStep() {
		return stepBuilderFactory.get("versionsRetrievalJobStep")
				.job(versionsRetrievalJob)
				.build();
	}

	@Bean
	public Step mapsRetrievalJobStep() {
		return stepBuilderFactory.get("mapsRetrievalJobStep")
				.job(mapsRetrievalJob)
				.build();
	}

	@Bean
	public Step summonerSpellsRetrievalJobStep() {
		return stepBuilderFactory.get("summonerSpellsRetrievalJobStep")
				.job(summonerSpellsRetrievalJob)
				.build();
	}

	@Bean
	public Step championsRetrievalJobStep() {
		return stepBuilderFactory.get("championsRetrievalJobStep")
				.job(championsRetrievalJob)
				.build();
	}

	@Bean
	public Step itemsRetrievalJobStep() {
		return stepBuilderFactory.get("itemsRetrievalJobStep")
				.job(itemsRetrievalJob)
				.build();
	}

}

@@ -646,16 +646,117 @@
 ation;%0A%0A
+import static com.drumonii.loltrollbuild.batch.scheduling.RetrievalJobsScheduling.LATEST_PATCH_KEY;%0A%0A
 /**%0A * %7B
@@ -1638,13 +1638,12 @@
 %09%09%09%09
-if (p
+JobP
 aram
@@ -1651,56 +1651,25 @@
 ters
- == null %7C%7C parameters.isEmpty()) %7B%0A%09%09%09%09%09%09return
+Builder builder =
  new
@@ -1695,322 +1695,232 @@
 er()
+;
 %0A%09%09%09%09%09
-%09%09%09.addLong(%22run.id%22, 1L)%0A%09%09%09%09%09%09%09%09.addString(%22latestRiotPatch%22,%0A%09%09%09%09%09%09%09%09%09%09versionsRetrieval.latestVersion(versionsRetrieval.versionsFromResponse()).getPatch())%0A%09%09%09%09%09%09%09%09.toJobParameters();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09long id = parameters.getLong(%22run.id%22, 1L) + 1;%0A%09%09%09%09%09return new JobParametersBuilder().addLong(%22run.id%22, id)
+if (parameters.getString(LATEST_PATCH_KEY) == null) %7B%0A%09%09%09%09%09%09builder.addString(LATEST_PATCH_KEY,%0A%09%09%09%09%09%09%09%09versionsRetrieval.latestVersion(versionsRetrieval.versionsFromResponse()).getPatch());%0A%09%09%09%09%09%7D%0A%09%09%09%09%09return builder
 .toJ
@@ -1951,12 +1951,13 @@
 %09%09%09.
-flow
+start
 (ver
@@ -2137,19 +2137,8 @@
 ())%0A
-%09%09%09%09.end()%0A
 %09%09%09%09

Simplify JobParameters generation for AllRetrievalsJob