Java tutorial
package nc.noumea.mairie.appock.entity; /*- * #%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.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import javax.persistence.*; import javax.validation.constraints.NotNull; import org.apache.commons.collections4.CollectionUtils; import nc.noumea.mairie.appock.core.entity.AbstractEntity; import nc.noumea.mairie.appock.core.utility.AppockUtil; import nc.noumea.mairie.appock.core.utility.DateUtil; import nc.noumea.mairie.appock.enums.EtatCommande; /** * Modlise une commande d'approvisionnement avec toutes les demandes initiales et toutes les commandes par service. * * @author AgileSoft.NC */ @Entity public class Commande extends AbstractEntity { private static final long serialVersionUID = 1L; @Version private Integer version; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_commande") @SequenceGenerator(name = "s_commande", sequenceName = "s_commande", allocationSize = 1) Long id; @Column @NotNull LocalDateTime dateCreation = LocalDateTime.now(); @Column LocalDateTime datePassageCommande; @Column(length = 200) String passageUser; @Column(length = 200) String createUser; @Column LocalDateTime dateValidationCommande; @Column(length = 200) String validationUser; @Column(length = 200) @NotNull String numero; @OneToMany(cascade = CascadeType.ALL, mappedBy = "commande", orphanRemoval = true) @OrderBy("id") List<Demande> listeDemande = new ArrayList<>(); @OneToMany(cascade = CascadeType.ALL, mappedBy = "commande", orphanRemoval = true) @OrderBy("id") List<CommandeService> listeCommandeService = new ArrayList<>(); @Enumerated(EnumType.ORDINAL) @NotNull(message = "L'tat de la commande est obligatoire") private EtatCommande etatCommande = EtatCommande.EN_COURS_DE_PREPARATION; @Override public Long getId() { return id; } @Override public Integer getVersion() { return this.version; } @Override public String getLibelleCourt() { return this.numero; } /** * @return la liste des demandes qui compose la commande */ public List<Demande> getListeDemande() { return listeDemande; } public void setListeDemande(List<Demande> listeDemande) { this.listeDemande = listeDemande; } /** * @return la date de cration de la commande */ public LocalDateTime getDateCreation() { return dateCreation; } public void setDateCreation(LocalDateTime dateCreation) { this.dateCreation = dateCreation; } /** * @return la personne qui a cr la commande */ public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } /** * @return la date laquelle a t passe la commande */ public LocalDateTime getDatePassageCommande() { return datePassageCommande; } public void setDatePassageCommande(LocalDateTime datePassageCommande) { this.datePassageCommande = datePassageCommande; } public String getPassageUser() { return passageUser; } public void setPassageUser(String passageUser) { this.passageUser = passageUser; } /** * @return la date de cration au format String */ public String getDateCreationAsString() { return DateUtil.formatDateTime(dateCreation); } /** * @return la date de passage de la commande au format String */ public String getDatePassageCommandeAsString() { return DateUtil.formatDateTime(datePassageCommande); } /** * @return la date de validation de la commande au format String */ public String getDateValidationCommandeAsString() { return DateUtil.formatDateTime(dateValidationCommande); } /** * @return le numro de la demande (auto-gnr) */ public String getNumero() { return numero; } public void setNumero(String numero) { this.numero = numero; } /** * @return l'tat de la commande */ public EtatCommande getEtatCommande() { return etatCommande; } public void setEtatCommande(EtatCommande etatCommande) { this.etatCommande = etatCommande; } /** * @return la liste des diffrentes commandes unitaires par services */ public List<CommandeService> getListeCommandeService() { return listeCommandeService; } public void setListeCommandeService(List<CommandeService> listeCommandeService) { this.listeCommandeService = listeCommandeService; } /** * @return le prix total de la commande au format CFP */ public String getPrixTotalCommandeFormatCfp() { return AppockUtil.representationCfp(getPrixTotalCommande()); } public int getPrixTotalCommande() { int result = 0; for (Demande demande : listeDemande) { result += demande.getPrixTotalCommande(); } return result; } public boolean isEnCoursPreparation() { return etatCommande == EtatCommande.EN_COURS_DE_PREPARATION; } public boolean isEnAttenteReceptionCommande() { return etatCommande == EtatCommande.EN_ATTENTE_RECEPTION_COMMANDE; } /** * Supprime une demande de la commande * @param demande demande supprimer de la commande */ public void removeDemande(Demande demande) { demande.setCommande(null); listeDemande.remove(demande); } /** * @return le pourcentage d'avancement du traitement de la demande par le service achat */ public int getAvancementTraitementReception() { if (CollectionUtils.isEmpty(listeCommandeService)) { return 0; } double i = 0; for (CommandeService commandeService : listeCommandeService) { if (commandeService.isReceptionne() || commandeService.isValide()) { i++; } } return AppockUtil.arrondiDizaine(i / listeCommandeService.size()); } /** * @return le lien relatif vers l'image qui correspond au pourcentage d'avancement */ public String getAvancementTraitementReceptionIconCss() { return "/imgs/progress/" + getAvancementTraitementReception() + "_pourcent.png"; } public boolean isReceptionne() { return etatCommande == EtatCommande.RECEPTIONNE; } /** * @return la date de validatioan de la commande */ public LocalDateTime getDateValidationCommande() { return dateValidationCommande; } public void setDateValidationCommande(LocalDateTime dateValidationCommande) { this.dateValidationCommande = dateValidationCommande; } /** * @return la personne initiatrice de la validation de la commande */ public String getValidationUser() { return validationUser; } public void setValidationUser(String validationUser) { this.validationUser = validationUser; } }