nc.noumea.mairie.appock.services.impl.DemandeServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.services.impl.DemandeServiceImpl.java

Source

package nc.noumea.mairie.appock.services.impl;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.List;

import javax.mail.MessagingException;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import nc.noumea.mairie.appock.core.security.AppUser;
import nc.noumea.mairie.appock.core.services.impl.GenericServiceImpl;
import nc.noumea.mairie.appock.core.utility.AppockUtil;
import nc.noumea.mairie.appock.entity.*;
import nc.noumea.mairie.appock.enums.EtatDemande;
import nc.noumea.mairie.appock.repositories.ArticlePanierRepository;
import nc.noumea.mairie.appock.repositories.DemandeRepository;
import nc.noumea.mairie.appock.services.*;

@org.springframework.stereotype.Service("demandeService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional
public class DemandeServiceImpl extends GenericServiceImpl<Demande> implements DemandeService {

    @Autowired
    DemandeRepository demandeRepository;

    @Autowired
    ArticlePanierRepository articlePanierRepository;

    @Autowired
    AuthHelper authHelper;

    @Autowired
    CatalogueService catalogueService;

    @Autowired
    MailService mailService;

    @Autowired
    AppUserService appUserService;

    @Override
    public CrudRepository getRepository() {
        return demandeRepository;
    }

    @Override
    public Demande findOne(Long id) {
        Demande demande = demandeRepository.findOne(id);
        return initLazyDemande(demande);
    }

    @Override
    public List<Demande> findAllByCurrentService() {
        AppUser currentUser = authHelper.getCurrentUser();
        Service service = currentUser.getService();

        if (service == null) {
            return null;
        }

        return demandeRepository.findAllByServiceOrderByDateCreationDesc(service);
    }

    @Override
    public List<Demande> findAllByCurrentServiceAndEtatDemandeNotOrderByDateCreationDesc(EtatDemande etatDemande) {

        AppUser currentUser = authHelper.getCurrentUser();
        Service service = currentUser.getService();

        if (service == null) {
            return null;
        }

        List<Demande> listeDemande = demandeRepository
                .findAllByServiceAndEtatDemandeNotOrderByDateCreationDesc(service, etatDemande);
        return initLazyListeDemande(listeDemande);
    }

    @Override
    public List<Demande> findAllByEtatDemandeOrderByDateCreationDesc(EtatDemande etatDemande) {

        List<Demande> listeDemande = demandeRepository.findAllByEtatDemandeOrderByDateCreationDesc(etatDemande);
        return initLazyListeDemande(listeDemande);
    }

    private List<Demande> initLazyListeDemande(List<Demande> listeDemande) {
        listeDemande.forEach(this::initLazyDemande);
        return listeDemande;
    }

    private Demande initLazyDemande(Demande demande) {
        AppockUtil.chargeElement(demande.getService().getDirection());
        AppockUtil.chargeElement(demande.getService().getDirection().getPole());
        AppockUtil.chargeCollection(demande.getListeArticleDemande());

        return demande;
    }

    @Override
    public Demande creeDemandeFromListeArticlePanier(List<ArticlePanier> listeArticlePanier) {
        if (CollectionUtils.isEmpty(listeArticlePanier)) {
            return null;
        }
        AppUser currentUser = authHelper.getCurrentUser();

        Demande demande = new Demande();
        demande.setService(currentUser.getService());
        demande.setCreateUser(currentUser.getNomComplet());
        demande.setNumero(findNextNumero(currentUser.getService()));
        for (ArticlePanier articlePanier : listeArticlePanier) {
            ArticleDemande articleDemande = new ArticleDemande();
            articleDemande.setArticleCatalogue(articlePanier.getArticleCatalogue());
            articleDemande.setQuantite(articlePanier.getQuantite());
            articleDemande.setQuantiteCommande(articlePanier.getQuantite());
            articleDemande.setDemande(demande);
            demande.getListeArticleDemande().add(articleDemande);
        }

        return save(demande);
    }

    private String findNextNumero(Service service) {
        if (service == null) {
            return null;
        }

        Catalogue catalogue = catalogueService.findActif();

        String prefixe = service.getDirection().getPole().getLibelle() + "-"
                + service.getDirection().getLibelleCourt() + "-" + service.getLibelleCourt() + "-"
                + catalogue.getLibelle() + "-";
        Demande demande = demandeRepository.findTopByNumeroStartingWithOrderByNumeroDesc(prefixe);
        int numero = 1;
        if (demande != null) {
            numero = Integer.parseInt(demande.getNumero().substring(prefixe.length(), demande.getNumero().length()))
                    + 1;
        }
        return prefixe + StringUtils.leftPad(String.valueOf(numero), 4, "0");
    }

    @Override
    public void transmetDemande(Demande demande) throws UnsupportedEncodingException, MessagingException {
        demande.setEtatDemande(EtatDemande.EN_ATTENTE_TRAITEMENT);
        demande.setDateTransmission(LocalDateTime.now());
        AppUser currentUser = authHelper.getCurrentUser();
        demande.setTransmetLogin(currentUser.getLogin());
        demande.setTransmetUser(currentUser.getNomComplet());
        demande = save(demande);
        mailService.sendMailSuiteTransmissionDemande(demande);
    }

    @Override
    public void traiteDemande(Demande demande) throws UnsupportedEncodingException, MessagingException {
        demande = demandeRepository.findOne(demande.getId());
        demande.setEtatDemande(EtatDemande.TRAITE);
        demande.setDateTraitement(LocalDateTime.now());
        demande.setTraitementUser(authHelper.getCurrentUser().getNomComplet());
        demandeRepository.save(demande);
        mailService.sendMailSuiteTraitementDemande(demande);
    }

    @Override
    public void annuleDemande(Demande demande) throws UnsupportedEncodingException, MessagingException {
        demande = demandeRepository.findOne(demande.getId());
        demande.setEtatDemande(EtatDemande.ANNULE);
        demande.setDateTraitement(LocalDateTime.now());
        demande.setTraitementUser(authHelper.getCurrentUser().getNomComplet());
        demandeRepository.save(demande);
        mailService.sendMailSuiteAnnulationDemande(demande);
    }

    @Override
    public void repasseATraiter(List<Demande> listeDemande) {
        listeDemande.forEach(this::repasseATraiter);
    }

    private void repasseATraiter(Demande demande) {
        demande.setEtatDemande(EtatDemande.EN_ATTENTE_TRAITEMENT);
        demande.setDateTraitement(null);
        demande.setTraitementUser(null);
        demandeRepository.save(demande);
    }
}