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

package TestRest.service;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import TestRest.Assocaro;
import com.sun.jersey.api.core.ResourceContext;
import TestRest.converter.PicksConverter;
/**
 *
 * @author aparnab
 */
@Path("/picks/query/")
public class QueryPicksResource {
    @Context
    protected UriInfo uriInfo;
    @Context
    protected ResourceContext resourceContext;

    public QueryPicksResource(){
    }

    @GET
    @Produces({"application/xml", "application/json"})
    public PicksConverter get(
    @QueryParam("arid")    @DefaultValue("-1")    int arid,
    @QueryParam("orid")    @DefaultValue("-1")    int orid,
    @QueryParam("query")    @DefaultValue("") String query) {

        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            if(arid != -1){
                return new PicksConverter(getPickEntities(arid, orid, query), uriInfo.getAbsolutePath());
            } else {
                return new PicksConverter();
            }
            } finally {
            persistenceSvc.commitTx();
            persistenceSvc.close();
            }
    }
    //events for an arid
    protected Assocaro getPickEntities(int arid, int orid, String query) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        Assocaro pick = new Assocaro();
        Query p = em.createNamedQuery("Assocaro.findByAridAndOrid").setParameter("arid",arid).setParameter("orid", orid);
        pick = (Assocaro)p.getSingleResult();
        System.out.println("number of arrivals =" + p.getResultList().size());
        return pick;
    }    
}
