use of javax.persistence.criteria.Predicate in project tomee by apache.
the class MoviesBean method getMovies.
public List<Movie> getMovies(Integer firstResult, Integer maxResults, String field, String searchTerm) {
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Movie> cq = qb.createQuery(Movie.class);
Root<Movie> root = cq.from(Movie.class);
EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
cq.where(condition);
}
TypedQuery<Movie> q = entityManager.createQuery(cq);
if (maxResults != null) {
q.setMaxResults(maxResults);
}
if (firstResult != null) {
q.setFirstResult(firstResult);
}
return q.getResultList();
}
use of javax.persistence.criteria.Predicate in project tomee by apache.
the class MoviesBean method count.
public int count(String field, String searchTerm) {
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = qb.createQuery(Long.class);
Root<Movie> root = cq.from(Movie.class);
EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
Path<String> path = root.get(type.getDeclaredSingularAttribute(field, String.class));
Predicate condition = qb.like(path, "%" + searchTerm + "%");
cq.select(qb.count(root));
cq.where(condition);
return entityManager.createQuery(cq).getSingleResult().intValue();
}
use of javax.persistence.criteria.Predicate in project ArachneCentralAPI by OHDSI.
the class PaperSpecification method toPredicate.
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
final Path<Long> id = root.get(Paper_.id);
final Path<Study> study = root.get(Paper_.study);
final Path<Long> studyId = study.get(Study_.id);
final Path<String> studyTitle = study.get(Study_.title);
final Path<PublishState> publishState = root.get(Paper_.publishState);
Expression<List> followers = root.get(Paper_.followers);
List<Predicate> predicates = new ArrayList<>();
predicates.add(getAdditionalPredicates(root, query, cb, studyId, id));
if (criteria.getPublishState() != null) {
predicates.add(cb.equal(publishState, criteria.getPublishState()));
}
if (criteria.getFavourite() != null) {
predicates.add(criteria.getFavourite() ? cb.isMember(user, followers) : cb.isNotMember(user, followers));
}
if (criteria.getQuery() != null) {
predicates.add(cb.like(cb.lower(studyTitle), getLowerPostfixPrefixLikeForm(criteria.getQuery().toLowerCase())));
}
final String sortBy = criteria.getSortBy();
if (sortBy != null) {
boolean sortAsc = criteria.getSortAsc();
Expression<String> path;
switch(sortBy) {
case FAVOURITE:
path = cb.size(followers).as(String.class);
sortAsc = !sortAsc;
break;
default:
Path relative = root;
for (String field : sortBy.split("\\.")) {
relative = relative.get(field);
}
path = relative;
}
Order order;
if (sortAsc) {
order = cb.asc(path);
} else {
order = cb.desc(path);
}
query.orderBy(order);
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
use of javax.persistence.criteria.Predicate in project ArachneCentralAPI by OHDSI.
the class PaperSpecification method getUserStudyLinkSubquery.
protected Subquery<UserStudyExtended> getUserStudyLinkSubquery(CriteriaQuery<?> query, CriteriaBuilder cb, Path<Long> studyId) {
final Subquery<UserStudyExtended> userStudyExtendedLinkSubquery = query.subquery(UserStudyExtended.class);
final Root<UserStudyExtended> userStudyLinkRoot = userStudyExtendedLinkSubquery.from(UserStudyExtended.class);
userStudyExtendedLinkSubquery.select(userStudyLinkRoot);
final Path<User> linkUser = userStudyLinkRoot.get(UserStudyExtended_.user);
final Path<Study> linkStudy = userStudyLinkRoot.get(UserStudyExtended_.study);
final Path<Long> linkStudyId = linkStudy.get(Study_.id);
final Path<ParticipantStatus> linkStatus = userStudyLinkRoot.get(UserStudyExtended_.status);
Predicate userStudyPredicate = cb.and(cb.equal(linkUser, user), cb.equal(linkStudyId, studyId));
userStudyPredicate = cb.and(userStudyPredicate, cb.notEqual(linkStatus, ParticipantStatus.DELETED));
userStudyExtendedLinkSubquery.where(userStudyPredicate);
return userStudyExtendedLinkSubquery;
}
use of javax.persistence.criteria.Predicate in project ArachneCentralAPI by OHDSI.
the class BaseDataSourceServiceImpl method suggestDataSource.
@Override
public Page<DS> suggestDataSource(final String query, final Long studyId, final Long userId, PageRequest pageRequest) {
List<DataSourceStatus> BAD_STATUSES = Arrays.asList(DataSourceStatus.DELETED, DataSourceStatus.DECLINED);
final String[] split = query.trim().split(" ");
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
CriteriaQuery<DS> cq = cb.createQuery(getType());
Root<DS> root = cq.from(getType());
Subquery sq = cq.subquery(Long.class);
Root<StudyDataSourceLink> dsLink = sq.from(StudyDataSourceLink.class);
sq.select(dsLink.get("dataSource").get("id"));
sq.where(cb.and(cb.equal(dsLink.get("study").get("id"), studyId), cb.not(dsLink.get("status").in(BAD_STATUSES))));
cq.select(root);
// TRUE
Predicate nameClause = cb.conjunction();
if (split.length > 1 || (split.length == 1 && !split[0].equals(""))) {
List<Predicate> predictList = new ArrayList<>();
for (String one : split) {
predictList.add(cb.like(cb.lower(root.get("name")), one + "%"));
predictList.add(cb.like(cb.lower(root.get("dataNode").get("name")), one + "%"));
}
nameClause = cb.or(predictList.toArray(new Predicate[] {}));
}
cq.where(cb.and(cb.not(root.get("id").in(sq)), nameClause, cb.isNull(root.get("deleted")), cb.isTrue(root.get("published")), cb.isFalse(root.get("dataNode").get("virtual"))));
TypedQuery<DS> typedQuery = this.entityManager.createQuery(cq);
List<DS> list = typedQuery.setFirstResult(pageRequest.getOffset()).setMaxResults(pageRequest.getPageSize()).getResultList();
return new PageImpl<>(list, pageRequest, list.size());
}
Aggregations