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

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.services.impl.ArticleCatalogueServiceImpl.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.util.List;

import javax.mail.MessagingException;

import org.apache.commons.collections4.CollectionUtils;
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.stereotype.Service;
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.dao.ArticleCatalogueDao;
import nc.noumea.mairie.appock.entity.*;
import nc.noumea.mairie.appock.enums.EtatDemande;
import nc.noumea.mairie.appock.repositories.AppUserRepository;
import nc.noumea.mairie.appock.repositories.ArticleCatalogueRepository;
import nc.noumea.mairie.appock.repositories.PanierRepository;
import nc.noumea.mairie.appock.services.ArticleCatalogueService;
import nc.noumea.mairie.appock.services.DemandeService;
import nc.noumea.mairie.appock.services.MailService;
import nc.noumea.mairie.appock.services.PanierService;

@Service("articleCatalogueService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional
public class ArticleCatalogueServiceImpl extends GenericServiceImpl<ArticleCatalogue>
        implements ArticleCatalogueService {

    @Autowired
    ArticleCatalogueRepository articleCatalogueRepository;

    @Autowired
    ArticleCatalogueDao articleCatalogueDao;

    @Autowired
    PanierService panierService;

    @Autowired
    AppUserRepository appUserRepository;

    @Autowired
    PanierRepository panierRepository;

    @Autowired
    MailService mailService;

    @Autowired
    DemandeService demandeService;

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

    @Override
    public ArticleCatalogue findByReferenceAndCatalogue(String reference, Catalogue catalogue) {
        return articleCatalogueDao.findByReferenceAndCatalogue(reference, catalogue);
    }

    @Override
    public void gereDesactivationArticleCatalogue(ArticleCatalogue articleCatalogue)
            throws UnsupportedEncodingException, MessagingException {
        // On gre suite  la dsactivation de l'article la suppression dans les paniers des utilisateurs et un envoie de mail
        for (Panier panier : panierService.findAllWithArticleCatalogue(articleCatalogue)) {

            ArticlePanier articlePanier = panier.findArticlePanier(articleCatalogue);
            if (articlePanier != null) {
                panier.removeArticlePanier(articlePanier);
                panierRepository.save(panier);
            }

            List<AppUser> listeAppUser = appUserRepository.findAllByService(panier.getService());
            mailService.sendMailSuiteDesactivationArticleCataloguePanier(articleCatalogue, listeAppUser);
        }

        // Ainsi qu'un envoie de mail aux personnes qui l'ont dans une demande qui est  l'tat cr
        for (Demande demande : demandeService.findAllByEtatDemandeOrderByDateCreationDesc(EtatDemande.CREE)) {
            ArticleDemande articleDemande = demande.findArticleDemande(articleCatalogue);
            if (articleDemande != null) {
                List<AppUser> listeAppUser = appUserRepository.findAllByService(demande.getService());
                mailService.sendMailSuiteDesactivationArticleCatalogueDemande(articleCatalogue, demande,
                        listeAppUser);
            }
        }
    }

    @Override
    public ArticleCatalogue findArticleCataloguePlusRecentParReference(String reference, Catalogue catalogue) {
        List<ArticleCatalogue> listeArticleCatalogueAvecReference = articleCatalogueRepository
                .findAllByReference(reference);
        if (CollectionUtils.isEmpty(listeArticleCatalogueAvecReference)) {
            return null;
        }

        for (ArticleCatalogue articleCatalogue : listeArticleCatalogueAvecReference) {
            AppockUtil.chargeElement(articleCatalogue.getSousFamille());
            AppockUtil.chargeElement(articleCatalogue.getSousFamille().getFamille());

            // Si la rfrence a t trouve dans le catalogue actif c'est la plus rcente, on la renvoie
            if (articleCatalogue.getSousFamille().getFamille().getCatalogue().equals(catalogue)) {
                return articleCatalogue;
            }
        }

        // Sinon, pas d'importance, on renvoie le premier qu'on trouve, a fera l'affaire
        return listeArticleCatalogueAvecReference.get(0);
    }
}