public void writeJsonIfLocal(JsonGenerator g) throws JsonGenerationException, IOException {
    if (this.is_local) {
      g.writeStartObject();
      g.writeFieldName(KEY_LATITUDE);
      g.writeNumber(this.latitude);
      g.writeFieldName(KEY_LONGITUDE);
      g.writeNumber(this.longitude);
      g.writeFieldName(KEY_BUILDING);
      g.writeString(this.buildingName);
      g.writeFieldName(KEY_ROOM);
      g.writeString(this.roomName);
      g.writeFieldName(KEY_RATING);
      g.writeNumber(this.my_productivity);
      g.writeFieldName(KEY_CAPACITY);
      g.writeNumber(this.my_capacity);
      g.writeFieldName(KEY_CROWD);
      g.writeNumber(this.my_crowd);
      g.writeFieldName(KEY_NUM_SURVEYS);
      g.writeNumber(this.my_n_surveys);
      g.writeFieldName(KEY_COMMENTS);
      g.writeStartArray();

      CloseableIterator<CommentEntry> commIterator = this.commentEntries.closeableIterator();
      while (commIterator.hasNext()) commIterator.next().writeJsonIfLocal(g);
      try {
        commIterator.close();
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      g.writeEndArray();
      g.writeFieldName(KEY_NOISE);
      g.writeStartArray();

      CloseableIterator<NoiseEntry> noiseIterator = this.noiseEntries.closeableIterator();
      while (noiseIterator.hasNext()) noiseIterator.next().writeJsonIfLocal(g);
      try {
        noiseIterator.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }

      g.writeEndArray();
      g.writeEndObject();
    }
  }
Example #2
0
 public void setupDisplay() {
   if (characters != null) return;
   characters = new ArrayList<PlayerCharacter>();
   CloseableIterator<PlayerCharacter> pcs = charactersFC.closeableIterator();
   while (pcs.hasNext()) {
     characters.add(pcs.next());
   }
   try {
     pcs.close();
   } catch (SQLException e) {
   }
 }
  @Test
  public void testQueryRawMax() throws Exception {
    Dao<Foo, Object> dao = createDao(Foo.class, true);

    Foo foo1 = new Foo();
    foo1.stringField = "1";
    foo1.val = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.stringField = "1";
    foo2.val = 20;
    assertEquals(1, dao.create(foo2));
    Foo foo3 = new Foo();
    foo3.stringField = "2";
    foo3.val = 30;
    assertEquals(1, dao.create(foo3));
    Foo foo4 = new Foo();
    foo4.stringField = "2";
    foo4.val = 40;
    assertEquals(1, dao.create(foo4));

    QueryBuilder<Foo, Object> qb = dao.queryBuilder();
    qb.selectRaw("string, max(val) as val");
    qb.groupBy(Foo.STRING_COLUMN_NAME);
    GenericRawResults<Foo> results =
        dao.queryRaw(qb.prepareStatementString(), dao.getRawRowMapper());
    assertNotNull(results);
    CloseableIterator<Foo> iterator = results.closeableIterator();
    try {
      assertTrue(iterator.hasNext());
      assertEquals(foo2.val, iterator.next().val);
      assertTrue(iterator.hasNext());
      assertEquals(foo4.val, iterator.next().val);
      assertFalse(iterator.hasNext());
    } finally {
      iterator.close();
    }
  }
  public double getCurrentNoise(TitaniumDb helper) throws SQLException, AngryJulienException {
    Dao<NoiseEntry, Long> dao = helper.dao(NoiseEntry.class, Long.class);
    QueryBuilder<NoiseEntry, Long> qb = dao.queryBuilder();
    long curTime = System.currentTimeMillis();

    double weekAgo = 0.0, dayAgo = 0.0, hourAgo = 0.0;

    //// WEEK AGO
    int count = 0;
    qb.where()
        .eq(NoiseEntry.COLUMN_NAME_PARENT_ID, this.id)
        .and()
        .between(
            NoiseEntry.COLUMN_NAME_TIMESTAMP,
            curTime - TimeUtils.WEEK - TimeUtils.HOUR,
            curTime - TimeUtils.WEEK + TimeUtils.HOUR);
    CloseableIterator<NoiseEntry> it = qb.iterator();
    while (it.hasNext()) {
      weekAgo += it.next().getNoise();
      count++;
    }
    it.close();
    if (count == 0) {
      weekAgo = -1.1;
    } else {
      weekAgo /= count;
    }

    /// DAY AGO
    count = 0;
    qb = dao.queryBuilder();
    qb.where()
        .eq(NoiseEntry.COLUMN_NAME_PARENT_ID, this.id)
        .and()
        .between(
            NoiseEntry.COLUMN_NAME_TIMESTAMP,
            curTime - TimeUtils.DAY - TimeUtils.HOUR,
            curTime - TimeUtils.DAY + TimeUtils.HOUR);
    it = qb.iterator();
    while (it.hasNext()) {
      dayAgo += it.next().getNoise();
      count++;
    }
    it.close();
    if (count == 0) {
      dayAgo = -1.1;
    } else {
      dayAgo /= count;
    }

    //// HOUR AGO
    count = 0;
    qb = dao.queryBuilder();
    qb.where()
        .eq(NoiseEntry.COLUMN_NAME_PARENT_ID, this.id)
        .and()
        .between(
            NoiseEntry.COLUMN_NAME_TIMESTAMP,
            curTime - TimeUtils.HOUR - (5 * TimeUtils.MINUTE),
            curTime - TimeUtils.HOUR + (5 * TimeUtils.MINUTE));
    it = qb.iterator();
    while (it.hasNext()) {
      hourAgo += it.next().getNoise();
      count++;
    }
    it.close();
    if (count == 0) {
      hourAgo = -1.1;
    } else {
      hourAgo /= count;
    }

    double time = 0;
    count = 0;
    if (dayAgo != -1.1) {
      count++;
      time += dayAgo;
    }
    if (weekAgo != -1.1) {
      count++;
      time += weekAgo;
    }
    if (hourAgo != -1.1) {
      count++;
      time += hourAgo;
    }

    if (count > 0) {
      return time / count;
    }

    time = 0.0;
    count = 0;

    it = noiseEntries.closeableIterator();
    while (it.hasNext()) {
      time += it.next().getNoise();
      count++;
    }

    if (count > 0) return time / count;
    throw new AngryJulienException("I am so mad at this point b/c you ain't got anything");
  }