Java Code Examples for com.querydsl.jpa.JPQLQuery#where()

The following examples show how to use com.querydsl.jpa.JPQLQuery#where() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryDslRepositorySupportExt.java    From springlets with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a global contains text filter on the provided attributes.
 * WARNING: this creates a very inefficient query. If you have many entity
 * instances to query, use instead an indexed text search solution for better
 * performance.
 * @param text the text to look for
 * @param query
 * @param globalSearchAttributes the list of attributes to perform the
 *        filter on
 * @return the updated query
 */
protected JPQLQuery<T> applyGlobalSearch(String text, JPQLQuery<T> query,
    Path<?>... globalSearchAttributes) {
  if (text != null && !StringUtils.isEmpty(text) && globalSearchAttributes.length > 0) {
    BooleanBuilder searchCondition = new BooleanBuilder();
    for (int i = 0; i < globalSearchAttributes.length; i++) {
      Path<?> path = globalSearchAttributes[i];
      if (path instanceof StringPath) {
        StringPath stringPath = (StringPath) path;
        searchCondition.or(stringPath.containsIgnoreCase(text));
      } else if (path instanceof NumberExpression) {
        searchCondition.or(((NumberExpression<?>) path).like("%".concat(text).concat("%")));
      }
    }
    return query.where(searchCondition);
  }
  return query;
}
 
Example 2
Source File: UserService.java    From eds-starter6-jpa with Apache License 2.0 6 votes vote down vote up
@ExtDirectMethod(STORE_READ)
@Transactional(readOnly = true)
public ExtDirectStoreResult<User> read(ExtDirectStoreReadRequest request) {

	JPQLQuery<User> query = this.jpaQueryFactory.selectFrom(QUser.user);
	if (!request.getFilters().isEmpty()) {
		StringFilter filter = (StringFilter) request.getFilters().iterator().next();

		BooleanBuilder bb = new BooleanBuilder();
		bb.or(QUser.user.loginName.containsIgnoreCase(filter.getValue()));
		bb.or(QUser.user.lastName.containsIgnoreCase(filter.getValue()));
		bb.or(QUser.user.firstName.containsIgnoreCase(filter.getValue()));
		bb.or(QUser.user.email.containsIgnoreCase(filter.getValue()));

		query.where(bb);
	}
	query.where(QUser.user.deleted.isFalse());

	QuerydslUtil.addPagingAndSorting(query, request, User.class, QUser.user);
	QueryResults<User> searchResult = query.fetchResults();

	return new ExtDirectStoreResult<>(searchResult.getTotal(),
			searchResult.getResults());
}
 
Example 3
Source File: ViolationRepositoryImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Override
public List<CountByAccountAndType> countByAccountAndType(final Set<String> accountIds,
                                                         final Optional<DateTime> fromDate,
                                                         final Optional<DateTime> toDate,
                                                         final boolean resolved,
                                                         final boolean whitelisted) {
    final QViolationEntity qViolation = new QViolationEntity("v");
    final QViolationTypeEntity qType = new QViolationTypeEntity("t");


    final JPQLQuery<ViolationEntity> query = from(qViolation);
    query.join(qViolation.violationTypeEntity, qType);

    final Collection<Predicate> whereClause = newArrayList();

    if (!accountIds.isEmpty()) {
        whereClause.add(qViolation.accountId.in(accountIds));
    }

    fromDate.map(qViolation.created::after).ifPresent(whereClause::add);
    toDate.map(qViolation.created::before).ifPresent(whereClause::add);

    if (whitelisted) {
        whereClause.add(qViolation.ruleEntity.isNotNull());
    } else if (resolved) {
        whereClause.add(qViolation.comment.isNotNull());
        whereClause.add(qViolation.ruleEntity.isNull());
    } else {
        whereClause.add(qViolation.comment.isNull());
        whereClause.add(qViolation.ruleEntity.isNull());
    }

    query.where(allOf(whereClause));

    query.groupBy(qViolation.accountId, qType.id);
    query.orderBy(qViolation.accountId.asc(), qType.id.asc());

    return query.select(Projections.constructor(CountByAccountAndType.class, qViolation.accountId, qType.id, qViolation.id.count())).fetch();
}
 
Example 4
Source File: FeedSubscriptionDAO.java    From commafeed with Apache License 2.0 5 votes vote down vote up
public List<FeedSubscription> findByCategory(User user, FeedCategory category) {
	JPQLQuery<FeedSubscription> query = query().selectFrom(sub).where(sub.user.eq(user));
	if (category == null) {
		query.where(sub.category.isNull());
	} else {
		query.where(sub.category.eq(category));
	}
	return initRelations(query.fetch());
}
 
Example 5
Source File: FeedDAO.java    From commafeed with Apache License 2.0 5 votes vote down vote up
public List<Feed> findNextUpdatable(int count, Date lastLoginThreshold) {
	JPQLQuery<Feed> query = query().selectFrom(feed);
	query.where(feed.disabledUntil.isNull().or(feed.disabledUntil.lt(new Date())));

	if (lastLoginThreshold != null) {
		QFeedSubscription subs = QFeedSubscription.feedSubscription;
		QUser user = QUser.user;

		JPQLQuery<Integer> subQuery = JPAExpressions.selectOne().from(subs);
		subQuery.join(subs.user, user).where(user.lastLogin.gt(lastLoginThreshold));
		query.where(subQuery.exists());
	}

	return query.orderBy(feed.disabledUntil.asc()).limit(count).distinct().fetch();
}
 
Example 6
Source File: LifecycleRepositoryImpl.java    From fullstop with Apache License 2.0 2 votes vote down vote up
@Override
public Page<LifecycleEntity> findByApplicationNameAndVersion(final String name, final String version, final Pageable pageable) {

    final QLifecycleEntity qLifecycleEntity = QLifecycleEntity.lifecycleEntity;
    final QApplicationEntity qApplicationEntity = QApplicationEntity.applicationEntity;
    final QVersionEntity qVersionEntity = QVersionEntity.versionEntity;


    final JPQLQuery<LifecycleEntity> query = from(qLifecycleEntity).leftJoin(qLifecycleEntity.applicationEntity, qApplicationEntity);

    if (version != null && isNotEmpty(version)) {
        query.join(qLifecycleEntity.versionEntity, qVersionEntity);
        query.where(qVersionEntity.name.eq(version));
    }

    query.where(qApplicationEntity.name.eq(name));

    final long total = query.fetchCount();

    query.groupBy(qLifecycleEntity.versionEntity,
            qLifecycleEntity.instanceId,
            qLifecycleEntity.created,
            qLifecycleEntity.id,
            qApplicationEntity.id);

    if (version != null && isNotEmpty(version)) {
        query.groupBy(qVersionEntity.id);
    }


    final Sort sort = pageable.getSort();
    final Sort fixedSort = (sort == null || isEmpty(sort)) ? SORT_BY_CREATED : sort;

    final PageRequest pageRequest = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), fixedSort);

    getQuerydsl().applyPagination(pageRequest, query);

    final List<LifecycleEntity> lifecycleEntities = total > 0 ? query.fetch() : emptyList();

    return new PageImpl<>(lifecycleEntities, pageRequest, total);

}