Java tutorial
package nc.noumea.mairie.appock.core.security; /*- * #%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.Collection; import java.util.Collections; import java.util.List; import javax.persistence.*; import javax.validation.constraints.NotNull; import org.apache.commons.lang.StringUtils; import nc.noumea.mairie.appock.core.entity.AbstractEntity; import nc.noumea.mairie.appock.core.utility.AppockUtil; import nc.noumea.mairie.appock.util.DroitUtil; import nc.noumea.mairie.appock.core.utility.TelUtil; import nc.noumea.mairie.appock.entity.Service; import nc.noumea.mairie.appock.enums.Role; /** * Modlise un utilisateur de l'application */ @Entity // le mapping sur une table nomme "user" ne passe pas sous postgresql (mot rserv) public class AppUser extends AbstractEntity { private static final long serialVersionUID = 1L; public static final String IDENTIFIANT_TEST = "____test____"; @Version private Integer version; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_app_user") @SequenceGenerator(name = "s_app_user", sequenceName = "s_app_user", allocationSize = 1) Long id; @Override public Long getId() { return id; } @Override public Integer getVersion() { return this.version; } /** rles de l'utilisateur */ @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "app_user_liste_role", joinColumns = @JoinColumn(name = "app_user")) @Enumerated(EnumType.ORDINAL) List<Role> listeRole = new ArrayList<>(); @NotNull(message = "L'identifiant est obligatoire") @Column(length = 50, unique = true) String login; @NotNull(message = "Le nom est obligatoire") @Column(length = 100) String nom; @NotNull(message = "Le prnom est obligatoire") @Column(length = 100) String prenom; @NotNull(message = "L'email est obligatoire") @Column(length = 200) String email; @Column(length = 30) String telephone; @Column(length = 30) String poste; @ManyToOne(fetch = FetchType.LAZY) private Service service; @Column boolean actif = true; @Column boolean titulaire = true; public void setLogin(String login) { this.login = StringUtils.trimToNull(StringUtils.lowerCase(login)); } public String getLogin() { return login; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = AppockUtil.majusculeSansAccentTrim(nom); } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = AppockUtil.capitalizeFullyFrench(prenom); } public boolean isActif() { return actif; } public void setActif(boolean actif) { this.actif = actif; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = TelUtil.formatteTel(telephone); } public Service getService() { return service; } public void setService(Service service) { this.service = service; } public String getPoste() { return poste; } public void setPoste(String poste) { this.poste = poste; } public boolean isTitulaire() { return titulaire; } public void setTitulaire(boolean titulaire) { this.titulaire = titulaire; } @Override public String getLibelleCourt() { return this.getLogin(); } public String getNomComplet() { String suffixeInactif = actif ? "" : " (inactif)"; return this.prenom + " " + this.nom + suffixeInactif; } public String getNomCompletAvecRoleMax() { Role roleMax = DroitUtil.getRoleMax(this); return getNomComplet() + " (" + (roleMax != null ? roleMax.getLibelle() : "Aucun rle") + ")"; } public String getLibelleListeRole() { Collections.sort(listeRole); return StringUtils.join(listeRole, ", "); } public boolean hasRole(Role role) { return this.listeRole.contains(role); } public void clearListeRole() { listeRole.clear(); } public void addRole(Role role) { if (role == null || listeRole.contains(role)) { return; } listeRole.add(role); } public void addCollectionRole(Collection<Role> collectionRole) { if (collectionRole == null) { return; } for (Role role : collectionRole) { addRole(role); } } public List<Role> getListeRole() { return listeRole; } }