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

package com.k_int.webapp.admin;

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;

import javax.servlet.http.HttpServletRequest;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.k_int.sql.data_dictionary.*;
import java.util.List;

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

@XmlRootElement
@ImplicitProduces("text/html;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
@Path("/admin/DataSources/{identifier}")
public class DataSource {

  @XmlTransient
  private static Log log = LogFactory.getLog(DataSource.class);

  @XmlTransient
  @Autowired
  @Qualifier(value="IdentSessionFactory")
  private SessionFactory factory = null;

  @XmlTransient
  @Context
  private UriInfo context;

  private com.k_int.sql.config.JDBCDataSource source = null;

  @XmlTransient
  @Autowired
  private com.k_int.webapp.helpers.ConfigManager config_manager = null;

  public DataSource() {
    log.debug("no args constructor");
  }

  public com.k_int.webapp.helpers.ConfigManager getConfigManager() {
    return config_manager;
  }

  /**
   * Retrieves representation of an instance of info.made4u.made4uws.workorder.Made4UWorkOrderResource
   * @param id resource URI parameter
   * @return an instance of java.lang.String
   */
  @GET
  @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XHTML_XML})
  public Viewable getXmlIdx(@PathParam("identifier") String identifier) {
    log.debug("getXml()");
    if ( identifier != null ) {
      loadSource(identifier);
    }
    return new Viewable("index",this);
  }

  @GET
  @Path("/conn")
  @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XHTML_XML})
  public Viewable getXmlConn(@PathParam("identifier") String identifier) {
    log.debug("getXml()");
    if ( identifier != null ) {
      loadSource(identifier);
    }
    return new Viewable("index",this);
  }


  @GET
  @Produces({MediaType.APPLICATION_JSON})
  public com.k_int.webapp.admin.DataSource getJson(@PathParam("identifier") String identifier) {
    log.debug("getJson");
    if ( identifier != null ) {
      loadSource(identifier);
    }
    return this;
  }

  @GET
  @Path("/conn")
  @Produces({MediaType.APPLICATION_JSON})
  public com.k_int.webapp.admin.DataSource getJsonConn(@PathParam("identifier") String identifier) {
    log.debug("getJson");
    if ( identifier != null ) {
      loadSource(identifier);
    }
    return this;
  }

  @GET
  @Path("/dictionary")
  @Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XHTML_XML})
  public Viewable getXml(@PathParam("identifier") String identifier) {
    log.debug("getXml()");
    if ( identifier != null ) {
      loadSource(identifier);
    }
    return new Viewable("dictionary",this);
  }

  
  public com.k_int.sql.config.JDBCDataSource getSource() {
    log.debug("getSource()");
    return source;
  }

  private void loadSource(String datasource_identifier) {
    log.debug("loadSource() - identifier : "+datasource_identifier);
    if ( factory != null ) {
      Session sess = null;
      try {
        sess = SessionFactoryUtils.getSession(factory,true);
        org.hibernate.Query q = sess.createQuery("select x from com.k_int.sql.config.JDBCDataSource x where x.datasourceIdentifier = ?");
        q.setString(0,datasource_identifier);
        source = (com.k_int.sql.config.JDBCDataSource) q.uniqueResult();
      } catch (HibernateException he ) {
        log.error("[firstrun:1] Problem with resource action",he);
      } catch (Exception e ) {
        log.error("[firstrun:1]Problem with resource action",e);
      } finally {
        log.debug("Loaded source: "+source);
      }
    }
    else {
      log.error("No Factory available");
    }
  }

 
}
