예제 #1
0
  @Test
  public void testQuery() {

    Car car = new Car();
    car.setLicenseNumber("MARIOS_CAR_LICENSE");
    Ebean.save(car);

    VehicleDriver driver = new VehicleDriver();
    driver.setName("Mario");
    driver.setVehicle(car);
    Ebean.save(driver);

    Query<VehicleDriver> query = Ebean.find(VehicleDriver.class);
    query.where().eq("vehicle.licenseNumber", "MARIOS_CAR_LICENSE");
    List<VehicleDriver> drivers = query.findList();

    Assert.assertNotNull(drivers);
    Assert.assertEquals(1, drivers.size());
    Assert.assertNotNull(drivers.get(0));

    Assert.assertEquals("Mario", drivers.get(0).getName());
    Assert.assertEquals("MARIOS_CAR_LICENSE", drivers.get(0).getVehicle().getLicenseNumber());

    Vehicle car2 = Ebean.find(Vehicle.class, car.getId());

    car2.setLicenseNumber("test");
    Ebean.save(car);
  }
예제 #2
0
 public static Page<Official> page(
     int page, int pageSize, String sortBy, String order, String filter) {
   Query<Official> query = Ebean.find(Official.class);
   query.where().ilike("lastName", "%" + filter + "%");
   query.where().orderBy(sortBy + " " + order);
   Page<Official> p = query.findPagingList(pageSize).getPage(page);
   return p;
 }
예제 #3
0
 public static Official findByNumber(String number, ProcessingType processingType) {
   Official official;
   Query<Official> query = null;
   if (processingType.equals(ProcessingType.batch)) query = ebeanServer.find(Official.class);
   else if (processingType.equals(ProcessingType.online)) query = Ebean.find(Official.class);
   query.where().eq("number", number);
   official = query.findUnique();
   return official;
 }
 /**
  * Filters a query to return only the junctions that point to the left model and the right model
  *
  * @param leftId The id of the left item
  * @param rightId The id of the right item
  * @param query The query to filter
  * @return
  */
 private List<J> applyJunctionFiltering(Long leftId, Long rightId, Query<J> query) {
   if (query == null) {
     query = createJunctionQuery(new ServiceParams());
   }
   return query
       .where()
       .eq(getTableName(leftClass, leftFieldName) + "_id", leftId)
       .eq(getTableName(junctionClass, rightFieldName) + "_id", rightId)
       .findList();
 }
예제 #5
0
 public static Official findByName(
     String lastName, String firstName, ProcessingType processingType) {
   Official official;
   Query<Official> query = null;
   if (processingType.equals(ProcessingType.batch)) query = ebeanServer.find(Official.class);
   else if (processingType.equals(ProcessingType.online)) query = Ebean.find(Official.class);
   query.where().eq("lastName", lastName);
   query.where().eq("firstName", firstName);
   official = query.findUnique();
   return official;
 }
예제 #6
0
 /**
  * Creates a _new_ Query from the specified Query and the filters as defined in Table.
  *
  * @param query
  * @return a new Query object (even if no filters exist)
  */
 protected Query applyFilters(Query query) {
   Query q = query.copy();
   if (table.getFilters().isEmpty()) {
     return q;
   }
   for (Map.Entry<String, Filter> e : table.getFilters().entrySet()) {
     Filter f = e.getValue();
     q.where(f.asExpression(q));
   }
   return q;
 }
 /** {@inheritDoc} */
 @Override
 @Transactional(readOnly = true)
 public PagingList<Session> find(
     User user, String name, String orderBy, boolean ascending, int pageSize) {
   // TODO union with sessions where the user has participated
   Query<Session> query = ebean.find(Session.class);
   ExpressionList<Session> expr = query.where().eq("author", user);
   if (!Strings.isNullOrEmpty(name)) {
     expr = expr.ilike("name", name);
   }
   query = ascending ? expr.orderBy().asc(orderBy) : expr.orderBy().desc(orderBy);
   return new EbeanPagingList<Session>(query.findPagingList(pageSize));
 }
  public void runExampleUsingOrders() {

    LoadExampleData.load();

    PathProperties pathProperties =
        PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))");

    Query<Order> query = Ebean.createQuery(Order.class);
    pathProperties.apply(query);

    List<Order> orders = query.where().gt("id", 1).findList();

    String rawJson = Ebean.json().toJson(orders);
    System.out.println(rawJson);
  }
  /**
   * Retrieves a list of child objects for the given junctions
   *
   * @param junctions The junctions to get the child objects from
   * @param params The params passed to the services
   * @return A list of child objects
   */
  private List<R> getChildObjects(List<? extends Model> junctions, ServiceParams params) {
    List ids = new ArrayList();
    for (Model junction : junctions) {
      List<String> fields = Arrays.asList(junction._ebean_getFieldNames());
      int fieldIndex = fields.indexOf(rightFieldName);
      ids.add(EbeanUtil.getFieldValue((R) junction._ebean_getField(fieldIndex, junction), "id"));
    }
    Query<R> query = createQuery(params);
    handleFieldsAndFetches(query, rightClass, params);

    // ?orderBy=name asc
    if (params.orderBy != null && params.orderBy.length > 0) {
      query.orderBy(formatOrderBy(params.orderBy, rightClass));
    }

    return query.where().in("id", ids).findList();
  }
