예제 #1
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);
    }
  }
예제 #2
0
 /**
  * Méthode permettant l'effacement de tout les messages postés pour un device donné
  *
  * @param device contient l'identifiant pour lequel les messages doivent être supprimé, si null
  *     tous les messages de la base sont supprimés
  * @return le nombre de message effectivement supprimés
  */
 public Integer raz(String device) {
   Query<Message> im = null;
   if (device == null) {
     im = ofy().query(Message.class);
   } else {
     im = ofy().query(Message.class).filter("device", device);
   }
   int rc = im.count();
   ofy().delete(im);
   return (rc);
 }
예제 #3
0
 // FIXME: not working
 int countUsersActiveInLastNDays(DatastoreService ds, int numDays) {
   com.googlecode.objectify.Query<User> q = ofy().query(User.class);
   if (numDays > 0) {
     // This is ridiculous, but 30 days in milliseconds is 2.5B, and if numDays is
     // in int, the expression below overflows and we look for
     // lastSeen > some-future-date. To fix, just cast it to a long.
     long numDays64Bit = numDays;
     q.filter(
         "lastSeen >", new Date(System.currentTimeMillis() - numDays64Bit * 24 * 60 * 60 * 1000));
   }
   return q.count();
 }
  public void putCathedra(final Cathedra cathedra) throws Exception {
    NamespaceController.getInstance().updateNamespace(NamespaceController.generalNamespace);
    Query<Cathedra> query = ofy().query(Cathedra.class).filter("name", cathedra.getName());
    if (query.count() != 0) {
      long oldCathedraId = query.getKey().getId();
      cathedra.setId(oldCathedraId);
    }
    DAOT.runInTransaction(
        logger,
        new DatastoreOperation<Void>() {
          @Override
          public Void run(DAOT daot) throws Exception {
            daot.getOfy().put(cathedra);
            return null;
          }

          @Override
          public String getOperationName() {
            return "Persisting of cathedra.";
          }
        });
  }