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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations