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