package main.java.recoin.mongodb_version.rest;

import java.io.InputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

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

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

@Path("/Task")
public class Task {

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

	@GET
	@Path("{id}")
	@Produces("application/json" + ";charset=utf-8")
	public Response toJson(@PathParam("id") int id) {
		Document data = new Document();
		try {

			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			data = MongodbMethods.getTaskFromMongoDB(id);
			if (data == null) {
				data = new Document();
				String message = "no task with id " + id;
				data.put("message", "");
			}

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

		}
	}

	@GET
	@Path("{id}")
	@Produces("application/json-ld" + ";charset=utf-8")
	public Response toJsonLD(@PathParam("id") int id) {
		Document data = new Document();
		JSONObject jsonLD = new JSONObject();
		try {

			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			data = MongodbMethods.getTaskFromMongoDB(id);
			jsonLD.put("@context", "http://recoin.cloudapp.net/social-computer/task/");
			jsonLD.put("@type", "task");

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

	@GET
	@Path("{id}/Responses")
	@Produces("application/json" + ";charset=utf-8")
	public Response getTaskRuns(@PathParam("id") int id) {
		Document data = new Document();
		try {

			InputStream stream = sendTaskRun.class.getResourceAsStream("/log4j.properties");
			PropertyConfigurator.configure(stream);
			Config.reload();

			data = MongodbMethods.getTaskRunsFromMongoDB(id);
			if (data == null) {
				data = new Document();
				String message = "no responses for id " + id;
				data.put("message", "");
			}
			data.put("status", "success");
			return Response.status(200).entity(data.toJson().toString()).build();
		} catch (Exception e) {
			logger.error("error", e);
			data.put("status", "error");
			data.put("message", e);
			return Response.status(500).entity(data.toJson().toString()).build();

		}
	}

}

@@ -68,16 +68,44 @@
 tStream;
+%0Aimport java.util.ArrayList;
 %0A%0Aimport
@@ -345,16 +345,43 @@
 cument;%0A
+import org.json.JSONArray;%0A
 import o
@@ -2420,32 +2420,92 @@
 new Document();%0A
+%09%09ArrayList%3CDocument%3E taskRuns = new ArrayList%3CDocument%3E();%0A
 %09%09try %7B%0A%0A%09%09%09Inpu
@@ -2640,36 +2640,40 @@
 g.reload();%0A%0A%09%09%09
-da
 ta
+skRuns
  = MongodbMethod
@@ -2701,36 +2701,40 @@
 oDB(id);%0A%09%09%09if (
-da
 ta
+skRuns
  == null) %7B%0A%09%09%09%09
@@ -2831,24 +2831,179 @@
 sage%22, %22%22);%0A
+%09%09%09%7Delse%7B%0A%09%09%09%09JSONArray array = new JSONArray();%0A%09%09%09%09for (Document document : taskRuns) %7B%0A%09%09%09%09%09array.put(document);%0A%09%09%09%09%7D%0A%09%09%09%09data.put(%22taskRuns%22, array);%0A
 %09%09%09%7D%0A%09%09%09data

fix getTaskRuns to return a list