package com.xmlintl.falcon.ws;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.xmlintl.falcon.util.FalconException;
import com.xmlintl.falcon.util.TranslateSegment;

/**
 * Servlet implementation class TranslateSegmentWS
 */
@WebService(targetNamespace = "http://ws.falcon.xmlintl.com/", portName = "TranslateSegmentWSPort", serviceName = "TranslateSegmentWSService")
@WebServlet("/TranslateSegmentWS")
public class TranslateSegmentWS extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TranslateSegmentWS()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {

        // Set a cookie for the user, so that the counter does not increate
        // every time the user press refresh
        HttpSession session = request.getSession(true);
        // Set the session valid for 5 secs
        session.setMaxInactiveInterval(5);
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String engineID = request.getParameter("engineID");
        String segment = request.getParameter("segment");

        System.out.println("engineID: " + engineID);
        System.out.println("segment: " + segment);

        JsonObject jsonObject = new JsonObject();

        GsonBuilder gsonBuilder = new GsonBuilder();

//      gsonBuilder.setPrettyPrinting();

        Gson gson = gsonBuilder.create();

        try
        {
            TranslateSegment translateSegment = new TranslateSegment(engineID, segment);

            gson = gsonBuilder.create();

            jsonObject = new JsonObject();

            String uuid = translateSegment.getUuid();

            jsonObject.addProperty("UUID", uuid);

            translateSegment.translate();

            String translation = translateSegment.getTranslation();

            jsonObject.addProperty("translation", translation);

            String confidenceScore = translateSegment.getBleuScore();

            if (confidenceScore != null)
            {
                jsonObject.addProperty("confidenceScore", confidenceScore);
            }

            String json = gson.toJson(jsonObject);

            out.println(json);
        }
        catch (FalconException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            getServletContext().log("An exception occurred in ListEnginesWS", e);
            
            jsonObject.addProperty("error", "FAILED");

            String json = gson.toJson(jsonObject);

            out.println(json);
        }
    }

}

@@ -1169,32 +1169,46 @@
 sponse)%0A     */%0A
+    @Override%0A
     protected vo
@@ -1460,24 +1460,38 @@
 se)%0A     */%0A
+    @Override%0A
     protecte
@@ -2100,25 +2100,24 @@
 %22segment%22);%0A
-%0A
         Syst
@@ -2117,78 +2117,343 @@
    S
-ystem.out.println(%22engineID: %22 + engineID);%0A        System.out.println
+tring customerID = request.getParameter(%22customerID%22);%0A        String srcLang = request.getParameter(%22srcLang%22);%0A        String tgtLang = request.getParameter(%22tgtLang%22);%0A%0A        log(%22engineID: %22 + engineID);%0A        log(%22customerID: %22 + customerID);%0A        log(%22srcLang: %22 + srcLang);%0A        log(%22tgtLang: %22 + tgtLang);%0A        log
 (%22se
@@ -3078,16 +3078,77 @@
 ation();
+%0A            %0A            log(%22translation: %22 + translation);
 %0A%0A      

Improve logging.