@@ -0,0 +1,3068 @@
+package com.google.sps.servlets;%0A%0Aimport java.io.IOException;%0Aimport javax.servlet.http.HttpServlet;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport javax.servlet.annotation.WebServlet;%0Aimport com.google.sps.data.Trend;%0Aimport com.google.gson.Gson;%0Aimport com.google.appengine.api.datastore.DatastoreService;%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0Aimport com.google.appengine.api.datastore.Entity;%0Aimport com.google.appengine.api.datastore.PreparedQuery;%0Aimport com.google.appengine.api.datastore.Query;%0Aimport com.google.appengine.api.datastore.Query.*;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Iterator;%0Aimport com.google.gson.Gson;%0Aimport java.util.Date;%0Aimport java.text.SimpleDateFormat;%0Aimport java.text.ParseException;%0A%0A@WebServlet(%22/archive%22)%0Apublic class ArchiveServlet extends HttpServlet %7B%0A%0A  public long millisInDay = 86400000;%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    String stringDate = request.getParameter(%22date%22);%0A    long millis = 0;%0A    try %7B%0A      millis = convertStringtoMillis(stringDate);%0A      System.out.println(millis);%0A    %7D catch (ParseException e) %7B%0A      e.printStackTrace();%0A    %7D%0A    PreparedQuery results = getFilteredQuery(millis);%0A    Iterator%3CEntity%3E totalTrends = results.asIterable().iterator();%0A    %0A    response.setContentType(%22application/json%22);%0A    String json = convertToJson(getTrends(totalTrends));%0A    response.getWriter().println(json);%0A  %7D%0A%0A  private String convertToJson(List%3CTrend%3E trends) %7B%0A    Gson gson = new Gson();%0A    String json = gson.toJson(trends);%0A    return json;%0A  %7D%0A%0A  private long convertStringtoMillis(String requestedDate) throws ParseException %7B%0A    SimpleDateFormat sdf = new SimpleDateFormat(%22MM/dd/yyyy%22);%0A    Date date = sdf.parse(requestedDate);%0A    return date.getTime();%0A  %7D%0A%0A  private PreparedQuery getFilteredQuery(long time) %7B%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    Filter startFilter = new FilterPredicate(%22timestamp%22, FilterOperator.GREATER_THAN_OR_EQUAL, time);%0A    Filter endFilter = new FilterPredicate(%22timestamp%22, FilterOperator.LESS_THAN_OR_EQUAL, time + millisInDay);%0A    CompositeFilter dayRangeFilter = CompositeFilterOperator.and(startFilter, endFilter);%0A    Query query = new Query(%22Trend%22).setFilter(dayRangeFilter).addSort(%22timestamp%22, SortDirection.DESCENDING);%0A    return datastore.prepare(query);%0A  %7D%0A%0A  private List%3CTrend%3E getTrends(Iterator%3CEntity%3E resultingTrends) %7B%0A    List%3CTrend%3E trends = new ArrayList%3C%3E();%0A    while (resultingTrends.hasNext()) %7B%0A      Entity entity = resultingTrends.next();%0A      long timestamp = (long) entity.getProperty(%22timestamp%22);%0A      long id = entity.getKey().getId();%0A      String title = (String) entity.getProperty(%22title%22);%0A      long frequency = (long) entity.getProperty(%22traffic%22);%0A      Trend trend = new Trend(id, title, frequency, timestamp);%0A      trends.add(trend);%0A    %7D%0A    return trends;%0A  %7D%0A%7D%0A

Add an archive servlet for retrieving stored trends