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

package TestRest.service;

import TestRest.QuakemlView;
import TestRest.QuakemlViewNG;
import java.util.Collection;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import com.sun.jersey.api.core.ResourceContext;
import javax.persistence.EntityManager;
import TestRest.converter.QuakemlViewsConverter;

/**
 *
 * @author aparnab
 */

@Path("/quakeml/")
public class QuakemlViewsResource {
    @Context
    protected UriInfo uriInfo;
    @Context
    protected ResourceContext resourceContext;
  
    /** Creates a new instance of QuakemlViewsResource */
    public QuakemlViewsResource() {
    }

    /**
     * Get method for retrieving a collection of QuakemlView instance in XML format.
     *
     * @return an instance of QuakemlViewsConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public QuakemlViewsConverter get(@QueryParam("start")
    @DefaultValue("0")
    int start, @QueryParam("max")
    @DefaultValue("10")
    int max, @QueryParam("expandLevel")
    @DefaultValue("1")
    int expandLevel, @QueryParam("query")
    @DefaultValue("SELECT e FROM QuakemlView e where e.etype = 'le' order by e.datetime desc")
    String query) {
        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            return new QuakemlViewsConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
        } finally {
            persistenceSvc.commitTx();
            persistenceSvc.close();
        }
    }

    /**
     * Post method for creating an instance of QuakemlView using XML as the input format.
     *
     * @param data an QuakemlViewConverter entity that is deserialized from an XML stream
     * @return an instance of QuakemlViewConverter
     */
    /*
    @POST
    @Consumes({"application/xml", "application/json"})
    public Response post(QuakemlViewConverter data) {
        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            EntityManager em = persistenceSvc.getEntityManager();
            QuakemlView entity = data.resolveEntity(em);
            createEntity(data.resolveEntity(em));
            persistenceSvc.commitTx();
            return Response.created(uriInfo.getAbsolutePath().resolve(entity.getEvid() + "/")).build();
        } finally {
            persistenceSvc.close();
        }
    }
*/
    /**
     * Returns a dynamic instance of QuakemlViewResource used for entity navigation.
     *
     * @return an instance of QuakemlViewResource
     */
    @Path("{evid}/")
    public QuakemlViewResource getQuakemlViewResource(@PathParam("evid")
    Long id) {
        QuakemlViewResource resource = resourceContext.getResource(QuakemlViewResource.class);
        resource.setId(id);
        return resource;
    }

    /**
     * Returns all the entities associated with this resource.
     *
     * @return a collection of QuakemlView instances
     */
    //protected Collection<QuakemlView> getEntities(int start, int max, String query) {
    protected Collection<QuakemlViewNG> getEntities(int start, int max, String query) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
    }

    /**
     * Persist the given entity.
     *
     * @param entity the entity to persist
     */
    /*
    protected void createEntity(QuakemlView entity) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        em.persist(entity);
    }*/
}
