Java tutorial
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.ArrayList; import java.util.List; import javax.mail.MessagingException; 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.TypeMouvementStock; import nc.noumea.mairie.appock.repositories.ArticleStockRepository; import nc.noumea.mairie.appock.repositories.StockRepository; import nc.noumea.mairie.appock.services.AuthHelper; import nc.noumea.mairie.appock.services.MailService; import nc.noumea.mairie.appock.services.ServiceService; import nc.noumea.mairie.appock.services.StockService; @org.springframework.stereotype.Service("stockService") @Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS) @Transactional public class StockServiceImpl extends GenericServiceImpl<Stock> implements StockService { @Autowired StockRepository stockRepository; @Autowired AuthHelper authHelper; @Autowired ArticleStockRepository articleStockRepository; @Autowired ServiceService serviceService; @Autowired MailService mailService; @Override public CrudRepository getRepository() { return stockRepository; } @Override public Stock findOne(Long id) { Stock result = super.findOne(id); AppockUtil.chargeCollection(result.getListeArticleStock()); AppockUtil.chargeCollection(result.getListeMouvementStock()); return result; } @Override public void creeEntreeSortieEtMouvement(ArticleStock articleStock, Integer quantiteRenseigne, TypeMouvementStock typeMouvementStock, String observation) { Integer oldQuantite = articleStock.getQuantiteStock(); String reference = articleStock.getReferenceArticleStock(); Stock stock = articleStock.getStock(); Integer quantiteActuelle = articleStock.getQuantiteStock(); Integer nouvelleQuantite = typeMouvementStock == TypeMouvementStock.SORTIE ? (quantiteActuelle - quantiteRenseigne) : (quantiteActuelle + quantiteRenseigne); if (nouvelleQuantite.equals(0)) { stock.removeArticleStock(articleStock); } else { articleStock.setQuantiteStock(nouvelleQuantite); articleStockRepository.save(articleStock); } AppUser currentUser = authHelper.getCurrentUser(); MouvementStock mouvementStock = new MouvementStock(); mouvementStock.setDateMouvement(LocalDateTime.now()); mouvementStock.setMouvementUser(currentUser.getNomComplet()); mouvementStock.setQuantiteRenseigne(quantiteRenseigne); mouvementStock.setOldQuantiteStock(oldQuantite); mouvementStock.setNewQuantiteStock(nouvelleQuantite); mouvementStock.setObservation(observation); mouvementStock.setReference(reference); mouvementStock.setTypeMouvementStock(typeMouvementStock); mouvementStock.setStock(stock); stock.getListeMouvementStock().add(mouvementStock); stockRepository.save(stock); } @Override public ArticleStock creeArticleStockEtMouvement(ArticleCatalogue articleCatalogue, Integer quantiteRenseigne, Stock stock, String observation) { ArticleStock articleStock = new ArticleStock(); articleStock.setQuantiteStock(quantiteRenseigne); articleStock.setReferenceArticleStock(articleCatalogue.getReference()); articleStock.setStock(stock); stock.getListeArticleStock().add(articleStock); AppUser currentUser = authHelper.getCurrentUser(); MouvementStock mouvementStock = new MouvementStock(); mouvementStock.setDateMouvement(LocalDateTime.now()); mouvementStock.setQuantiteRenseigne(quantiteRenseigne); mouvementStock.setMouvementUser(currentUser.getNomComplet()); mouvementStock.setReference(articleCatalogue.getReference()); mouvementStock.setNewQuantiteStock(quantiteRenseigne); mouvementStock.setTypeMouvementStock(TypeMouvementStock.NOUVEL_ARTICLE); mouvementStock.setObservation(observation); mouvementStock.setStock(stock); stock.getListeMouvementStock().add(mouvementStock); stockRepository.save(stock); return articleStock; } @Override public void majStockSuiteValidationReceptionCommande(CommandeService commandeService) throws UnsupportedEncodingException, MessagingException { Service service = serviceService.findOneAndChargeElementStock(commandeService.getService().getId()); Stock stock = service.getStock(); if (stock == null) { service.setStock(new Stock()); serviceService.save(service); stock = findOne(service.getStock().getId()); } List<Object[]> listeNouveauArticleStockAvecQuantite = new ArrayList<>(); List<Object[]> listeExistantArticleStockAvecQuantite = new ArrayList<>(); for (ArticleCommande articleCommande : commandeService.getListeArticleCommande()) { boolean articleDejaPresentDansStock = false; Object[] articleEtQuantite = new Object[2]; String observation = "Suite rception commande " + commandeService.getCommande().getNumero(); for (ArticleStock articleStock : stock.getListeArticleStock()) { if (articleCommande.getArticleCatalogue().getReference() .equals(articleStock.getReferenceArticleStock())) { Integer quantite = articleCommande.getQuantiteRecu() * articleCommande.getArticleCatalogue().getQuantiteColisage(); creeEntreeSortieEtMouvement(articleStock, quantite, TypeMouvementStock.ENTREE, observation); articleEtQuantite[0] = articleStock; articleEtQuantite[1] = quantite; listeExistantArticleStockAvecQuantite.add(articleEtQuantite); articleDejaPresentDansStock = true; break; } } if (!articleDejaPresentDansStock && articleCommande.getQuantiteRecu() > 0) { Integer quantite = articleCommande.getQuantiteRecu() * articleCommande.getArticleCatalogue().getQuantiteColisage(); ArticleStock articleStock = creeArticleStockEtMouvement(articleCommande.getArticleCatalogue(), quantite, stock, observation); articleEtQuantite[0] = articleStock; articleEtQuantite[1] = quantite; listeNouveauArticleStockAvecQuantite.add(articleEtQuantite); } } mailService.sendMailSuiteMiseAJourStock(listeNouveauArticleStockAvecQuantite, listeExistantArticleStockAvecQuantite, commandeService); } @Override public void initArticleQuantiteStockPourService(Article article, Service service) { service = serviceService.findOneAndChargeElementStock(service.getId()); Stock stock = service.getStock(); if (stock == null) { return; } for (ArticleStock articleStock : stock.getListeArticleStock()) { if (article.getArticleCatalogue().getReference().equals(articleStock.getReferenceArticleStock())) { article.setQuantiteStock(articleStock.getQuantiteStock()); break; } } } }