package edu.emory.cci.aiw.cvrg.eureka.etl.resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import com.google.inject.Inject;

import edu.emory.cci.aiw.cvrg.eureka.common.entity.Configuration;
import edu.emory.cci.aiw.cvrg.eureka.common.entity.Job;
import edu.emory.cci.aiw.cvrg.eureka.common.entity.JobEvent;
import edu.emory.cci.aiw.cvrg.eureka.etl.dao.ConfDao;
import edu.emory.cci.aiw.cvrg.eureka.etl.dao.JobDao;

@Path("/job")
public class RestInterfaces {

	// the methods here act as the service() method does in a servlet.
	//
	// these are the clients of the singleton ProtempaDeviceManager.
	//
	// tomcat:run

	private final JobDao jobDao;
	private final ConfDao confDao;
	private final ProtempaDeviceManager protempaDeviceManager;

	@Inject
	public RestInterfaces(JobDao jobDao, ConfDao confDao,
			ProtempaDeviceManager protempaDeviceManager) {

		this.jobDao = jobDao;
		this.confDao = confDao;
		this.protempaDeviceManager = protempaDeviceManager;
	}

	@GET
	@Path("test")
	public String getJobbbb() {

		System.out.println ("ETL:getJob");
		Job j = new Job();
		j.setJobEvents(new ArrayList<JobEvent>());
		this.protempaDeviceManager.qJob(j);
		return "right";
	}

	//
	// Job submit
	//

	@POST
	@Path("submitJob")
	@Consumes("application/json")
	@Produces("application/json")
	public Job startJob(Job job) {

		// create Job from Configuration.
		// the Job parameter has no assigned jobId yet... so persist it
		this.jobDao.save(job);
		job.setNewState("CREATED", null, null);
		this.jobDao.save(job);
		this.protempaDeviceManager.qJob(job);
		return job;
	}

	//
	// Job status
	//

	@GET
	@Path("status")
	@Produces("application/json")
	public List<Job> getJob(@QueryParam("jobId") String jobId,
			@QueryParam("userId") String userId,
			@QueryParam("status") String status,
			@QueryParam("intervalBegin") Date intervalBegin,
			@QueryParam("intervalEnd") Date intervalEnd,
			@Context HttpServletRequest req) {

		Long _jobId = null;
		Long _userId = null;

		try {

			_jobId = Long.parseLong(jobId);
		} catch (NumberFormatException ignore) {

		} catch (NullPointerException ignore) {

		}
		try {

			_userId = Long.parseLong(userId);
		} catch (NumberFormatException ignore) {

		} catch (NullPointerException ignore) {

		}

		JobFilter jobFilter = new JobFilter(_jobId, _userId, status,
				intervalBegin, intervalEnd);
		return this.jobDao.get(jobFilter);
	}

	//
	// Configuration
	//

	@GET
	@Path("getConf/{userId}")
	@Consumes("application/json")
	@Produces("application/json")
	public Configuration getConfByUserId(@PathParam("userId") String userId) {

		Configuration ret = null;
		Long _userId = null;
		try {

			_userId = Long.parseLong(userId);
		} catch (NumberFormatException nfe) {

			return null;
		} catch (NullPointerException npe) {

			return null;
		}

		ret = this.confDao.get(_userId);
		return ret;
	}

	@POST
	@Path("submitConf")
	@Consumes("application/json")
	public Response submitConfiguration(Configuration conf) {
		this.confDao.save(conf);
		return Response.ok().build();
	}
}

@@ -1373,17 +1373,16 @@
 .println
- 
 (%22ETL:ge
@@ -2037,20 +2037,18 @@
 jobId%22) 
-Stri
+Lo
 ng jobId
@@ -2066,36 +2066,34 @@
 Param(%22userId%22) 
-Stri
+Lo
 ng userId,%0A%09%09%09@Q
@@ -2270,327 +2270,8 @@
  %7B%0A%0A
-%09%09Long _jobId = null;%0A%09%09Long _userId = null;%0A%0A%09%09try %7B%0A%0A%09%09%09_jobId = Long.parseLong(jobId);%0A%09%09%7D catch (NumberFormatException ignore) %7B%0A%0A%09%09%7D catch (NullPointerException ignore) %7B%0A%0A%09%09%7D%0A%09%09try %7B%0A%0A%09%09%09_userId = Long.parseLong(userId);%0A%09%09%7D catch (NumberFormatException ignore) %7B%0A%0A%09%09%7D catch (NullPointerException ignore) %7B%0A%0A%09%09%7D%0A%0A
 %09%09Jo
@@ -2308,17 +2308,15 @@
 ter(
-_
 jobId, 
-_
 user
@@ -2586,267 +2586,504 @@
 d%22) 
-String userId) %7B%0A%0A%09%09Configuration ret = null;%0A%09%09Long _userId = null;%0A%09%09try %7B%0A%0A%09%09%09_userId = Long.parseLong(userId);%0A%09%09%7D catch (NumberFormatException nfe) %7B%0A%0A%09%09%09return null;%0A%09%09%7D catch (NullPointerException npe) %7B%0A%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09ret = this.confDao.get(_
+Long userId) %7B%0A%09%09// return this.confDao.get(userId);%0A%09%09// TODO: GET RID OF THIS FAKE CONFIGURATION WHEN REAL CONFIGURATIONS%0A%09%09// ARE AVAILABLE%0A%09%09Configuration fakeConfiguration = new Configuration();%0A%09%09fakeConfiguration.setProtempaDatabaseName(%22XE%22);%0A%09%09fakeConfiguration.setProtempaHost(%22adrastea.cci.emory.edu%22);%0A%09%09fakeConfiguration.setProtempaPort(Integer.valueOf(1521));%0A%09%09fakeConfiguration.setProtempaSchema(%22cvrg%22);%0A%09%09fakeConfiguration.setProtempaPass(%22cvrg%22);%0A%09%09fakeConfiguration.setUserId(
 user
@@ -3096,19 +3096,33 @@
 %09return 
-ret
+fakeConfiguration
 ;%0A%09%7D%0A%0A%09@

Send a fake configuration when asked, instead of trying to find one in the database.