示例#1
0
  private <E> Predicate<E> createJoinPredicate(final AtomicBoolean more) {
    if (column == null && this.nodes == null) return null;
    final EntityCache<E> joinCache = this.joinEntity.getCache();
    Predicate<E> filter = createElementPredicate(joinCache, true);
    if (this.nodes != null) {
      for (FilterNode node : this.nodes) {
        if (((FilterJoinNode) node).joinClass != this.joinClass) {
          more.set(true);
          continue;
        }
        Predicate<E> f = ((FilterJoinNode) node).createJoinPredicate(more);
        if (f == null) continue;
        final Predicate<E> one = filter;
        final Predicate<E> two = f;
        filter =
            (filter == null)
                ? f
                : (or
                    ? new Predicate<E>() {

                      @Override
                      public boolean test(E t) {
                        return one.test(t) || two.test(t);
                      }

                      @Override
                      public String toString() {
                        return "(" + one + " OR " + two + ")";
                      }
                    }
                    : new Predicate<E>() {

                      @Override
                      public boolean test(E t) {
                        return one.test(t) && two.test(t);
                      }

                      @Override
                      public String toString() {
                        return "(" + one + " AND " + two + ")";
                      }
                    });
      }
    }
    return filter;
  }
示例#2
0
  @Override
  protected <T, E> Predicate<T> createPredicate(final EntityCache<T> cache) {
    if (column == null && this.nodes == null) return null;
    final EntityCache<E> joinCache = this.joinEntity.getCache();
    final AtomicBoolean more = new AtomicBoolean();
    Predicate<E> filter = createJoinPredicate(more);
    Predicate<T> rs = null;
    if (filter == null && !more.get()) return rs;
    if (filter != null) {
      final Predicate<E> inner = filter;
      rs =
          new Predicate<T>() {

            @Override
            public boolean test(final T t) {
              Predicate<E> joinPredicate = null;
              for (String joinColumn : joinColumns) {
                final Serializable key = cache.getAttribute(joinColumn).get(t);
                final Attribute<E, Serializable> joinAttr = joinCache.getAttribute(joinColumn);
                Predicate<E> p = (E e) -> key.equals(joinAttr.get(e));
                joinPredicate = joinPredicate == null ? p : joinPredicate.and(p);
              }
              return joinCache.exists(inner.and(joinPredicate));
            }

            @Override
            public String toString() {
              StringBuilder sb = new StringBuilder();
              sb.append(" #-- ON ")
                  .append(joinColumns[0])
                  .append("=")
                  .append(joinClass == null ? "null" : joinClass.getSimpleName())
                  .append(".")
                  .append(joinColumns[0]);
              for (int i = 1; i < joinColumns.length; i++) {
                sb.append(" AND ")
                    .append(joinColumns[i])
                    .append("=")
                    .append(joinClass == null ? "null" : joinClass.getSimpleName())
                    .append(".")
                    .append(joinColumns[i]);
              }
              sb.append(" --# ").append(inner.toString());
              return sb.toString();
            }
          };
    }
    if (more.get()) { // 存在不同Class的关联表
      if (this.nodes != null) {
        for (FilterNode node : this.nodes) {
          if (((FilterJoinNode) node).joinClass == this.joinClass) continue;
          Predicate<T> f = node.createPredicate(cache);
          if (f == null) continue;
          final Predicate<T> one = rs;
          final Predicate<T> two = f;
          rs =
              (rs == null)
                  ? f
                  : (or
                      ? new Predicate<T>() {

                        @Override
                        public boolean test(T t) {
                          return one.test(t) || two.test(t);
                        }

                        @Override
                        public String toString() {
                          return "(" + one + " OR " + two + ")";
                        }
                      }
                      : new Predicate<T>() {

                        @Override
                        public boolean test(T t) {
                          return one.test(t) && two.test(t);
                        }

                        @Override
                        public String toString() {
                          return "(" + one + " AND " + two + ")";
                        }
                      });
        }
      }
    }
    return rs;
  }