package com.google.step.snippet.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/vote")
public class FeedbackServlet extends HttpServlet {

  private static final String UP = "upvote";
  private static final String DOWN = "downvote";
  private static final String URL = "url";
  private static final String FEEDBACK = "feedback";

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Entity feedbackEntity = null;
    String url = request.getParameter(URL);

    if (url == null) {
      response.sendRedirect(request.getHeader("Referer"));
    }

    /* Query for card feedback by card URL */
    Query.FilterPredicate filter = new Query.FilterPredicate(URL, FilterOperator.EQUAL, url);
    Query query = new Query(FEEDBACK).setFilter(filter);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    feedbackEntity = datastore.prepare(query).asSingleEntity();
    long totalVotes = 0;

    if (feedbackEntity == null) {
      feedbackEntity = new Entity(FEEDBACK);
      feedbackEntity.setProperty(URL, url);
      /* Initialize vote count */
      if (request.getParameter(UP) != null) {
        totalVotes = 1;
        feedbackEntity.setProperty(UP, (long) 1);
        feedbackEntity.setProperty(DOWN, (long) 0);
      } else if (request.getParameter(DOWN) != null) {
        totalVotes = -1;
        feedbackEntity.setProperty(DOWN, (long) 1);
        feedbackEntity.setProperty(UP, (long) 0);
      }
    } else {
      /* Update with upvote count */
      if (request.getParameter(UP) != null) {
        totalVotes =
            (long) feedbackEntity.getProperty(UP) - (long) feedbackEntity.getProperty(DOWN) + 1;
        feedbackEntity.setProperty(UP, (long) feedbackEntity.getProperty(UP) + 1);
      }
      /* Update with downvote count */
      else if (request.getParameter(DOWN) != null) {
        totalVotes =
            (long) feedbackEntity.getProperty(UP) - (long) feedbackEntity.getProperty(DOWN) - 1;
        feedbackEntity.setProperty(DOWN, (long) feedbackEntity.getProperty(DOWN) + 1);
      }
    }
    /* Convert vote count to response string */
    String totalStr;
    if (totalVotes < 0) {
      totalStr = "-" + Long.toString(Math.abs(totalVotes));
    } else {
      totalStr = Long.toString(totalVotes);
    }
    datastore.put(feedbackEntity);
    response.setContentType("text/plain");
    response.getWriter().println(totalStr);
  }
}

@@ -1021,57 +1021,12 @@
   re
-sponse.sendRedirect(request.getHeader(%22Referer%22))
+turn
 ;%0A  
@@ -1524,114 +1524,8 @@
 l);%0A
-      /* Initialize vote count */%0A      if (request.getParameter(UP) != null) %7B%0A        totalVotes = 1;%0A  
     
@@ -1556,38 +1556,36 @@
 erty(UP, (long) 
-1
+0
 );%0A
-  
       feedbackEn
@@ -1626,91 +1626,36 @@
     
-  %7D else if (request.getParameter(DOWN) != null) %7B%0A        totalVotes = -1;%0A       
+%7D%0A%0A    long upVotes = (long)
  fee
@@ -1658,33 +1658,33 @@
  feedbackEntity.
-s
+g
 etProperty(DOWN,
@@ -1682,32 +1682,40 @@
 rty(
-DOWN, (long) 1);%0A       
+UP);%0A    long downVotes = (long)
  fee
@@ -1718,33 +1718,33 @@
  feedbackEntity.
-s
+g
 etProperty(UP, (
@@ -1742,83 +1742,16 @@
 rty(
-UP, (long) 0);%0A      %7D%0A    %7D else %7B%0A      /* Update with upvote count */%0A  
+DOWN);%0A%0A
     
@@ -1788,34 +1788,32 @@
 = null) %7B%0A      
-  
 totalVotes =%0A   
@@ -1812,108 +1812,34 @@
 es =
-%0A            (long) feedbackEntity.getProperty(UP) - (long) feedbackEntity.getProperty(DOWN)
+ upVotes - downVotes
  + 1;%0A
-  
     
@@ -1875,104 +1875,27 @@
 UP, 
-(long) feedbackEntity.getProperty(UP) + 1);%0A      %7D%0A      /* Update with downvote count */%0A     
+upVotes + 1);%0A    %7D
  els
@@ -1940,26 +1940,24 @@
 ll) %7B%0A      
-  
 totalVotes =
@@ -1960,100 +1960,28 @@
 es =
-%0A            (long) feedbackEntity.getProperty(UP) - (long) feedbackEntity.getProperty(DOWN)
+ upVotes - downVotes
  - 1
@@ -1984,26 +1984,24 @@
  - 1;%0A      
- 
- 
 feedbackEnti
@@ -2025,277 +2025,21 @@
 WN, 
-(long) feedbackEntity.getProperty(DOWN) + 1);%0A      %7D%0A    %7D%0A    /* Convert vote count to response string */%0A    String totalStr;%0A    if (totalVotes %3C 0) %7B%0A      totalStr = %22-%22 + Long.toString(Math.abs(totalVotes));%0A    %7D else %7B%0A      totalStr = Long.toString(totalVotes
+downVotes + 1
 );%0A 
@@ -2043,16 +2043,17 @@
 ;%0A    %7D%0A
+%0A
     data
@@ -2159,16 +2159,33 @@
 tln(
-totalStr
+Long.toString(totalVotes)
 );%0A 

Remove negative conversion