use of org.springframework.data.jpa.domain.Specification in project irida by phac-nml.
the class ProjectServiceImpl method searchForProjects.
/**
* Search for projects using a few different types of search fields.
*
* @param allFields
* the search criteria to apply to all fields
* @param projectNameFilter
* the filter to apply specifically to project name
* @param organismNameFilter
* the filter to apply specifically to organism name
* @param user
* the filter to apply for user filtering
* @return the specification
*/
private static final Specification<Project> searchForProjects(final String allFields, final String projectNameFilter, final String organismNameFilter, final User user) {
return new Specification<Project>() {
/**
* This {@link Predicate} considers *all* fields on a
* {@link Project} with an OR filter.
*
* @param root
* the root of the query
* @param query
* the query
* @param cb
* the builder
* @return a {@link Predicate} that covers all fields with OR.
*/
private Predicate allFieldsPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final List<Predicate> allFieldsPredicates = new ArrayList<>();
allFieldsPredicates.add(cb.like(root.get("name"), "%" + allFields + "%"));
allFieldsPredicates.add(cb.like(root.get("organism"), "%" + allFields + "%"));
allFieldsPredicates.add(cb.like(root.get("id").as(String.class), "%" + allFields + "%"));
return cb.or(allFieldsPredicates.toArray(new Predicate[0]));
}
/**
* This {@link Predicate} considers each specific field on
* {@link Project} separately and joins them with an AND filter.
*
* @param root
* the root of the query
* @param query
* the query
* @param cb
* the builder
* @return a {@link Predicate} that covers individual fields with an
* AND.
*/
private Predicate specificFiltersPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final List<Predicate> filterPredicates = new ArrayList<>();
if (!StringUtils.isEmpty(projectNameFilter)) {
filterPredicates.add(cb.like(root.get("name"), "%" + projectNameFilter + "%"));
}
if (!StringUtils.isEmpty(organismNameFilter)) {
filterPredicates.add(cb.like(root.get("organism"), "%" + organismNameFilter + "%"));
}
return cb.and(filterPredicates.toArray(new Predicate[0]));
}
/**
* This {@link Predicate} filters out {@link Project}s for the
* specific user where they are assigned individually as a member.
*
* @param root
* the root of the query
* @param query
* the query
* @param cb
* the builder
* @return a {@link Predicate} that filters {@link Project}s where
* users are individually assigned.
*/
private Predicate individualProjectMembership(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final Subquery<Long> userMemberSelect = query.subquery(Long.class);
final Root<ProjectUserJoin> userMemberJoin = userMemberSelect.from(ProjectUserJoin.class);
userMemberSelect.select(userMemberJoin.get("project").get("id")).where(cb.equal(userMemberJoin.get("user"), user));
return cb.in(root.get("id")).value(userMemberSelect);
}
/**
* This {@link Predicate} filters out {@link Project}s for the
* specific user where they are assigned transitively through a
* {@link UserGroup}.
*
* @param root
* the root of the query
* @param query
* the query
* @param cb
* the builder
* @return a {@link Predicate} that filters {@link Project}s where
* users are assigned transitively through {@link UserGroup}
* .
*/
private Predicate groupProjectMembership(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final Subquery<Long> groupMemberSelect = query.subquery(Long.class);
final Root<UserGroupProjectJoin> groupMemberJoin = groupMemberSelect.from(UserGroupProjectJoin.class);
groupMemberSelect.select(groupMemberJoin.get("project").get("id")).where(cb.equal(groupMemberJoin.join("userGroup").join("users").get("user"), user));
return cb.in(root.get("id")).value(groupMemberSelect);
}
/**
* {@inheritDoc}
*/
@Override
public Predicate toPredicate(final Root<Project> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) {
final Predicate allFieldsPredicate = allFieldsPredicate(root, query, cb);
final Predicate specificFiltersPredicate = specificFiltersPredicate(root, query, cb);
final Predicate projectMember = cb.or(individualProjectMembership(root, query, cb), groupProjectMembership(root, query, cb));
if (user != null) {
return cb.and(allFieldsPredicate, specificFiltersPredicate, projectMember);
} else {
return cb.and(allFieldsPredicate, specificFiltersPredicate);
}
}
};
}
use of org.springframework.data.jpa.domain.Specification in project CzechIdMng by bcvsolutions.
the class AbstractReadDtoService method toCriteria.
/**
* Constructs find / count jpa criteria from given filter and permissions
*
* @param filter
* @param applyFetchMode fetch related entities in the master select
* @param permission
* @return
*/
protected Specification<E> toCriteria(F filter, boolean applyFetchMode, BasePermission... permission) {
return new Specification<E>() {
private static final long serialVersionUID = 1L;
public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
// if filter is null, no filter predicates will be built
if (filter != null) {
predicates.addAll(AbstractReadDtoService.this.toPredicates(root, query, builder, filter));
}
//
// permissions are not evaluated, if no permission was given
// or authorizable type is null (=> authorization policies are not supported)
BasePermission[] permissions = PermissionUtils.trimNull(permission);
if (!ObjectUtils.isEmpty(permissions) && (AbstractReadDtoService.this instanceof AuthorizableService)) {
AuthorizableType authorizableType = ((AuthorizableService<?>) AbstractReadDtoService.this).getAuthorizableType();
if (authorizableType != null && authorizableType.getType() != null) {
boolean usePermissionOperatorOr = false;
if (filter instanceof PermissionContext) {
PermissionContext permissionContext = (PermissionContext) filter;
usePermissionOperatorOr = permissionContext.usePermissionOperatorOr();
}
if (usePermissionOperatorOr) {
predicates.add(getAuthorizationManager().getPredicateOr(root, query, builder, permissions));
} else {
predicates.add(getAuthorizationManager().getPredicate(root, query, builder, permissions));
}
}
}
//
// check IN predicates limit
predicates.forEach(predicate -> {
checkFilterSizeExceeded(predicate);
});
// include referenced entity in "master" select => reduces number of sub selects
if (applyFetchMode) {
// FIXME: is needed in new hibernate?
// applyFetchMode(root);
}
//
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
};
}
use of org.springframework.data.jpa.domain.Specification in project MyPig by weifuchow.
the class OrdersQuery method where.
public Specification<Orders> where() {
return new Specification<Orders>() {
@Override
public Predicate toPredicate(Root<Orders> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate where = null;
Predicate where1 = null;
if (!StringUtils.isEmpty(getKeyword())) {
where = criteriaBuilder.equal(root.get("userId").as(Integer.class), getKeyword());
where1 = criteriaBuilder.greaterThan(root.get("addressId").as(Integer.class), 0);
//
if (!StringUtils.isEmpty(getStatus())) {
where = criteriaBuilder.and(where, where1);
where1 = criteriaBuilder.equal(root.get("status").as(Integer.class), getStatus());
}
}
return where == null ? null : criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate"))).where(criteriaBuilder.and(where, where1)).getRestriction();
}
};
}
use of org.springframework.data.jpa.domain.Specification in project MyPig by weifuchow.
the class CartQuery method where.
/**
* 自定义查询字段
*/
// public String xxx;
public Specification<Cart> where() {
return new Specification<Cart>() {
@Override
public Predicate toPredicate(Root<Cart> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate where = null;
Predicate where1 = null;
if (!StringUtils.isEmpty(getKeyword())) {
where = criteriaBuilder.equal(root.get("userId").as(Integer.class), getKeyword());
where1 = criteriaBuilder.equal(root.get("status").as(Integer.class), 0);
}
return where == null ? null : criteriaQuery.where(criteriaBuilder.and(where, where1)).getRestriction();
}
};
}
Aggregations