Search in sources :

Example 26 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 27 with Predicate

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 28 with Predicate

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()]));
}
Also used : Path(javax.persistence.criteria.Path) Order(javax.persistence.criteria.Order) Study(com.odysseusinc.arachne.portal.model.Study) ArrayList(java.util.ArrayList) PublishState(com.odysseusinc.arachne.portal.model.PublishState) Predicate(javax.persistence.criteria.Predicate) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with Predicate

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;
}
Also used : Study(com.odysseusinc.arachne.portal.model.Study) IUser(com.odysseusinc.arachne.portal.model.IUser) User(com.odysseusinc.arachne.portal.model.User) ParticipantStatus(com.odysseusinc.arachne.portal.model.ParticipantStatus) UserStudyExtended(com.odysseusinc.arachne.portal.model.UserStudyExtended) Predicate(javax.persistence.criteria.Predicate)

Example 30 with Predicate

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());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) StudyDataSourceLink(com.odysseusinc.arachne.portal.model.StudyDataSourceLink) ArrayList(java.util.ArrayList) Subquery(javax.persistence.criteria.Subquery) Predicate(javax.persistence.criteria.Predicate) DataSourceStatus(com.odysseusinc.arachne.portal.model.DataSourceStatus)

Aggregations

Predicate (javax.persistence.criteria.Predicate)59 EntityManager (javax.persistence.EntityManager)19 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)19 Test (org.junit.Test)17 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 ArrayList (java.util.ArrayList)9 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)6 DAOException (org.jbei.ice.storage.DAOException)6 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)5 Root (javax.persistence.criteria.Root)5 LinkedList (java.util.LinkedList)3 List (java.util.List)3 TypedQuery (javax.persistence.TypedQuery)3 Study (com.odysseusinc.arachne.portal.model.Study)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)2 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)2 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)2 IdmRoleCatalogueRole (eu.bcvsolutions.idm.core.model.entity.IdmRoleCatalogueRole)2 IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)2