/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.net.URI;
import model.Personne;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;

/**
 *
 * @author michel
 */

@XmlRootElement(name = "personne")
public class PersonneConverter {
    private Personne entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of PersonneConverter */
    public PersonneConverter() {
        entity = new Personne();
    }

    /**
     * Creates a new instance of PersonneConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public PersonneConverter(Personne entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getId() + "/").build() : uri;
        this.expandLevel = expandLevel;
    }

    /**
     * Creates a new instance of PersonneConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public PersonneConverter(Personne entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for id.
     *
     * @return value for id
     */
    @XmlElement
    public Integer getId() {
        return (expandLevel > 0) ? entity.getId() : null;
    }

    /**
     * Setter for id.
     *
     * @param value the value to set
     */
    public void setId(Integer value) {
        entity.setId(value);
    }

    /**
     * Getter for nom.
     *
     * @return value for nom
     */
    @XmlElement
    public String getNom() {
        return (expandLevel > 0) ? entity.getNom() : null;
    }

    /**
     * Setter for nom.
     *
     * @param value the value to set
     */
    public void setNom(String value) {
        entity.setNom(value);
    }

    /**
     * Getter for prenom.
     *
     * @return value for prenom
     */
    @XmlElement
    public String getPrenom() {
        return (expandLevel > 0) ? entity.getPrenom() : null;
    }

    /**
     * Setter for prenom.
     *
     * @param value the value to set
     */
    public void setPrenom(String value) {
        entity.setPrenom(value);
    }

    /**
     * Getter for adresse.
     *
     * @return value for adresse
     */
    @XmlElement
    public String getAdresse() {
        return (expandLevel > 0) ? entity.getAdresse() : null;
    }

    /**
     * Setter for adresse.
     *
     * @param value the value to set
     */
    public void setAdresse(String value) {
        entity.setAdresse(value);
    }

    /**
     * Getter for numero.
     *
     * @return value for numero
     */
    @XmlElement
    public Long getNumero() {
        return (expandLevel > 0) ? entity.getNumero() : null;
    }

    /**
     * Setter for numero.
     *
     * @param value the value to set
     */
    public void setNumero(Long value) {
        entity.setNumero(value);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Personne entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Personne getEntity() {
        if (entity.getId() == null) {
            PersonneConverter converter = UriResolver.getInstance().resolve(PersonneConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Personne entity.
     *
     * @return an resolved entity
     */
    public Personne resolveEntity(EntityManager em) {
        return entity;
    }
}
