package com.google.sps.servlets;

import java.io.IOException;
import java.io.*;
import org.json.*;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;
import com.google.sps.data.Form;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/form-handler")
public class FormHandlerServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      //TODO 
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
         System.out.println("Request: " + request);
    //   System.out.println(request.getParameter("editUrl"));
    //   System.out.println(request.getParameter("publishedUrl"));
    //   System.out.println(request.getParameter("formID"));
      
    //   String editURL = request.getParameter("editUrl");
    //   String publishedURL = request.getParameter("publishedUrl");
    //   int formID = Integer.parseInt(request.getParameter("formID"));

        
      BufferedReader reader = request.getReader();
      StringBuilder sb = new StringBuilder();
      try {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
      } finally {
          reader.close();
      }
      System.out.println(sb.toString());

    // StringBuffer jb = new StringBuffer();
    // String line = null;
    // try {
    //     BufferedReader reader = request.getReader();
    //     while ((line = reader.readLine()) != null)
    //     jb.append(line);
    // } catch (Exception e) { /*report an error*/ }

    //     JSONObject jsonObject;
    // try {
    //     jsonObject =  HTTP.toJSONObject(jb.toString());
    // } catch (JSONException e) {
    //     // crash and burn
    //     throw new IOException("Error parsing JSON request string");
    // }
    //   System.out.println(jsonObject.toString());


    //   System.out.println("Reader: " + reader);
    //   System.out.println("Reader.toString : " + reader.toString());

      Gson gson = new Gson();
      Form form = gson.fromJson(sb.toString(), Form.class);

    //   Form form = new Form(editURL, publishedURL, 0);
      
      Entity formEntity = form.toDatastoreEntity();
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      datastore.put(formEntity);

      response.setContentType("text/html");
      response.sendRedirect("/form.html");
  }
}

@@ -865,514 +865,564 @@
     
-//TODO %0A  %7D%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A         System.out.println(%22Request: %22 + reque
+  ArrayList%3CMemePost%3E memePosts = new ArrayList%3C%3E();%0A%0A        MemePost memePost = null;%0A        Query query = new Query(%22MemePo
 st
+%22
 );%0A    
-//   System.out.println(request.getParameter(%22editUrl%22)
+    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(
 );%0A    
-//   System.out.println(request.getParameter(%22publishedUrl%22));%0A    //   System.out.println(request.getParameter(%22formID%22));%0A 
+    PreparedQuery results = datastore.prepare(query);%0A        for (Entity entity : results.asIterable()) %7B%0A
      
-%0A
     
-//
    
-String editURL = request.getParameter(%22editUrl%22
+memePost = new MemePost(entity);%0A            memePosts.add(memePost);%0A        %7D%0A%0A        Gson gson = new Gson(
 );%0A    
-//
    
+ 
 String 
-publishedURL = request.getParameter(%22publishedUrl
+json = gson.toJson(memePosts);%0A        response.setContentType(%22application/json
 %22);%0A    
 //  
@@ -1421,85 +1421,166 @@
     
-//
    
-int formID = Integer.parseInt(request.getParameter(%22formID%22));%0A%0A        
+ response.getWriter().println(json);%0A%0A%0A  %7D%0A%0A  @Override%0A  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException %7B
 %0A   
@@ -1769,18 +1769,16 @@
         
-  
 sb.appen
@@ -1907,711 +1907,8 @@
 );%0A%0A
-    // StringBuffer jb = new StringBuffer();%0A    // String line = null;%0A    // try %7B%0A    //     BufferedReader reader = request.getReader();%0A    //     while ((line = reader.readLine()) != null)%0A    //     jb.append(line);%0A    // %7D catch (Exception e) %7B /*report an error*/ %7D%0A%0A    //     JSONObject jsonObject;%0A    // try %7B%0A    //     jsonObject =  HTTP.toJSONObject(jb.toString());%0A    // %7D catch (JSONException e) %7B%0A    //     // crash and burn%0A    //     throw new IOException(%22Error parsing JSON request string%22);%0A    // %7D%0A    //   System.out.println(jsonObject.toString());%0A%0A%0A    //   System.out.println(%22Reader: %22 + reader);%0A    //   System.out.println(%22Reader.toString : %22 + reader.toString());%0A%0A
     
@@ -1996,66 +1996,8 @@
 ss);
-%0A%0A    //   Form form = new Form(editURL, publishedURL, 0);
 %0A   
@@ -2211,24 +2211,27 @@
 /html%22);%0A   
+ //
    response.

Add comments