예제 #10
0
 public static List<Official> findActive(boolean active) {
   Query<Official> query = Ebean.find(Official.class);
   query.where().eq("active", active);
   List<Official> officials = query.findList();
   return officials;
 }
예제 #11
0
 public static Official findByKey(String key, String value) {
   Query<Official> query = Ebean.find(Official.class);
   query.where().eq(key, value);
   Official official = query.findUnique();
   return official;
 }
예제 #12
0
  @Override
  public WebResponse writeResponse(RequestFlags flags) throws IOException {
    dop.writeBytes("HTTP/1.0 200 Ok\r\n");
    dop.writeBytes("Connection: close\r\n");
    dop.writeBytes("Server: ModeratorGui\r\n");
    dop.writeBytes("Content-Type: text/plain\r\n");
    dop.writeBytes("\r\n");

    Integer i = 0;

    TreeMap<Integer, String> map = new TreeMap<Integer, String>();

    Query<Lists> eLists = ModeratorGui.instance.getDatabase().find(Lists.class);

    ExpressionFactory factory = eLists.getExpressionFactory();

    Expression lastExpr = null;

    if (!issues && !bans && !unbans && !promotions && !demotions) {
      lastExpr = factory.ne("type", 0);
    }

    if (issues) {
      lastExpr = factory.eq("type", 1);
    }

    if (bans) {
      if (lastExpr != null) {
        lastExpr = factory.or(lastExpr, factory.eq("type", 2));
      } else {
        lastExpr = factory.eq("type", 2);
      }
    }

    if (unbans) {
      if (lastExpr != null) {
        lastExpr = factory.or(lastExpr, factory.eq("type", 3));
      } else {
        lastExpr = factory.eq("type", 3);
      }
    }

    if (promotions) {
      if (lastExpr != null) {
        lastExpr = factory.or(lastExpr, factory.eq("type", 4));
      } else {
        lastExpr = factory.eq("type", 4);
      }
    }

    if (demotions) {
      if (lastExpr != null) {
        lastExpr = factory.or(lastExpr, factory.eq("type", 5));
      } else {
        lastExpr = factory.eq("type", 5);
      }
    }

    if (lastExpr != null) {
      lastExpr = factory.and(lastExpr, factory.like("target", request + "%"));
    } else {
      lastExpr = factory.like("target", request + "%");
    }

    List<Lists> lists = eLists.where(lastExpr).setMaxRows(500).orderBy("id DESC").findList();

    for (Lists list : lists) {
      int id = list.getReportId();
      ReportType type = ReportType.getType(list.getType());

      String result = "";
      switch (type) {
        case ISSUE:
          Issues issue =
              ModeratorGui.instance
                  .getDatabase()
                  .find(Issues.class)
                  .where()
                  .eq("id", id)
                  .findUnique();

          result = "{ ";
          result += "type: \"issue\", ";
          result += "closed: " + (issue.isClosed() ? "true" : "false") + ", ";
          result += "reporter: \"" + JSONObject.escape(issue.getReporter()) + "\", ";
          result += "reported: \"" + JSONObject.escape(issue.getReported()) + "\", ";
          result +=
              "time: \""
                  + JSONObject.escape(dateFormat.format(Long.valueOf(issue.getTimestamp())))
                  + "\", ";
          result += "reason: \"" + JSONObject.escape(issue.getIssue()) + "\"";
          result += " }";

          map.put(i++, result);
          break;
        case BAN:
          Bans ban =
              ModeratorGui.instance
                  .getDatabase()
                  .find(Bans.class)
                  .where()
                  .eq("id", id)
                  .findUnique();

          result = "{ ";
          result += "type: \"ban\", ";
          result += "reporter: \"" + JSONObject.escape(ban.getReporter()) + "\", ";
          result += "reported: \"" + JSONObject.escape(ban.getReported()) + "\", ";
          result +=
              "time: \""
                  + JSONObject.escape(dateFormat.format(Long.valueOf(ban.getTimestamp())))
                  + "\", ";
          result += "reason: \"" + JSONObject.escape(ban.getReason()) + "\"";
          result += " }";

          map.put(i++, result);
          break;
        case UNBAN:
          Unbans unban =
              ModeratorGui.instance
                  .getDatabase()
                  .find(Unbans.class)
                  .where()
                  .eq("id", id)
                  .findUnique();

          result = "{ ";
          result += "type: \"unban\", ";
          result += "reporter: \"" + JSONObject.escape(unban.getReporter()) + "\", ";
          result += "reported: \"" + JSONObject.escape(unban.getReported()) + "\", ";
          result +=
              "time: \""
                  + JSONObject.escape(dateFormat.format(Long.valueOf(unban.getTimestamp())))
                  + "\", ";
          result += "reason: \"" + JSONObject.escape(unban.getReason()) + "\"";
          result += " }";

          map.put(i++, result);
          break;
        case PROMOTE:
          Promotions promote =
              ModeratorGui.instance
                  .getDatabase()
                  .find(Promotions.class)
                  .where()
                  .eq("id", id)
                  .findUnique();

          result = "{ ";
          result += "type: \"promote\", ";
          result += "reporter: \"" + JSONObject.escape(promote.getReporter()) + "\", ";
          result += "reported: \"" + JSONObject.escape(promote.getReported()) + "\", ";
          result +=
              "time: \""
                  + JSONObject.escape(dateFormat.format(Long.valueOf(promote.getTimestamp())))
                  + "\", ";
          result += "prev: \"" + JSONObject.escape(promote.getPrevRank()) + "\", ";
          result += "new: \"" + JSONObject.escape(promote.getNewRank()) + "\", ";
          result += "reason: \"" + JSONObject.escape(promote.getReason()) + "\"";
          result += " }";

          map.put(i++, result);
          break;
        case DEMOTE:
          Demotions demote =
              ModeratorGui.instance
                  .getDatabase()
                  .find(Demotions.class)
                  .where()
                  .eq("id", id)
                  .findUnique();

          result = "{ ";
          result += "type: \"demote\", ";
          result += "reporter: \"" + JSONObject.escape(demote.getReporter()) + "\", ";
          result += "reported: \"" + JSONObject.escape(demote.getReported()) + "\", ";
          result +=
              "time: \""
                  + JSONObject.escape(dateFormat.format(Long.valueOf(demote.getTimestamp())))
                  + "\", ";
          result += "prev: \"" + JSONObject.escape(demote.getPrevRank()) + "\", ";
          result += "new: \"" + JSONObject.escape(demote.getNewRank()) + "\", ";
          result += "reason: \"" + JSONObject.escape(demote.getReason()) + "\"";
          result += " }";

          map.put(i++, result);
          break;
        default:
          map.put(i++, "");
          break;
      }
    }

    String result = "[ ";
    for (Integer I = 0; I < map.size(); I++) {
      result += (I != 0 ? ", " : "") + map.get(I);
    }
    result += " ]";

    dop.writeBytes(result);

    return this;
  }