package com.drumonii.loltrollbuild.batch.versions;

import com.drumonii.loltrollbuild.model.Version;
import com.drumonii.loltrollbuild.repository.VersionsRepository;
import com.drumonii.loltrollbuild.riot.service.VersionsService;
import org.springframework.batch.core.Job;
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.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * {@link Job} configuration for retrieving the latest {@link Version} data from Riot's API.
 */
@Configuration
public class VersionsRetrievalJobConfig {

	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Autowired
	private DataSource dataSource;

	@Bean
	public Job versionsRetrievalJob(JobBuilderFactory jobBuilderFactory) {
		return jobBuilderFactory.get("versionsRetrievalJob")
				.incrementer(new RunIdIncrementer())
				.start(versionsRetrievalStep())
				.build();
	}

	@Bean
	public Step versionsRetrievalStep() {
		return stepBuilderFactory.get("versionsRetrievalStep")
				.<Version, Version> chunk(25)
				.reader(versionsRetrievalItemReader(null))
				.processor(versionsRetrievalItemProcessor(null))
				.writer(versionsRetrievalItemWriter())
				.build();
	}

	@StepScope
	@Bean
	public VersionsRetrievalItemReader versionsRetrievalItemReader(VersionsService versionsService) {
		return new VersionsRetrievalItemReader(versionsService.getVersions());
	}

	@StepScope
	@Bean
	public VersionsRetrievalItemProcessor versionsRetrievalItemProcessor(VersionsRepository versionsRepository) {
		return new VersionsRetrievalItemProcessor(versionsRepository.latestVersion());
	}

	@Bean
	public ItemWriter<Version> versionsRetrievalItemWriter() {
		return new JdbcBatchItemWriterBuilder<Version>()
				.dataSource(dataSource)
				.sql("INSERT INTO VERSION (PATCH, MAJOR, MINOR, REVISION) VALUES (:patch, :major, :minor, :revision)")
				.beanMapped()
				.assertUpdates(false)
				.build();
	}

}

@@ -716,12 +716,8 @@
 data
-base
 .bui
@@ -721,25 +721,26 @@
 builder.
-JdbcBatch
+Repository
 ItemWrit
@@ -931,38 +931,8 @@
 n;%0A%0A
-import javax.sql.DataSource;%0A%0A
 /**%0A
@@ -1172,29 +1172,45 @@
 ate 
-DataSource dataSource
+VersionsRepository versionsRepository
 ;%0A%0A%09
@@ -2229,17 +2229,18 @@
 new 
-JdbcBatch
+Repository
 Item
@@ -2273,180 +2273,61 @@
 %09%09%09.
-dataSource(dataSource)%0A%09%09%09%09.sql(%22INSERT INTO VERSION (PATCH, MAJOR, MINOR, REVISION) VALUES (:patch, :major, :minor, :revision)%22)%0A%09%09%09%09.beanMapped()%0A%09%09%09%09.assertUpdates(false
+repository(versionsRepository)%0A%09%09%09%09.methodName(%22save%22
 )%0A%09%09

Replace versions JdbcBatchItemWriter with a RepositoryItemWriter