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 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();

		}
	}

}

@@ -316,16 +316,44 @@
 ocument;
+%0Aimport org.json.JSONObject;
 %0A%0Aimport
@@ -1386,16 +1386,14 @@
 %7D%0A%0A%09
- 
 @GET%0A%09
- 
 @Pat
@@ -1403,17 +1403,16 @@
 %7Bid%7D%22)%0A%09
- 
 @Produce
@@ -1456,17 +1456,16 @@
 tf-8%22)%0A%09
- 
 public R
@@ -1509,17 +1509,17 @@
  id) %7B%0A%09
- 
+%09
 Document
@@ -1543,17 +1543,17 @@
 ent();%0A%09
- 
+%09
 JSONObje
@@ -1587,19 +1587,19 @@
 );%0A%09
- 
+%09
 try %7B%0A
-%09
 %0A%09
- 
+%09%09
 Inpu
@@ -1614,18 +1614,16 @@
 stream =
-%0A%09
  sendTas
@@ -1676,17 +1676,18 @@
 ies%22);%0A%09
- 
+%09%09
 Property
@@ -1719,17 +1719,18 @@
 ream);%0A%09
- 
+%09%09
 Config.r
@@ -1738,20 +1738,20 @@
 load();%0A
-%09
 %0A%09
- 
+%09%09
 data = M
@@ -1785,25 +1785,26 @@
 ngoDB(id);%0A%09
- 
+%09%09
 jsonLD.put(%22
@@ -1813,18 +1813,16 @@
 ontext%22,
-%0A%09
  %22http:/
@@ -1868,17 +1868,18 @@
 sk/%22);%0A%09
- 
+%09%09
 jsonLD.p
@@ -1903,12 +1903,12 @@
 %22);%0A
-%09%0A%09 
+%0A%09%09%09
 data
@@ -1931,25 +1931,26 @@
 success%22);%0A%09
- 
+%09%09
 return Respo
@@ -2008,17 +2008,17 @@
 ild();%0A%09
- 
+%09
 %7D catch 
@@ -2034,17 +2034,18 @@
 n e) %7B%0A%09
- 
+%09%09
 logger.e
@@ -2059,25 +2059,26 @@
 rror%22, e);%0A%09
- 
+%09%09
 data.put(%22st
@@ -2095,17 +2095,18 @@
 ror%22);%0A%09
- 
+%09%09
 data.put
@@ -2122,17 +2122,18 @@
 %22, e);%0A%09
- 
+%09%09
 return R
@@ -2199,13 +2199,12 @@
 );%0A%09
- 
+%09
 %7D%0A%09
- 
 %7D%0A%0A%09

fix import error