Search in sources :

Example 31 with Predicate

use of javax.persistence.criteria.Predicate in project ice by JBEI.

the class FolderDAO method getFolderContentIds.

/**
     * Retrieves the ids of any entries that are contained in the specified folder; optionally filtered by entry type
     *
     * @param folderId unique folder identifier
     * @param type     optional filter for entries. If null, all entries will be retrieved
     * @return List of entry ids found in the folder with the filter applied if applicable and which have
     * a visibility of "OK"
     */
public List<Long> getFolderContentIds(long folderId, EntryType type, boolean visibleOnly) {
    try {
        CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
        Root<Folder> from = query.from(Folder.class);
        Join<Folder, Entry> entry = from.join("contents");
        ArrayList<Predicate> predicates = new ArrayList<>();
        predicates.add(getBuilder().equal(from.get("id"), folderId));
        if (visibleOnly) {
            predicates.add(getBuilder().equal(entry.get("visibility"), Visibility.OK.getValue()));
        }
        if (type != null) {
            predicates.add(getBuilder().equal(entry.get("recordType"), type.getName()));
        }
        query.select(entry.get("id")).where(predicates.toArray(new Predicate[predicates.size()]));
        return currentSession().createQuery(query).list();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) Predicate(javax.persistence.criteria.Predicate)

Example 32 with Predicate

use of javax.persistence.criteria.Predicate in project ice by JBEI.

the class FolderDAO method getFolderSize.

/**
     * Retrieves the count of the number of contents in the folder.
     * Currently, it is assumed that the contents of folders are only entries. The entries
     * that are counted are those that have a visibility of "OK"
     *
     * @param id          unique folder identifier
     * @param filter      optional filter for entry fields
     * @param visibleOnly if true, counts only entries with visibility "OK"
     * @return number of child contents in the folder
     */
public Long getFolderSize(long id, String filter, boolean visibleOnly) {
    try {
        CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
        Root<Folder> from = query.from(Folder.class);
        Join<Folder, Entry> entry = from.join("contents");
        ArrayList<Predicate> predicates = new ArrayList<>();
        if (filter != null && !filter.trim().isEmpty()) {
            filter = filter.toLowerCase();
            predicates.add(getBuilder().or(getBuilder().like(getBuilder().lower(entry.get("name")), "%" + filter + "%"), getBuilder().like(getBuilder().lower(entry.get("alias")), "%" + filter + "%"), getBuilder().like(getBuilder().lower(entry.get("shortDescription")), "%" + filter + "%"), getBuilder().like(getBuilder().lower(entry.get("partNumber")), "%" + filter + "%")));
        }
        if (visibleOnly) {
            predicates.add(entry.get("visibility").in(Arrays.asList(Visibility.OK.getValue(), Visibility.REMOTE.getValue())));
        }
        predicates.add(getBuilder().equal(from.get("id"), id));
        query.select(getBuilder().countDistinct(entry.get("id")));
        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return currentSession().createQuery(query).uniqueResult();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) Predicate(javax.persistence.criteria.Predicate)

Example 33 with Predicate

use of javax.persistence.criteria.Predicate in project tomee by apache.

the class MoviesBean method findRange.

public List<Movie> findRange(String field, String searchTerm, int firstResult, int maxResults) {
    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);
    Path<String> path = root.get(type.getDeclaredSingularAttribute(field, String.class));
    Predicate condition = qb.like(path, "%" + searchTerm + "%");
    cq.where(condition);
    TypedQuery<Movie> q = entityManager.createQuery(cq);
    q.setMaxResults(maxResults);
    q.setFirstResult(firstResult);
    return q.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 34 with Predicate

use of javax.persistence.criteria.Predicate in project tomee by apache.

the class MoviesImpl method findByStringField.

private List<Movie> findByStringField(final String fieldname, final String param) {
    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    final CriteriaQuery<Movie> query = builder.createQuery(Movie.class);
    final Root<Movie> root = query.from(Movie.class);
    final EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
    final Path<String> path = root.get(type.getDeclaredSingularAttribute(fieldname, String.class));
    final Predicate condition = builder.like(path, "%" + param + "%");
    query.where(condition);
    return entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate)

Example 35 with Predicate

use of javax.persistence.criteria.Predicate in project tomee by apache.

the class QueryProxy method createFinderQuery.

private <T> Query createFinderQuery(final EntityManager entityManager, final String methodName, final Class<T> entityType, final Object[] args) {
    final List<String> conditions = parseMethodName(methodName);
    final EntityType<T> et = entityManager.getMetamodel().entity(entityType);
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object> query = cb.createQuery();
    final Root<T> from = query.from(entityType);
    query = query.select(from);
    int i = 0;
    Predicate where = null;
    for (final String condition : conditions) {
        final SingularAttribute<? super T, ?> attribute = et.getSingularAttribute(condition);
        final Path<?> path = from.get(attribute);
        final Class<?> javaType = attribute.getType().getJavaType();
        final Predicate currentClause;
        if (javaType.equals(String.class)) {
            currentClause = cb.like((Expression<String>) path, (String) args[i++]);
        } else if (Number.class.isAssignableFrom(javaType) || javaType.isPrimitive()) {
            currentClause = cb.equal(path, args[i++]);
        } else {
            LOGGER.warning("field " + condition + " not found, ignoring");
            continue;
        }
        if (where == null) {
            where = currentClause;
        } else {
            where = cb.and(where, currentClause);
        }
    }
    if (where != null) {
        query = query.where(where);
    }
    // pagination
    final TypedQuery<?> emQuery = entityManager.createQuery(query);
    if (args != null && args.length == conditions.size() + 2 && isInt(args[args.length - 2].getClass()) && isInt(args[args.length - 1].getClass())) {
        final int first = (Integer) args[args.length - 2];
        final int max = (Integer) args[args.length - 1];
        emQuery.setFirstResult(first);
        emQuery.setMaxResults(max);
    }
    return emQuery;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Predicate(javax.persistence.criteria.Predicate) Expression(javax.persistence.criteria.Expression)

Aggregations

Predicate (javax.persistence.criteria.Predicate)35 EntityManager (javax.persistence.EntityManager)19 Test (org.junit.Test)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)7 DAOException (org.jbei.ice.storage.DAOException)7 ArrayList (java.util.ArrayList)4 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)2 Map (java.util.Map)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)2 Request (org.jbei.ice.storage.model.Request)2 Function (com.google.common.base.Function)1 Transactional (com.google.inject.persist.Transactional)1 DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1