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

/* Converts Event entity (i.e. db output) into xml using JAXB*/
package TestRest.converter;

import TestRest.Event;
import java.net.URI;
import java.util.Date;
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 aparnab
 */

public class EventConverter {
    private Event entity;
    private URI uri;
    private int expandLevel;

    @XmlAttribute
    //String xmlns = "http://quakeml.org/xmlns/quakeml/1.1";
    String publicID = "smi:data.scec.org/event/";

    NetMagConverter mag = new NetMagConverter();
    
    /** Creates a new instance of EventConverter */
    public EventConverter() {
        entity = new Event();
    }

    /**
     * Creates a new instance of EventConverter.
     *
     * @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 EventConverter(Event entity, URI uri, int expandLevel, boolean isUriExtendable) {
    public EventConverter(Event entity, URI uri, int expandLevel, boolean isUriExtendable) {
        //this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getEvid() + "/").build() : uri;
        this.expandLevel = expandLevel;
    }

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

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

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

    /**
     * Getter for commid.
     *
     * @return value for commid
     */
    
    @XmlElement
    public Long getCommid() {
        return (expandLevel > 0) ? entity.getCommid() : null;
    }
    
    /**
     * Setter for commid.
     *
     * @param value the value to set
     */
    
    public void setCommid(Long value) {
        entity.setCommid(value);
    }
    
    /**
     * Getter for auth.
     *
     * @return value for auth
     */
    
    @XmlElement
    public String getAuth() {
        return (expandLevel > 0) ? entity.getAuth() : null;
    }
    
    /**
     * Setter for auth.
     *
     * @param value the value to set
     */
    
    public void setAuth(String value) {
        entity.setAuth(value);
    }
    
    /**
     * Getter for subsource.
     *
     * @return value for subsource
     */
    
    @XmlElement
    public String getSubsource() {
        return (expandLevel > 0) ? entity.getSubsource() : null;
    }
    
    /**
     * Setter for subsource.
     *
     * @param value the value to set
     */
    
    public void setSubsource(String value) {
        entity.setSubsource(value);
    }
    
    /**
     * Getter for etype.
     *
     * @return value for etype
     */
    
    @XmlElement
    public String getEtype() {
        return (expandLevel > 0) ? entity.getEtype() : null;
    }
    
    /**
     * Setter for etype.
     *
     * @param value the value to set
     */
    
    public void setEtype(String value) {
        entity.setEtype(value);
    }
    
    /**
     * Getter for selectflag.
     *
     * @return value for selectflag
     */
    
    @XmlElement
    public Short getSelectflag() {
        return (expandLevel > 0) ? entity.getSelectflag() : null;
    }
    
    /**
     * Setter for selectflag.
     *
     * @param value the value to set
     */
    
    public void setSelectflag(Short value) {
        entity.setSelectflag(value);
    }
    
    /**
     * Getter for version.
     *
     * @return value for version
     */
    
    @XmlElement
    public Long getVersion() {
        return (expandLevel > 0) ? entity.getVersion() : null;
    }
    
    /**
     * Setter for version.
     *
     * @param value the value to set
     */
    
    public void setVersion(Long value) {
        entity.setVersion(value);
    }
    
    /**
     * Getter for lddate.
     *
     * @return value for lddate
     */
    
    @XmlElement
    public Date getLddate() {
        return (expandLevel > 0) ? entity.getLddate() : null;
    }
    
    /**
     * Setter for lddate.
     *
     * @param value the value to set
     */
    
    public void setLddate(Date value) {
        entity.setLddate(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 Event entity.
     *
     * @return an entity
     */
    
    @XmlTransient
    public Event getEntity() {
        if (entity.getEvid() == null) {
            EventConverter converter = UriResolver.getInstance().resolve(EventConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }
    
    /**
     * Returns the resolved Event entity.
     *
     * @return an resolved entity
     */
    
    public Event resolveEntity(EntityManager em) {
        return entity;
    }
     
}
