SupplementVerreService.java

package com.sintia.ffl.admin.optique.services.services;

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.dal.entities.catalogue.SupplementVerreCatalogue;
import com.sintia.ffl.admin.optique.dal.entities.TypeSupplement;
import com.sintia.ffl.admin.optique.dal.repositories.catalogue.DistributeurCatalogueRepository;
import com.sintia.ffl.admin.optique.dal.repositories.catalogue.FabricantCatalogueRepository;
import com.sintia.ffl.admin.optique.dal.repositories.catalogue.SupplementVerreCatalogueRepository;
import com.sintia.ffl.admin.optique.dal.repositories.TypeSupplementRepository;
import com.sintia.ffl.admin.optique.services.dto.Extras;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class SupplementVerreService {

	private final SupplementVerreCatalogueRepository supplementVerreRepository;

	private final DistributeurCatalogueRepository distributeurRepository;

	private final FabricantCatalogueRepository fabricantRepository;

	private final TypeSupplementRepository typeSupplementRepository;

	/**
	 * Return true if a SupplementVerre already exists in the database with the same
	 * cOptoCodeFabricant (maker), cOptoCodeDistributeur (provider),
	 * cSupplementVerre (extraCode)
	 *
	 * @param maker Code opto fabricant
	 * @param provider Code opto distributeur
	 * @param extraCode Code supplément
	 * @return true si un tel supplément existe, false sinon
	 */
	public boolean exist(String maker, String provider, String extraCode) {
		if (maker != null && provider != null && extraCode != null) {
			return supplementVerreRepository.findByCodeSupplementVerreAndCodeOptoDistributeurAndCodeOptoFabricant(
					extraCode, maker, provider).isPresent();
		} else {
			return false;
		}
	}

	/**
	 * Return an Extra from the database, and with the corresponding maker, provider
	 * and extraCode.<br>
	 * If there's no corresponding extra in the database, return null
	 *
	 * @param maker
	 * @param provider
	 * @param extraCode
	 * @return
	 */
	public Extras findExtraById(String maker, String provider, String extraCode) {
		Extras result = null;

		// TODO Should check if the given parameters are null or not
		Optional<SupplementVerreCatalogue> opt = supplementVerreRepository.findByCodeSupplementVerreAndCodeOptoDistributeurAndCodeOptoFabricant(
				extraCode, maker, provider);
		if (opt.isPresent()) {
			SupplementVerreCatalogue entity = opt.get();
			result = new Extras();
			result.setSeizable(Boolean.TRUE.equals(entity.getBSaisie())? '1' : '0');
			result.setExtrasCode(entity.getCodeSupplementVerre());
			result.setProvider(this.distributeurRepository.findById(entity.getIdDistributeur()).map(DistributeurCatalogue::getCOptoCodeDistributeur).orElse(null));
			result.setMaker(this.fabricantRepository.findById(entity.getIdFabricant()).map(FabricantCatalogue::getCodeOptoCodeFabricant).orElse(null));
			result.setExtrasName(entity.getLibelleSupplementVerre());
			result.setExtrasType(this.typeSupplementRepository.findById(entity.getIdTypeSupplement()).map(TypeSupplement::getCTypeSupplement).orElse(null));
			result.setReflectFreeTreatmentType(entity.getCTypeTraitAntireflet());
			result.setSpecialFabricationProcess(entity.getCProcedeSpecialFabrication());
			result.setColorType(entity.getCTypeTeinte().charAt(0));
			result.setPhotochromicLayer(Boolean.TRUE.equals(entity.getBCouchePhotochromique()) ? '1' : '0');
			result.setScratchFree(Boolean.TRUE.equals(entity.getBAntirayure()) ? '1' : '0');
			result.setFoulingFree(Boolean.TRUE.equals(entity.getBAntisalissure()) ? '1' : '0');
			result.setUvLayer(Boolean.TRUE.equals(entity.getBCoucheUv()) ? '1' : '0');

			if (null != entity.getDateCreation()) {
				result.setCreationDate(entity.getDateCreation());
			} else {
				result.setCreationDate(null);
			}
			if (null != entity.getDateMaj()) {
				result.setUpdateDate(entity.getDateMaj());
			} else {
				result.setUpdateDate(null);
			}
		}
		return result;
	}
}