FabricantDistributeurItemWriter.java

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

import com.sintia.ffl.admin.optique.catalogue.batch.reporter.FabricantDistributeurReporter;
import com.sintia.ffl.admin.optique.dal.entities.Promoteur;
import com.sintia.ffl.admin.optique.dal.entities.catalogue.DistributeurCatalogue;
import com.sintia.ffl.admin.optique.dal.entities.catalogue.FabricantCatalogue;
import com.sintia.ffl.admin.optique.services.dto.FabricantDistributeur;
import com.sintia.ffl.admin.optique.services.services.DistributeurFabricantAssoService;
import com.sintia.ffl.admin.optique.services.services.DistributeurPromoteurService;
import com.sintia.ffl.admin.optique.services.services.DistributeurService;
import com.sintia.ffl.admin.optique.services.services.FabricantPromoteurService;
import com.sintia.ffl.admin.optique.services.services.FabricantService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * Take in input a data in the format {@link FabricantDistributeur} and save
 * them in the database
 *
 * @author jumazet
 */
@Component
@StepScope
public class FabricantDistributeurItemWriter implements ItemWriter<FabricantDistributeur> {

	private static final Logger LOGGER = LoggerFactory.getLogger(FabricantDistributeurItemWriter.class);

	private final FabricantService fabricantService;

	private final DistributeurService distributeurService;

	private final DistributeurFabricantAssoService distributeurFabricantAssoService;
	
	private final DistributeurPromoteurService distributeurPromoteurService;
	
	private final FabricantPromoteurService fabricantPromoteurService;

	private final FabricantDistributeurReporter fabricantDistributeurReporter;

	private final Date scriptDate;

	// We pass the service thru the constructor, because otherwise we can't mock
	// them in the unit testing (some SpringBatch/Mockito issue)
	public FabricantDistributeurItemWriter(@Value("#{jobParameters[scriptDate]}") Date scriptDate,
										   @Autowired FabricantService fabricantService,
										   @Autowired FabricantDistributeurReporter fabricantDistributeurReporter,
										   @Autowired DistributeurService distributeurService,
										   @Autowired DistributeurFabricantAssoService distributeurFabricantAssoService,
										   DistributeurPromoteurService distributeurPromoteurService, FabricantPromoteurService fabricantPromoteurService) {
		this.scriptDate = scriptDate;
		this.fabricantService = fabricantService;
		this.fabricantDistributeurReporter = fabricantDistributeurReporter;
		this.distributeurService = distributeurService;
		this.distributeurFabricantAssoService = distributeurFabricantAssoService;
		this.distributeurPromoteurService = distributeurPromoteurService;
		this.fabricantPromoteurService = fabricantPromoteurService;
	}

	@Override
	public void write(List<? extends FabricantDistributeur> items) throws Exception {
		LOGGER.debug("Start write");
		if (items != null) {

			for (FabricantDistributeur fabDist : items) {
				if (fabDist.isCreateFabricant()) {
					FabricantCatalogue maker = fabricantService.addMaker(fabDist,
							LocalDateTime.ofInstant(this.scriptDate.toInstant(), ZoneId.systemDefault()));
					fabricantDistributeurReporter.addMakerCreated(maker);
				}
				if (fabDist.isCreateDistributeur()) {
					DistributeurCatalogue provider = distributeurService.addProvider(fabDist,
							LocalDateTime.ofInstant(this.scriptDate.toInstant(), ZoneId.systemDefault()));
					fabricantDistributeurReporter.addProviderCreated(provider);
				}
				if (fabDist.isCreateAssociationFabDis()) {
					FabricantCatalogue maker = fabricantService.getFabricant(fabDist.getCOptoCodeFabricant());
					DistributeurCatalogue provider = distributeurService.getDistributeur(fabDist.getCOptoCodeDistributeur());
					distributeurFabricantAssoService.addAssociation(maker, provider, LocalDateTime.ofInstant(this.scriptDate.toInstant(), ZoneId.systemDefault()));
				}
				
				if (fabDist.getPromoteurs() != null) {
					FabricantCatalogue maker = fabricantService.getFabricant(fabDist.getCOptoCodeFabricant());
					DistributeurCatalogue provider = distributeurService.getDistributeur(fabDist.getCOptoCodeDistributeur());
					if(fabDist.getPromoteurs().get("FabProm") != null){
						for(Promoteur promoteur : fabDist.getPromoteurs().get("FabProm")){
							fabricantPromoteurService.addAssociation(maker,promoteur);
						}
					}
					if(fabDist.getPromoteurs().get("DisProm") != null){
						for(Promoteur promoteur : fabDist.getPromoteurs().get("DisProm")){
							distributeurPromoteurService.addAssociation(provider,promoteur);
						}
					}
				}
			}

			LOGGER.debug("End write");
		}
	}

}