package main.java.recoin.mongodb_version.rest;

import java.io.InputStream;
import java.util.ArrayList;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
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.Response;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONObject;

import main.java.sociam.pybossa.rest.sendTaskRun;
import sociam.pybossa.config.Config;
import sociam.pybossa.methods.MongodbMethods;

/**
 * 
 * @author user Saud Aljaloud
 * @author email sza1g10@ecs.soton.ac.uk
 *
 */
@Path("/TaskRun")
public class TaskRun {

	private static final Logger logger = Logger.getLogger(TaskRun.class);

	@GET
	@Produces("application/json" + ";charset=utf-8")
	public Response toJson(@DefaultValue("0") @QueryParam("offset") int offset,
			@DefaultValue("200") @QueryParam("limit") int limit) {

		JSONObject jsonResponse = new JSONObject();
		try {
			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			jsonResponse = MongodbMethods.getStatsFroRest(Config.taskRunCollection, null, null, offset, limit);

			return Response.status(200).entity(jsonResponse.toString()).build();
		} catch (Exception e) {
			logger.error("error", e);
			jsonResponse.put("status", "error");
			jsonResponse.put("message", e);
			return Response.status(500).entity(jsonResponse.toString()).build();

		}
	}

	@GET
	@Path("task")
	@Produces("application/json" + ";charset=utf-8")
	public Response taskRunsByTaskID(@DefaultValue("0") @QueryParam("offset") int offset,
			@DefaultValue("200") @QueryParam("limit") int limit, @QueryParam("task_id") int task_id) {

		JSONObject jsonResponse = new JSONObject();
		try {
			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			jsonResponse = MongodbMethods.getStatsFroRest(Config.taskRunCollection, "task_id", task_id, offset, limit);

			return Response.status(200).entity(jsonResponse.toString()).build();
		} catch (Exception e) {
			logger.error("error", e);
			jsonResponse.put("status", "error");
			jsonResponse.put("message", e);
			return Response.status(500).entity(jsonResponse.toString()).build();

		}
	}

	@GET
	@Path("{id}")
	@Produces("application/json" + ";charset=utf-8")
	public Response taskRunsToJson(@PathParam("id") Integer id, @DefaultValue("0") @QueryParam("offset") int offset,
			@DefaultValue("200") @QueryParam("limit") int limit) {

		JSONObject jsonResponse = new JSONObject();
		try {
			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			jsonResponse = MongodbMethods.getStatsFroRest(Config.taskRunCollection, "task_run_id", id, offset, limit);

			return Response.status(200).entity(jsonResponse.toString()).build();
		} catch (Exception e) {
			logger.error("error", e);
			jsonResponse.put("status", "error");
			jsonResponse.put("message", e);
			return Response.status(500).entity(jsonResponse.toString()).build();

		}
	}
}

@@ -1548,16 +1548,41 @@
 %22task%22)%0A
+%09@QueryParam(%22task_id%22) %0A
 %09@Produc
@@ -2386,32 +2386,901 @@
 ng()).build();%0A%0A
+%09%09%7D%0A%09%7D%0A%09%0A%09@GET%0A%09@Path(%22project%22)%0A%09@Produces(%22application/json%22 + %22;charset=utf-8%22)%0A%09public Response taskRunsByProjectID(@DefaultValue(%220%22) @QueryParam(%22offset%22) int offset,%0A%09%09%09@DefaultValue(%22200%22) @QueryParam(%22limit%22) int limit, @QueryParam(%22project_id%22) int project_id) %7B%0A%0A%09%09JSONObject jsonResponse = new JSONObject();%0A%09%09try %7B%0A%09%09%09InputStream stream = sendTaskRun.class.getResourceAsStream(%22/log4j.properties%22);%0A%09%09%09PropertyConfigurator.configure(stream);%0A%09%09%09Config.reload();%0A%0A%09%09%09jsonResponse = MongodbMethods.getStatsFroRest(Config.taskRunCollection, %22project_id%22, project_id, offset, limit);%0A%0A%09%09%09return Response.status(200).entity(jsonResponse.toString()).build();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09logger.error(%22error%22, e);%0A%09%09%09jsonResponse.put(%22status%22, %22error%22);%0A%09%09%09jsonResponse.put(%22message%22, e);%0A%09%09%09return Response.status(500).entity(jsonResponse.toString()).build();%0A%0A
 %09%09%7D%0A%09%7D%0A%0A%09@GET%0A%09@

add TaskRuns based on project_id