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

package com.k_int.processmgt;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.POST;

import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.api.view.ImplicitProduces;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlTransient;

import com.sun.jersey.spi.resource.Singleton;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;

/**
 * REST Web Service
 *
 * @author ibbo
 */

@XmlRootElement
@ImplicitProduces("text/html;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Path("/admin/processes")
@Component
public class Processes implements ApplicationContextAware {

  private ApplicationContext ctx;

  @Autowired
  @Qualifier(value="IdentSessionFactory")
  private SessionFactory factory = null;
  private static Log log = LogFactory.getLog(Processes.class);

  @XmlTransient
  @Context
  private UriInfo context;

  public Processes() {
    log.debug("New Processes Instance "+this.hashCode());
  }

  public void setApplicationContext(ApplicationContext ctx) {
    this.ctx = ctx;
    log.debug("Setting app ctx");
  }

  public void setSessionFactory(SessionFactory factory)     { 
    this.factory = factory;         
  }

  @GET
  @Produces({MediaType.TEXT_HTML})
  public Viewable getHtml() {
    log.debug("Processes.get html");
    Viewable result = new Viewable("index",this);
    return result;
  }

  @GET
  @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XHTML_XML})
  public Processes getJson() {
    log.debug("Processes.get json");
    return this;
  }

  @POST
  @Produces({MediaType.TEXT_HTML})
  public Viewable postHtml() {
    log.debug("Processes.post - html");
    Viewable result = new Viewable("index",this);
    return result;
  }

  @POST
  @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XHTML_XML})
  public CreateProcessResultDTO postJson() {
    log.debug("Processes.post - json and xml");
    return new CreateProcessResultDTO();
  }

}
