nc.noumea.mairie.appock.entity.Adresse.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.entity.Adresse.java

Source

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.util.ArrayList;
import java.util.List;

import javax.persistence.*;

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.core.utility.MessageErreur;

/**
 * Modlise une adresse du rfrentiel.
 * 
 * @author AgileSoft.NC
 */
@Entity
public class Adresse extends AbstractEntity {
    private static final long serialVersionUID = 1L;

    @Version
    private Integer version;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_adresse")
    @SequenceGenerator(name = "s_adresse", sequenceName = "s_adresse", allocationSize = 1)
    Long id;

    @Column
    Integer numVoie;

    @Column(length = 10)
    String complementNumVoie;

    @Column(length = 100)
    String voie;

    @Column(length = 100)
    String pointRemise;

    @Column(length = 100)
    String complement;

    @Column(length = 100)
    String lotissement;

    @Column(length = 50)
    String quartier;

    @Column(length = 20)
    String bp;

    @Column(length = 10)
    String cp;

    @Column(length = 80)
    String ville;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public Integer getVersion() {
        return this.version;
    }

    /**
     * @return numro de voie
     */
    public Integer getNumVoie() {
        return numVoie;
    }

    public void setNumVoie(Integer numVoie) {
        this.numVoie = numVoie;
    }

    /**
     * @return complment du numro de voie, ex : "BIS", "TER", etc.
     */
    public String getComplementNumVoie() {
        return complementNumVoie;
    }

    public void setComplementNumVoie(String complementNumVoie) {
        this.complementNumVoie = AppockUtil.majusculeSansAccentTrim(complementNumVoie);
    }

    /**
     * @return la voie, ex : "RUE GABRIEL LAROQUE", sans le numro en prfixe
     */
    public String getVoie() {
        return voie;
    }

    public void setVoie(String voie) {
        this.voie = AppockUtil.majusculeSansAccentTrim(voie);
    }

    /**
     * @return des informations sur le point de remise (Appartement, tage, couloir, etc.)
     */
    public String getPointRemise() {
        return pointRemise;
    }

    public void setPointRemise(String pointRemise) {
        this.pointRemise = AppockUtil.majusculeSansAccentTrim(pointRemise);
    }

    /**
     * @return informations complmentaires (btiment, rsidence, etc.)
     */
    public String getComplement() {
        return complement;
    }

    public void setComplement(String complement) {
        this.complement = AppockUtil.majusculeSansAccentTrim(complement);
    }

    /**
     * @return le libell du lotissement
     */
    public String getLotissement() {
        return lotissement;
    }

    public void setLotissement(String lotissement) {
        this.lotissement = AppockUtil.majusculeSansAccentTrim(lotissement);
    }

    /**
     * @return le libell du quartier
     */
    public String getQuartier() {
        return quartier;
    }

    public void setQuartier(String quartier) {
        this.quartier = AppockUtil.majusculeSansAccentTrim(quartier);
    }

    /**
     * @return la boite postale (sans le prfixe BP)
     */
    public String getBp() {
        return bp;
    }

    private String getBpAvecPrefixeBp() {
        return StringUtils.isBlank(bp) ? null : "BP " + bp;
    }

    public void setBp(String bp) {
        this.bp = AppockUtil.majusculeSansAccentTrim(bp);
        if (this.bp.startsWith("BP")) {
            this.bp = StringUtils.trimToEmpty(this.bp.substring(2));
        }
    }

    /**
     * @return le code postal
     */
    public String getCp() {
        return cp;
    }

    public void setCp(String cp) {
        this.cp = AppockUtil.majusculeSansAccentTrim(cp);
    }

    /**
     * @return la ville
     */
    public String getVille() {
        return ville;
    }

    public void setVille(String ville) {
        this.ville = AppockUtil.majusculeSansAccentTrim(ville);
    }

    /**
     * @return le libell sur une ligne, sans retour  la ligne, de l'adresse
     */
    @Override
    public String getLibelleCourt() {
        List<String> listeChampRetenu = new ArrayList<>();
        if (numVoie != null) {
            listeChampRetenu.add(String.valueOf(numVoie));
        }
        for (String champ : new String[] { complementNumVoie, voie, pointRemise, complement, lotissement, quartier,
                getBpAvecPrefixeBp(), cp, ville }) {
            if (!StringUtils.isBlank(champ)) {
                listeChampRetenu.add(StringUtils.trimToEmpty(champ));
            }
        }
        return StringUtils.join(listeChampRetenu, " ");
    }

    public String getReprListeLigneGeneric(String separateur) {
        List<String> listeLigne = new ArrayList<>();
        listeLigne.add(AppockUtil.creerReprLigne(numVoie != null ? "" + numVoie : "", complementNumVoie, voie));
        if (!StringUtils.isBlank(lotissement)) {
            String lotissementUppercase = AppockUtil.majusculeSansAccentTrim(lotissement);
            String prefixeLotissement = "";
            if (!lotissementUppercase.startsWith("LOTISSEMENT")
                    && !lotissementUppercase.startsWith("MORCELLEMENT")) {
                prefixeLotissement = "Lotissement";
            }
            listeLigne.add(
                    AppockUtil.creerReprLigne(prefixeLotissement, AppockUtil.capitalizeFullyFrench(lotissement)));
        }
        listeLigne.add(AppockUtil.creerReprLigne(pointRemise));
        listeLigne.add(AppockUtil.creerReprLigne(complement));
        listeLigne.add(AppockUtil.creerReprLigne(quartier));
        if (!StringUtils.isBlank(bp)) {
            listeLigne.add(getBpAvecPrefixeBp());
        }
        listeLigne.add(AppockUtil.creerReprLigne(cp, ville));

        return construitReprMultiLigneNonBlanche(listeLigne, separateur);
    }

    /**
     * @return une reprsentation mono-ligne
     */
    public String getReprMonoLigne() {
        return getReprListeLigneGeneric(" - ");
    }

    /**
     * @return une reprsentation multi-ligne, standard et complte, pour affichage dans les ditiques (hors arrts, qui ont un affichage particulier)
     */
    public String getReprMultiLigne() {
        return getReprListeLigneGeneric("\n");
    }

    public boolean isVide() {
        return StringUtils.isBlank(getReprMultiLigne());
    }

    private String construitReprMultiLigneNonBlanche(List<String> listeLigne, String separateur) {
        List<String> listeLigneRetenu = new ArrayList<>();
        for (String ligne : listeLigne) {
            if (!StringUtils.isBlank(ligne)) {
                listeLigneRetenu.add(StringUtils.trimToEmpty(ligne));
            }
        }
        return StringUtils.join(listeLigneRetenu, separateur);
    }

    @Override
    public List<MessageErreur> construitListeMessageErreur() {
        List<MessageErreur> result = super.construitListeMessageErreur();
        if (StringUtils.isBlank(ville)) {
            result.add(new MessageErreur("Adresse incomplte"));
        }
        return result;
    }
}