All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.entity.Partenaire Maven / Gradle / Ivy

The newest version!
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.evasion.entity;

import com.evasion.EntityJPA;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Classe représentant un partenariat entre une personne morale et le détenteur
 * du site.
 *
 * @author sebastien
 */
@Entity(name=Partenaire.ENTITY_NAME)
@NamedQueries({
    @NamedQuery(name = Partenaire.FIND_ALL_ACTIF,
    query = "SELECT p FROM "+Partenaire.ENTITY_NAME+" p WHERE "
    + "p.type <> com.evasion.entity.Partenaire.Type.aValider "
    + "OR p.dateFinInternal < CURRENT_DATE "),
    @NamedQuery(name = Partenaire.POTENTIAL_PARTENAIRE,
    query = "SELECT c FROM "+Corporation.ENTITY_NAME+" c "
    + "WHERE c.id NOT IN ("
    + "SELECT p.person.id FROM "+Partenaire.ENTITY_NAME+" p "
    + "WHERE p.type <> com.evasion.entity.Partenaire.Type.permanent "
    + "OR p.dateFinInternal < CURRENT_DATE )")})
@Table(name = Partenaire.ENTITY_NAME)
public class Partenaire extends EntityJPA {

    /**
     * *
     *
     * serialVersionUID.
     */
    private static final long serialVersionUID = 1L;
public static final String ENTITY_NAME = "PARTNER_PARTENAIRE";
    public static final String FIND_ALL_ACTIF = "FIND_ALL_ACTIF";

    public static final String POTENTIAL_PARTENAIRE = "CORPO_NOT_PARTENAIRE";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    private Corporation person;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "dateEnregistrement")
    private Calendar dateEnregistrementInternal = null;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "dateDebut")
    private Calendar dateDebutInternal = null;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "dateFin")
    private Calendar dateFinInternal = null;

    public enum Etat {

        aVenir,
        actif,
        inactif;
    }

    @Transient
    private Etat etat;

    public enum Type {

        aValider,
        temporaire,
        permanent;

        public static List listValues() {
            Type[] types = Type.values();
            return Arrays.asList(types);
        }

        ;

        private String toString;

        Type(String toString) {
            this.toString = toString;
        }

        Type() {
        }

        @Override
        public String toString() {
            return ((toString != null) ? toString : super.toString());
        }
    }

    @Enumerated(EnumType.STRING)
    private Type type;

    public Partenaire() {
    }

    public Partenaire(Date dateEnregistrement) {
        this.dateEnregistrementInternal = new GregorianCalendar();
        this.dateEnregistrementInternal.setTime(dateEnregistrement);
    }

    @PrePersist
    private void initDateEnregistrement() {
        if (dateEnregistrementInternal == null) {
            dateEnregistrementInternal = new GregorianCalendar();
        }
    }

    protected Calendar getDateDebutInternal() {
        return dateDebutInternal;
    }

    public Date getDateDebut() {
        if (dateDebutInternal == null) {
            return null;
        } else {
            return getDateDebutInternal().getTime();
        }
    }

    protected void setDateDebutInternal(Calendar dateDebut) {
        dateDebutInternal = dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        if (dateDebut == null) {
            dateDebutInternal = null;
        } else {
            dateDebutInternal = new GregorianCalendar();
            this.getDateDebutInternal().setTime(dateDebut);
        }
    }

    protected Calendar getDateFinInternal() {
        return dateFinInternal;
    }

    public Date getDateFin() {
        return getDateFinInternal().getTime();
    }

    protected void setDateFinInternal(Calendar dateFin) {
        dateFinInternal = dateFin;
    }

    public void setDateFin(Date dateFin) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(dateFin);
        this.setDateFinInternal(cal);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Corporation getPerson() {
        return person;
    }

    public void setPerson(Corporation person) {
        this.person = person;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    protected Calendar getDateEnregistrementInternal() {
        if (dateEnregistrementInternal == null) {
            this.initDateEnregistrement();
        }
        return dateEnregistrementInternal;
    }

    public Date getDateEnregistrement() {
        return getDateEnregistrementInternal().getTime();
    }

    protected void setDateEnregistrementInternal(Calendar dateEnregistrement) {
        this.dateEnregistrementInternal = dateEnregistrement;
    }

    /**
     * Définit l'état du partenaire.
     * Si son type est permanent alors il est à l'état actif;
     * Si la date de début est inférieur à la date du jour alors, l'état est à venir;
     * Si la date de fin est supérieur à la date du jour alors, l'état est innactif;
     * @return
     */
    public Etat getEtat() {
        final Date actuelle = new Date();
        if (getDateDebut() == null || actuelle.before(getDateDebut())) {
            etat = Etat.aVenir;
        } else if (actuelle.after(getDateDebut()) && (type == Type.permanent || actuelle.before(getDateFin()))) {
            etat = Etat.actif;
        } else {
            etat = Etat.inactif;
        }

        return etat;
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Partenaire)) {
            return false;
        }
        Partenaire rhs = (Partenaire) obj;
        return new EqualsBuilder().append(this.person, rhs.person).append(this.getDateEnregistrement(), rhs.getDateEnregistrement()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(this.person).append(this.getDateEnregistrement()).toHashCode();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy