Example #1
0
  @Test
  public void BeanClassOfTExpressionOfQArray() {
    PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class, "entity");
    QBean<Entity> beanProjection =
        Projections.bean(
            Entity.class,
            entity.getNumber("cId", Integer.class),
            entity.getNumber("eId", Integer.class));

    assertEquals(Entity.class, beanProjection.newInstance(1, 2).getClass());
  }
 /**
  * If filter is number its required to know its concrete class so this private helper method
  * creates and returns predicate based on concrete class.
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 private NumberPath createNumberPath(PathBuilder entityPath, String key, Object filter) {
   if (filter instanceof BigDecimal) {
     return entityPath.getNumber(key, BigDecimal.class);
   } else if (filter instanceof Long) {
     return entityPath.getNumber(key, Long.class);
   } else if (filter instanceof Integer) {
     return entityPath.getNumber(key, Integer.class);
   } else if (filter instanceof Double) {
     return entityPath.getNumber(key, Double.class);
   } else if (filter instanceof Float) {
     return entityPath.getNumber(key, Float.class);
   } else if (filter instanceof Byte) {
     return entityPath.getNumber(key, Byte.class);
   } else if (filter instanceof Short) {
     return entityPath.getNumber(key, Short.class);
   } else {
     throw new IllegalStateException(
         "Unknown number type in search filter. Supported type: BigDecimal, Long, Integer, Double, Float, Byte, Short");
   }
 }
 @Test
 public void Eq_with_deep_path() throws Exception {
   StringPath deepPath = entityPath.get("property1", Object.class).getString("property2");
   testQuery(deepPath.eq("good"), "property1.property2:good", 0);
 }
  @Before
  public void setUp() throws Exception {
    serializer = new LuceneSerializer(true, true);
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    author = entityPath.getString("author");
    text = entityPath.getString("text");
    publisher = entityPath.getString("publisher");
    year = entityPath.getNumber("year", Integer.class);
    rating = entityPath.getString("rating");
    gross = entityPath.getNumber("gross", Double.class);

    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);

    idx = new RAMDirectory();
    config =
        new IndexWriterConfig(Version.LUCENE_31, new StandardAnalyzer(Version.LUCENE_30))
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE);
    writer = new IndexWriter(idx, config);

    writer.addDocument(createDocument());

    writer.close();

    IndexReader reader = IndexReader.open(idx);
    searcher = new IndexSearcher(reader);
  }
  /**
   * Creates a Predicate from list of BooleanExpression predicates which represents all search
   * filters.
   *
   * @param config PaginationConfiguration data holding object
   * @return query to filter entities according pagination configuration data.
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  protected Predicate getPredicate(PaginationConfiguration config) {

    PathBuilder<T> entityPath = new PathBuilder(entityClass, getAliasName(entityClass));
    BooleanExpression predicate = null;

    Map<String, Object> filters = config.getFilters();
    if (filters != null) {
      // first we process nonstandard filters
      List<String> filtersToRemove = processNonStandardFilters(filters, entityPath);
      filters = removeUsedFilters(filtersToRemove, filters);
      if (!filters.isEmpty()) {
        for (Map.Entry<String, Object> entry : filters.entrySet()) {
          String key = entry.getKey();
          Object filter = entry.getValue();
          if (filter != null) {

            // if ranged search (from - to fields)
            if (key.contains("fromRange-")) {
              // CHECKSTYLE:OFF
              String parsedKey = key.substring(10);
              // CHECKSTYLE:ON
              if (filter instanceof Number) {
                NumberPath path = createNumberPath(entityPath, parsedKey, filter);
                predicate = and(predicate, path.goe((Number) filter));
              } else if (filter instanceof Date) {
                DatePath path = entityPath.getDate(parsedKey, Date.class);
                predicate = and(predicate, path.goe((Date) filter));
              }
            } else if (key.contains("toRange-")) {
              // CHECKSTYLE:OFF
              String parsedKey = key.substring(8);
              // CHECKSTYLE:ON
              if (filter instanceof Number) {
                NumberPath path = createNumberPath(entityPath, parsedKey, filter);
                predicate = and(predicate, path.loe((Number) filter));
              } else if (filter instanceof Date) {
                DatePath path = entityPath.getDate(parsedKey, Date.class);
                predicate = and(predicate, path.loe((Date) filter));
              }
            } else if (key.contains("list-")) {
              // CHECKSTYLE:OFF
              // if searching elements from list
              String parsedKey = key.substring(5);
              // CHECKSTYLE:ON
              ListPath path = entityPath.getList(parsedKey, filter.getClass());
              predicate = and(predicate, path.contains(filter));
            } else { // if not ranged search
              if (filter instanceof String) {
                StringPath path = entityPath.getString(key);
                String filterString = (String) filter;
                predicate = and(predicate, path.startsWithIgnoreCase(filterString));
              } else if (filter instanceof Date) {
                DatePath path = entityPath.getDate(key, Date.class);
                predicate = and(predicate, path.eq(filter));
              } else if (filter instanceof Number) {
                NumberPath path = createNumberPath(entityPath, key, filter);
                predicate = and(predicate, path.eq(filter));
              } else if (filter instanceof Boolean) {
                BooleanPath path = entityPath.getBoolean(key);
                predicate = and(predicate, path.eq((Boolean) filter));
              } else if (filter instanceof Enum) {
                EnumPath path = entityPath.getEnum(key, Enum.class);
                predicate = and(predicate, path.eq(filter));
              } else if (BaseEntity.class.isAssignableFrom(filter.getClass())) {
                PathBuilder path = entityPath.get(key);
                predicate = and(predicate, path.eq(filter));
              }
            }
          }
        }
      }
    }
    return predicate;
  }