package edu.brown.hackathon.fifteenminutes;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;

@SuppressWarnings("serial")
public class NewFamousUserServlet extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    List<Entity> results = new LinkedList<Entity>();
    Random rand = new Random();
    while (results.size() == 0) {
      Filter randomFilter = new FilterPredicate("rand_number",
          FilterOperator.GREATER_THAN_OR_EQUAL, rand.nextFloat());
      Query query = new Query("User");
      query.addSort("rand_number");
      query.setFilter(randomFilter);
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      results = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1));
    }
    
    long userId = UserResource.parseUserIdFromEntity(results.get(0));
    
    Entity famousUser = new Entity("FamousUser");
    famousUser.setProperty("user_id", userId);
    famousUser.setProperty("current_time", System.currentTimeMillis());

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(famousUser);
    
    resp.setContentType("application/json");
    UserResource ur = new UserResource(userId);
    resp.getWriter().println(new Gson().toJson(ur));
  }
}

@@ -846,16 +846,545 @@
 ption %7B%0A
+    long userId = selectRandomUserId();%0A    %0A    Entity famousUser = new Entity(%22FamousUser%22);%0A    famousUser.setProperty(%22user_id%22, userId);%0A    famousUser.setProperty(%22current_time%22, System.currentTimeMillis());%0A%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    datastore.put(famousUser);%0A    %0A    resp.setContentType(%22application/json%22);%0A    UserResource ur = new UserResource(userId);%0A    resp.getWriter().println(new Gson().toJson(ur));%0A  %7D%0A  %0A  private static long selectRandomUserId() %7B%0A
     List
@@ -1912,34 +1912,22 @@
   %7D%0A    
-%0A    long userId =
+return
  UserRes
@@ -1975,445 +1975,8 @@
 ));%0A
-    %0A    Entity famousUser = new Entity(%22FamousUser%22);%0A    famousUser.setProperty(%22user_id%22, userId);%0A    famousUser.setProperty(%22current_time%22, System.currentTimeMillis());%0A%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    datastore.put(famousUser);%0A    %0A    resp.setContentType(%22application/json%22);%0A    UserResource ur = new UserResource(userId);%0A    resp.getWriter().println(new Gson().toJson(ur));%0A
   %7D%0A

Refactor rand user selection into its own method.