All checks were successful
CI - Build & Test (develop) / build-and-test (pull_request) Successful in 33s
170 lines
7.8 KiB
Java
170 lines
7.8 KiB
Java
package io.gmss.fiscad.implementations.decoupage;
|
|
|
|
import io.gmss.fiscad.entities.decoupage.Secteur;
|
|
import io.gmss.fiscad.entities.decoupage.SecteurDecoupage;
|
|
import io.gmss.fiscad.entities.infocad.parametre.Structure;
|
|
import io.gmss.fiscad.entities.user.AvoirFonction;
|
|
import io.gmss.fiscad.entities.user.User;
|
|
import io.gmss.fiscad.exceptions.BadRequestException;
|
|
import io.gmss.fiscad.exceptions.NotFoundException;
|
|
import io.gmss.fiscad.interfaces.decoupage.SecteurService;
|
|
import io.gmss.fiscad.paylaods.request.crudweb.SecteurPaylaodWeb;
|
|
import io.gmss.fiscad.paylaods.request.synchronisation.SecteurDecoupagePayload;
|
|
import io.gmss.fiscad.paylaods.request.synchronisation.SecteurPayload;
|
|
import io.gmss.fiscad.persistence.repositories.decoupage.ArrondissementRepository;
|
|
import io.gmss.fiscad.persistence.repositories.decoupage.QuartierRepository;
|
|
import io.gmss.fiscad.persistence.repositories.decoupage.SecteurRepository;
|
|
import io.gmss.fiscad.persistence.repositories.infocad.metier.ParcelleRepository;
|
|
import io.gmss.fiscad.persistence.repositories.infocad.parametre.StructureRepository;
|
|
import io.gmss.fiscad.persistence.repositories.user.AvoirFonctionRepository;
|
|
import io.gmss.fiscad.persistence.repositories.user.UserRepository;
|
|
import io.gmss.fiscad.service.EntityFromPayLoadService;
|
|
import lombok.AllArgsConstructor;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
@AllArgsConstructor
|
|
@Service
|
|
public class SecteurServiceImpl implements SecteurService {
|
|
private final SecteurRepository secteurRepository;
|
|
private final ParcelleRepository parcelleRepository;
|
|
private final UserRepository userRepository;
|
|
private final ArrondissementRepository arrondissementRepository;
|
|
private final QuartierRepository quartierRepository;
|
|
private final StructureRepository structureRepository;
|
|
private final AvoirFonctionRepository avoirFonctionRepository;
|
|
private final EntityFromPayLoadService entityFromPayLoadService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
public SecteurPaylaodWeb createSecteur(SecteurPaylaodWeb secteurPaylaodWeb) throws BadRequestException {
|
|
if (secteurPaylaodWeb.getId() != null) {
|
|
throw new BadRequestException("Impossible de créer un nouveau secteur ayant un id non null.");
|
|
}
|
|
Secteur secteur = entityFromPayLoadService.getSecteurFromPayLoadWeb(secteurPaylaodWeb);
|
|
secteur = secteurRepository.save(secteur);
|
|
return secteurRepository.findSecteurToDtoById(secteur.getId()).orElse(null);
|
|
}
|
|
|
|
// private Secteur getSecteurFromPayload(SecteurPayload secteurPayload) {
|
|
// Secteur secteur = new Secteur();
|
|
//// Optional<User> optionalUser = userRepository.findById(secteurPayload.getChefSecteurId());
|
|
// //secteur.setChefSecteur(optionalUser.orElse(null));
|
|
//// Optional<Structure> optionalStructure = structureRepository.findById(secteurPayload.getStructureId());
|
|
//// secteur.setStructure(optionalStructure.orElse(null));
|
|
//// List<SecteurDecoupage> secteurDecoupageList = new ArrayList<>();
|
|
//
|
|
//// for (SecteurDecoupagePayload sdp : secteurPayload.getSecteurDecoupages()) {
|
|
//// SecteurDecoupage sd = new SecteurDecoupage();
|
|
//// if (sdp.getSecteurId() != null && secteurRepository.existsById(sdp.getSecteurId())) {
|
|
//// sd.setSecteur(secteurRepository.findById(sdp.getSecteurId()).orElse(null));
|
|
//// }
|
|
////
|
|
//// if (sdp.getArrondissementId() != null && arrondissementRepository.existsById(sdp.getArrondissementId())) {
|
|
//// sd.setArrondissement(arrondissementRepository.findById(sdp.getArrondissementId()).orElse(null));
|
|
//// }
|
|
////
|
|
//// if (sdp.getQuartierId() != null && quartierRepository.existsById(sdp.getQuartierId())) {
|
|
//// sd.setQuartier(quartierRepository.findById(sdp.getQuartierId()).orElse(null));
|
|
//// }
|
|
//// sd.setDateDebut(sdp.getDateDebut());
|
|
//// sd.setDateFin(sdp.getDateFin());
|
|
//// sd.setId(sdp.getId());
|
|
//// secteurDecoupageList.add(sd);
|
|
//// }
|
|
// //secteur.setSecteurDecoupages(secteurDecoupageList);
|
|
// //TODO
|
|
// secteur.setId(secteurPayload.getId());
|
|
// secteur.setCode(secteurPayload.getCode());
|
|
// secteur.setNom(secteurPayload.getNom());
|
|
// return secteur;
|
|
// }
|
|
|
|
@Override
|
|
public SecteurPaylaodWeb updateSecteur(Long id, SecteurPaylaodWeb secteurPaylaodWeb) throws NotFoundException {
|
|
if (secteurPaylaodWeb.getId() == null) {
|
|
throw new BadRequestException("Impossible de mettre à jour un nouveau secteur ayant un id null.");
|
|
}
|
|
|
|
if (!secteurRepository.existsById(secteurPaylaodWeb.getId())) {
|
|
throw new NotFoundException("Impossible de trouver le secteur spécifié.");
|
|
}
|
|
Secteur secteur = entityFromPayLoadService.getSecteurFromPayLoadWeb(secteurPaylaodWeb);
|
|
secteur = secteurRepository.save(secteur);
|
|
return secteurRepository.findSecteurToDtoById(secteur.getId()).orElse(null);
|
|
}
|
|
|
|
@Override
|
|
public void deleteSecteur(Long id) throws NotFoundException {
|
|
Optional<Secteur> secteurOptional = secteurRepository.findById(id);
|
|
if (secteurOptional.isPresent()) {
|
|
secteurRepository.deleteById(secteurOptional.get().getId());
|
|
} else {
|
|
throw new NotFoundException("Impossible de trouver le secteur spécifié.");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Page<SecteurPaylaodWeb> getSecteurList(Pageable pageable) {
|
|
return secteurRepository.findAllSecteurToDtoPageable(pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<SecteurPaylaodWeb> getSecteurList() {
|
|
return secteurRepository.findAllSecteurToDto();
|
|
}
|
|
|
|
@Override
|
|
public Page<SecteurPaylaodWeb> getSecteurListBySectionId(Long sectionId, Pageable pageable) {
|
|
return secteurRepository.findAllSecteurBySectionIdToDtoPageable(sectionId,pageable);
|
|
}
|
|
|
|
@Override
|
|
public List<SecteurPaylaodWeb> getSecteurListBySectionId(Long sectionId) {
|
|
return secteurRepository.findAllSecteurBySectionIdToDto(sectionId);
|
|
}
|
|
|
|
@Override
|
|
public List<SecteurPaylaodWeb> getSecteurListByStructureId(Long structureId) {
|
|
return secteurRepository.findAllSecteurByStructureIdToDto(structureId);
|
|
}
|
|
|
|
@Override
|
|
public Page<SecteurPaylaodWeb> getSecteurListByStructureId(Long structureId, Pageable pageable) {
|
|
return secteurRepository.findAllSecteurByStructureIdToDtoPageable(structureId,pageable);
|
|
}
|
|
|
|
|
|
@Override
|
|
public Optional<SecteurPaylaodWeb> getSecteurById(Long id) {
|
|
return secteurRepository.findSecteurToDtoById(id);
|
|
}
|
|
|
|
|
|
@Override
|
|
public List<Secteur> getListSecteurUserId(Long userId) {
|
|
List<AvoirFonction> avoirFonctions= avoirFonctionRepository.findAvoirFonctionByUser_Id(userId);
|
|
List<Secteur> secteurs = new ArrayList<>();
|
|
avoirFonctions.stream()
|
|
.filter(af -> af.getDateFin() == null || af.getDateFin().isAfter(LocalDate.now()))
|
|
.forEach(avoirFonction -> {
|
|
if(avoirFonction.getFonction().getSecteur()!=null){
|
|
secteurs.addAll(List.of(avoirFonction.getFonction().getSecteur()));
|
|
}else
|
|
if (avoirFonction.getFonction().getSection()!=null){
|
|
secteurs.addAll(secteurRepository.findDistinctBySection_Id(avoirFonction.getFonction().getSection().getId()));
|
|
}else if(avoirFonction.getFonction().getStructure()!=null){
|
|
secteurs.addAll(secteurRepository.findDistinctBySection_Structure_Id(avoirFonction.getFonction().getStructure().getId()));
|
|
}
|
|
});
|
|
return secteurs;
|
|
}
|
|
} |