package qa.qcri.aidr.trainer.api.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import qa.qcri.aidr.trainer.api.service.TaskAnswerService;
import qa.qcri.aidr.trainer.api.template.TaskAnswerResponse;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: jilucas
 * Date: 9/15/13
 * Time: 8:37 AM
 * To change this template use File | Settings | File Templates.
 */
@Path("/taskanswer")
@Component
public class TaskAnswerController {

    protected static Logger logger = Logger.getLogger("service");

    @Autowired
    private TaskAnswerService taskAnswerService;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/save")
    public void saveTaskAnswer(String data){
        logger.debug("saveTaskAnswer start: " + new Date());
        logger.debug("saveTaskAnswer..: " + data);

        try{
            TaskAnswerResponse taskAnswerResponse =  taskAnswerService.getTaskAnswerResponseData(data);
            taskAnswerService.markOnHasHumanTag(taskAnswerResponse.getDocumentID());
            taskAnswerService.addToTaskAnswer( taskAnswerResponse);
            taskAnswerService.addToDocumentNominalLabel(taskAnswerResponse);
            taskAnswerService.removeTaskAssignment(taskAnswerResponse);
            taskAnswerService.pushTaskAnswerToJedis(taskAnswerResponse);

        }
        catch(Exception e){
            System.out.println("saveTaskAnswer : " +  e);
        }

    }
}

@@ -969,16 +969,291 @@
  data);%0A
+        // place into transaction block%0A        try%7B%0A            taskAnswerService.processTaskAnswer(data);%0A        %7D%0A        catch(Exception e)%7B%0A            logger.error(%22saveTaskAnswer got exception : %22 + e);%0A        %7D%0A%0A%0A%0A       // below is non transaction block%0A       /**
 %0A       
@@ -1843,16 +1843,21 @@
        %7D
+  **/
 %0A%0A    %7D%0A

taskAnswer will be handled in transaction