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.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.dao.PanierDao; import nc.noumea.mairie.appock.entity.ArticleCatalogue; import nc.noumea.mairie.appock.entity.ArticlePanier; import nc.noumea.mairie.appock.entity.Panier; import nc.noumea.mairie.appock.entity.Service; import nc.noumea.mairie.appock.repositories.PanierRepository; import nc.noumea.mairie.appock.services.AuthHelper; import nc.noumea.mairie.appock.services.PanierService; @org.springframework.stereotype.Service("panierService") @Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS) @Transactional public class PanierServiceImpl extends GenericServiceImpl<Panier> implements PanierService { private static Logger log = LoggerFactory.getLogger(PanierServiceImpl.class); @Autowired PanierRepository panierRepository; @Autowired PanierDao panierDao; @Autowired AuthHelper authHelper; @Override public CrudRepository getRepository() { return panierRepository; } @Override public Panier findOne(Long id) { Panier panier = panierRepository.findOne(id); AppockUtil.chargeCollection(panier.getListeArticlePanier()); return panier; } @Override public Panier findOrCreateByCurrentService() { AppUser currentUser = authHelper.getCurrentUser(); Service service = currentUser.getService(); if (service == null) { log.error("Service de l'utilisateur connect non trouv !"); return null; } Panier panier = panierRepository.findFirstByService(service); if (panier != null) { AppockUtil.chargeCollection(panier.getListeArticlePanier()); return panier; } else { return createPanierForCurrentService(); } } @Override public Panier createPanierForCurrentService() { AppUser currentUser = authHelper.getCurrentUser(); Panier panier = new Panier(); Service service = currentUser.getService(); if (service == null) { log.error("Service de l'utilisateur connect non trouv !"); return null; } panier.setService(service); return panierRepository.save(panier); } @Override public int countNombreArticleDansPanierForCurrentService() { AppUser currentUser = authHelper.getCurrentUser(); if (currentUser.getService() == null) { log.error("Service de l'utilisateur connect non trouv !"); return 0; } Panier panier = findOrCreateByCurrentService(); return panier.getListeArticlePanier().size(); } @Override public Panier ajoutArticleCatalogue(ArticleCatalogue articleCatalogue) { Panier panier = findOrCreateByCurrentService(); boolean articleExisteDeja = false; for (ArticlePanier articlePanier : panier.getListeArticlePanier()) { if (articlePanier.getArticleCatalogue().equals(articleCatalogue)) { articlePanier.setQuantite(articlePanier.getQuantite() + 1); articleExisteDeja = true; break; } } if (!articleExisteDeja) { ArticlePanier articlePanier = new ArticlePanier(); articlePanier.setPanier(panier); articlePanier.setArticleCatalogue(articleCatalogue); panier.getListeArticlePanier().add(articlePanier); } return panierRepository.save(panier); } @Override public Panier videPanier(Panier panier) { // On passe par une liste intrmdiaire pour ne pas avoir de ConcurrentModificationException List<ArticlePanier> listeArticlePanier = new ArrayList<>(); listeArticlePanier.addAll(panier.getListeArticlePanier()); listeArticlePanier.forEach(panier::removeArticlePanier); panierRepository.save(panier); return panier; } @Override public void videTousPanier() { for (Panier panier : panierRepository.findAll()) { this.videPanier(panier); } } @Override public List<Panier> findAllWithArticleCatalogue(ArticleCatalogue articleCatalogue) { return panierDao.findAllWithArticleCatalogue(articleCatalogue); } }