Search in sources :

Example 41 with Predicate

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

the class AccountDAO method getMatchingAccounts.

/**
     * Retrieves accounts whose firstName, lastName, or email fields match the specified
     * token up to the specified limit.
     *
     * @param token filter for the account fields
     * @param limit maximum number of matching accounts to return; 0 to return all
     * @return list of matching accounts
     */
public List<Account> getMatchingAccounts(String token, int limit) {
    try {
        CriteriaQuery<Account> query = getBuilder().createQuery(Account.class);
        Root<Account> from = query.from(Account.class);
        String[] tokens = token.split("\\s+");
        List<Predicate> predicates = new ArrayList<>();
        for (String tok : tokens) {
            tok = tok.toLowerCase();
            predicates.add(getBuilder().or(getBuilder().like(getBuilder().lower(from.get("firstName")), "%" + tok + "%"), getBuilder().like(getBuilder().lower(from.get("lastName")), "%" + tok + "%"), getBuilder().like(getBuilder().lower(from.get("email")), "%" + tok + "%")));
        }
        query.where(predicates.toArray(new Predicate[predicates.size()])).distinct(true);
        return currentSession().createQuery(query).setMaxResults(limit).list();
    } catch (HibernateException e) {
        Logger.error(e);
        throw new DAOException(e);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) Account(org.jbei.ice.storage.model.Account) HibernateException(org.hibernate.HibernateException) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Example 42 with Predicate

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

the class RequestDAO method getCount.

public int getCount(SampleRequestStatus status, String filter) {
    try {
        CriteriaQuery<Long> query = getBuilder().createQuery(Long.class);
        Root<Request> from = query.from(Request.class);
        query.select(getBuilder().countDistinct(from.get("id")));
        List<Predicate> predicates = createPredicates(from, filter, status);
        if (!predicates.isEmpty())
            query.where(predicates.toArray(new Predicate[predicates.size()]));
        return currentSession().createQuery(query).uniqueResult().intValue();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) Request(org.jbei.ice.storage.model.Request) SampleRequest(org.jbei.ice.lib.dto.sample.SampleRequest) Predicate(javax.persistence.criteria.Predicate)

Example 43 with Predicate

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

the class RequestDAO method get.

public List<Request> get(int start, int limit, String sort, boolean asc, SampleRequestStatus status, String filter) {
    try {
        CriteriaQuery<Request> query = getBuilder().createQuery(Request.class).distinct(true);
        Root<Request> from = query.from(Request.class);
        List<Predicate> predicates = createPredicates(from, filter, status);
        if (!predicates.isEmpty())
            query.where(predicates.toArray(new Predicate[predicates.size()]));
        query.orderBy(asc ? getBuilder().asc(from.get(sort)) : getBuilder().desc(from.get(sort)));
        return currentSession().createQuery(query).setMaxResults(limit).setFirstResult(start).list();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : DAOException(org.jbei.ice.storage.DAOException) HibernateException(org.hibernate.HibernateException) Request(org.jbei.ice.storage.model.Request) SampleRequest(org.jbei.ice.lib.dto.sample.SampleRequest) Predicate(javax.persistence.criteria.Predicate)

Example 44 with Predicate

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

the class FolderDAO method retrieveFolderContents.

/**
     * Retrieves list of entries that conforms to the parameters
     *
     * @param folderId       unique identifier for folder whose entries are being retrieved
     * @param pageParameters paging params
     * @param visibleOnly    whether to only include entries with "OK" visibility
     * @return list of found entries
     * @throws DAOException on HibernateException retrieving
     */
public List<Entry> retrieveFolderContents(long folderId, PageParameters pageParameters, boolean visibleOnly) {
    try {
        String sortString;
        switch(pageParameters.getSortField()) {
            default:
            case CREATED:
                sortString = "id";
                break;
            case STATUS:
                sortString = "status";
                break;
            case NAME:
                sortString = "name";
                break;
            case PART_ID:
                sortString = "partNumber";
                break;
            case TYPE:
                sortString = "recordType";
                break;
        }
        CriteriaQuery<Entry> query = getBuilder().createQuery(Entry.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));
        String filter = pageParameters.getFilter();
        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())));
        }
        query.select(entry).where(predicates.toArray(new Predicate[predicates.size()]));
        query.orderBy(pageParameters.isAscending() ? getBuilder().asc(entry.get(sortString)) : getBuilder().desc(entry.get(sortString)));
        return currentSession().createQuery(query).setFirstResult(pageParameters.getOffset()).setMaxResults(pageParameters.getLimit()).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 45 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)

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