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

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.services.impl.CommandeServiceServiceImpl.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.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.mail.MessagingException;

import org.apache.commons.io.FileUtils;
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.comparator.CommandeServiceComparator;
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.BonLivraison;
import nc.noumea.mairie.appock.entity.CommandeService;
import nc.noumea.mairie.appock.entity.Demande;
import nc.noumea.mairie.appock.enums.EtatCommandeService;
import nc.noumea.mairie.appock.enums.EtatDemande;
import nc.noumea.mairie.appock.repositories.*;
import nc.noumea.mairie.appock.services.AuthHelper;
import nc.noumea.mairie.appock.services.CommandeServiceService;
import nc.noumea.mairie.appock.services.ConfigService;
import nc.noumea.mairie.appock.services.MailService;

@org.springframework.stereotype.Service("commandeServiceService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional
public class CommandeServiceServiceImpl extends GenericServiceImpl<CommandeService>
        implements CommandeServiceService {

    @Autowired
    CommandeServiceRepository commandeServiceRepository;

    @Autowired
    AuthHelper authHelper;

    @Autowired
    CommandeRepository commandeRepository;

    @Autowired
    MimeTypeRepository mimeTypeRepository;

    @Autowired
    BonLivraisonRepository bonLivraisonRepository;

    @Autowired
    ConfigService configService;

    @Autowired
    MailService mailService;

    @Autowired
    DemandeRepository demandeRepository;

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

    @Override
    public CommandeService findOne(Long id) {
        CommandeService commandeService = commandeServiceRepository.findOne(id);

        AppockUtil.chargeElement(commandeService.getCommande());
        AppockUtil.chargeCollection(commandeService.getCommande().getListeCommandeService());
        AppockUtil.chargeCollection(commandeService.getListeArticleCommande());

        return commandeService;
    }

    @Override
    public List<CommandeService> findAllForCurrentService(List<EtatCommandeService> listeEtatCommandeService) {
        AppUser currentUser = authHelper.getCurrentUser();

        if (currentUser.getService() == null) {
            return null;
        }

        List<CommandeService> listeCommandeService = commandeServiceRepository
                .findAllByServiceAndEtatCommandeServiceIn(currentUser.getService(), listeEtatCommandeService);
        for (CommandeService commandeService : listeCommandeService) {
            AppockUtil.chargeElement(commandeService.getCommande());
            AppockUtil.chargeCollection(commandeService.getListeArticleCommande());
        }

        Collections.sort(listeCommandeService, new CommandeServiceComparator());
        return listeCommandeService;
    }

    @Override
    public int countNombreEnAttenteReceptionForCurrentService() {
        AppUser currentUser = authHelper.getCurrentUser();

        if (currentUser.getService() == null) {
            return 0;
        }

        List<EtatCommandeService> listeEtatCommandeService = new ArrayList<>();
        listeEtatCommandeService.add(EtatCommandeService.EN_ATTENTE_RECEPTION_COMMANDE);
        return commandeServiceRepository
                .findAllByServiceAndEtatCommandeServiceIn(currentUser.getService(), listeEtatCommandeService)
                .size();
    }

    @Override
    public void receptionneCommandeService(CommandeService commandeService)
            throws UnsupportedEncodingException, MessagingException {
        AppUser currentUser = authHelper.getCurrentUser();
        commandeService = commandeServiceRepository.findOne(commandeService.getId());
        commandeService.setEtatCommandeService(EtatCommandeService.RECEPTIONNE);
        commandeService.setDatePassageReception(LocalDateTime.now());
        commandeService.setReceptionUser(currentUser.getNomComplet());
        commandeService.setReceptionLogin(currentUser.getLogin());
        commandeService = commandeServiceRepository.save(commandeService);

        for (Demande demande : commandeService.getCommande().getListeDemande()) {
            demande.setEtatDemande(EtatDemande.TERMINE);
            demandeRepository.save(demande);
        }

        mailService.sendMailSuiteReceptionCommandeService(commandeService);
    }

    @Override
    public BonLivraison saveBonLivraison(byte[] content, String nomFichier) throws IOException {
        BonLivraison bonLivraison = new BonLivraison();
        bonLivraison.setNomFichier(nomFichier);
        bonLivraison.setContenu(content);
        bonLivraison.setMimeType(mimeTypeRepository.findFirstByLibelle(AppockUtil.getMimeType(content)));

        bonLivraison = bonLivraisonRepository.save(bonLivraison);

        creeFichierCoteServeur(bonLivraison);

        return bonLivraison;
    }

    private void creeFichierCoteServeur(BonLivraison bonLivraison) throws IOException {
        if (bonLivraison.getContenu() == null) {
            return;
        }

        FileUtils.writeByteArrayToFile(getFileBonLivraison(bonLivraison), bonLivraison.getContenu());
    }

    @Override
    public File getFileBonLivraison(BonLivraison bonLivraison) {
        if (bonLivraison == null || bonLivraison.getId() == null) {
            throw new IllegalArgumentException("bonLivraison null ou sans id : " + bonLivraison);
        }
        return new File(
                configService.getPieceJointeDir() + "/bonlivraison" + File.separatorChar + bonLivraison.getId());
    }
}