示例#1
0
 @SuppressWarnings("unchecked")
 public Collection<Point> getSearchPointsForLine(
     Key<Line> l, String geoCell, int plusMinusIndex, Objectify ofy) {
   String functionName = "getSearchPointsForLine(String geoCell)";
   Query<Point> q =
       ofy.query(Point.class)
           .ancestor(l)
           .filter("ignore", false)
           .filter("defaultGeoCell", geoCell)
           .limit(1);
   Key<Point> kMiddle;
   try {
     Cache cache =
         CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
     kMiddle = (Key<Point>) cache.get(q.toString());
     if (kMiddle == null) {
       kMiddle = q.getKey();
       cache.put(q.toString(), kMiddle);
     }
   } catch (CacheException e) {
     kMiddle = q.getKey();
     Logger.getLogger(location).log(Level.SEVERE, functionName + ": Cache error: " + e);
     e.printStackTrace();
   }
   if (kMiddle
       == null) { // dirty hack, but still better than failing in case that no point was found
     q = ofy.query(Point.class).ancestor(l).filter("defaultGeoCell", geoCell).limit(1);
     kMiddle = q.getKey();
     System.err.println(
         "had to resort to dirty hack and retrieve a point that is set to ignore for line "
             + l
             + " in cell "
             + geoCell
             + ". Result: "
             + kMiddle);
   }
   try {
     Point middle = ofy.get(kMiddle);
     Collection<Point> points;
     if (plusMinusIndex > 0) {
       points = getSearchPointsForLine(l, middle.getIndex(), plusMinusIndex, ofy);
     } else {
       points = new LinkedList<Point>();
       points.add(middle);
     }
     return points;
   } catch (NullPointerException e) {
     q = ofy.query(Point.class).ancestor(l).filter("ignore", false);
     System.err.println(
         "because of "
             + e
             + " had to resort to even dirtier hack and retrieve all potential points for line "
             + l
             + ", even those not in cell "
             + geoCell);
     return q.list();
   }
 }
示例#2
0
  private void listMessages(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.setCharacterEncoding("UTF-8");
    if (!IrssiNotifier.versionCheck(req.getParameter("version"))) {
      IrssiNotifier.printError(resp.getWriter(), new OldVersionException());
      return;
    }
    String id = req.getParameter("apiToken");
    String guid = req.getParameter("guid");

    if (id == null || guid == null || id.equals("") || guid.equals("")) {
      IrssiNotifier.printError(resp.getWriter(), "Virheellinen pyyntö");
      return;
    }

    ObjectifyDAO dao = new ObjectifyDAO();
    try {
      IrssiNotifierUser user = IrssiNotifier.getUser(dao, id);
      Query<Message> messages =
          dao.ofy().query(Message.class).order("-timestamp").ancestor(user).limit(LIMIT + 1);
      String starting = req.getParameter("starting");
      if (starting != null) {
        try {
          long startingTimestamp = Long.parseLong(starting);
          messages = messages.filter("timestamp <=", startingTimestamp);
        } catch (NumberFormatException e) {
        }
      }

      Message next = null;
      if (messages.count() == LIMIT + 1) {
        IrssiNotifier.log.info("Lisää viestejä on saatavilla");
        next = messages.list().get(LIMIT);
      } else {
        IrssiNotifier.log.info("Sisältää viestihistorian viimeiset viestit");
      }

      MessageListResponse response = new MessageListResponse(messages.limit(LIMIT));
      response.nextMessage = next;
      response.isNextFetch = starting != null;

      JSONSerializer serializer = new JSONSerializer();
      String jsonObject =
          serializer
              .include("messages")
              .transform(new CustomTimeTransformer(), "messages.timestamp")
              .transform(new CustomTimeTransformer(), "nextMessage.timestamp")
              .exclude("*.class")
              .serialize(response);
      resp.setHeader("Content-Type", "application/json");
      resp.getWriter().println(jsonObject);
      resp.getWriter().close();
    } catch (UserNotFoundException e) {
      IrssiNotifier.printError(resp.getWriter(), e);
    }
  }