 
package com.google.sps.servlets;

import com.google.gson.Gson;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.datastore.*;
import java.io.PrintWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.*; 
import java.util.*; 

@WebServlet("/interests")
public class InterestsServlet extends HttpServlet{
  
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    UserService userService =  UserServiceFactory.getUserService();
    // If user is logged in, prints out their topics
    if (userService.isUserLoggedIn()) {
      User user = userService.getCurrentUser();
      String userId = user.getUserId();
      Query query = 
          new Query("UserInfo")
          .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, userId));
      PreparedQuery results = datastore.prepare(query);
      Entity entity = results.asSingleEntity();
      String topics = (String) entity.getProperty("topics");
      ArrayList<String> topicsOutput = new ArrayList<>();
      String[] topicsArray = topics.split(",");
      for (String topic : topicsArray) {
        String topicOutput = "<p>" + topic + "</p>";
	      topicsOutput.add(topicOutput);
      }

      response.setContentType("application/json;");
      Gson gson = new Gson();
      response.getWriter().println(gson.toJson(topicsOutput));
    }
  }
}

@@ -1055,101 +1055,458 @@
     
-// If user is logged in, prints out their topics%0A    if (userService.isUserLoggedIn()
+ArrayList%3CString%3E topicsOutput;%0A    if (userService.isUserLoggedIn()) %7B%0A      topicsOutput = getTopics(userService, datastore);%0A      response.setContentType(%22application/json;%22);%0A      Gson gson = new Gson();%0A      response.getWriter().println(gson.toJson(topicsOutput));%0A    %7D%0A  %7D%0A%0A  // If user is logged in, returns list of their topics to be printed%0A  public ArrayList%3CString%3E getTopics(UserService userService, DatastoreService datastore
 ) %7B%0A    
   Us
@@ -1501,18 +1501,16 @@
 ) %7B%0A    
- 
- 
 User use
@@ -1543,26 +1543,24 @@
 User();%0A    
-  
 String userI
@@ -1577,26 +1577,24 @@
 etUserId();%0A
-  
     Query qu
@@ -1599,27 +1599,16 @@
 query = 
-%0A          
 new Quer
@@ -1621,18 +1621,16 @@
 rInfo%22)%0A
-  
         
@@ -1714,18 +1714,16 @@
 ));%0A    
-  
 Prepared
@@ -1764,18 +1764,16 @@
 query);%0A
-  
     Enti
@@ -1810,26 +1810,24 @@
 tity();%0A    
-  
 String topic
@@ -1865,26 +1865,24 @@
 (%22topics%22);%0A
-  
     ArrayLis
@@ -1925,26 +1925,24 @@
 st%3C%3E();%0A    
-  
 String%5B%5D top
@@ -1975,18 +1975,16 @@
 %22);%0A    
-  
 for (Str
@@ -2016,18 +2016,16 @@
 %7B%0A      
- 
- 
 String t
@@ -2065,9 +2065,8 @@
 %3E%22;%0A
-%09
     
@@ -2102,146 +2102,25 @@
 t);%0A
-  
     %7D%0A
-%0A  
     re
-sponse.setContentType(%22application/json;%22);%0A      Gson gson = new Gson();%0A      response.getWriter().println(gson.toJson(
+turn 
 topi
@@ -2123,28 +2123,20 @@
 topicsOutput
-));%0A    %7D
+;
 %0A  %7D%0A%7D%0A

refactor interests servlet