/** * This method will return the amount of results that would be generated for this CriteriaQuery as an integer * * @param context * The standard DSpace Context object * @param criteriaQuery * The CriteriaQuery for which this result will be retrieved * @param criteriaBuilder * The CriteriaBuilder that accompagnies the CriteriaQuery * @param root * The root that'll determine on which class object we need to calculate the result * @return The amount of results that would be found by this CriteriaQuery as an integer value * @throws SQLException */ public int count(Context context, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder, Root<T> root) throws SQLException { return Math.toIntExact(countLong(context, criteriaQuery, criteriaBuilder, root)); }
@Override public List<T> findMany(Context context, String query) throws SQLException { @SuppressWarnings("unchecked") List<T> result = (List<T>) createQuery(context, query).getResultList(); return result; }
@Override public List<T> findAll(Context context, Class<T> clazz) throws SQLException { CriteriaQuery criteriaQuery = getCriteriaQuery(getCriteriaBuilder(context), clazz); Root<T> root = criteriaQuery.from(clazz); criteriaQuery.select(root); return executeCriteriaQuery(context, criteriaQuery, false, -1, -1); }
/** * Retrieve a single result from the query. Best used if you expect a * single result, but this isn't enforced on the database. * @param criteriaQuery JPA criteria * @return a DAO specified by the criteria */ public T singleResult(Context context, CriteriaQuery criteriaQuery) throws SQLException { Query query = this.getHibernateSession(context).createQuery(criteriaQuery); return singleResult(query); }
@Override public T findByID(Context context, Class clazz, UUID id) throws SQLException { if (id == null) { return null; } @SuppressWarnings("unchecked") T result = (T) getHibernateSession(context).get(clazz, id); return result; }
/** * This method will return the first result from the given query or null if no results were found * * @param query * The query that is to be executed * @return One result from the given query or null if none was found */ public T singleResult(final Query query) { query.setMaxResults(1); List<T> list = list(query); if (CollectionUtils.isNotEmpty(list)) { return list.get(0); } else { return null; } }
/** * This method will return a list with unique results, no duplicates, made by the given CriteriaQuery and parameters * * @param context * The standard DSpace context object * @param criteriaQuery * The CriteriaQuery for which this list will be retrieved * @param cacheable * Whether or not this query should be cacheable * @param clazz * The clazz for which this CriteriaQuery will be executed on * @param maxResults * The maxmimum amount of results that will be returned for this CriteriaQuery * @param offset * The offset to be used for the CriteriaQuery * @return A list of distinct results as depicted by the CriteriaQuery and parameters * @throws SQLException */ public List<T> list(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset) throws SQLException { criteriaQuery.distinct(true); @SuppressWarnings("unchecked") List<T> result = (List<T>) executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset); return result; }
@Override public T findByID(Context context, Class clazz, int id) throws SQLException { @SuppressWarnings("unchecked") T result = (T) getHibernateSession(context).get(clazz, id); return result; }
/** * Retrieve a unique result from the query. If multiple results CAN be * retrieved an exception will be thrown, * so only use when the criteria state uniqueness in the database. * @param criteriaQuery JPA criteria * @return a DAO specified by the criteria */ public T uniqueResult(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset) throws SQLException { List<T> list = list(context, criteriaQuery, cacheable, clazz, maxResults, offset); if (CollectionUtils.isNotEmpty(list)) { if (list.size() == 1) { return list.get(0); } else { throw new IllegalArgumentException("More than one result found"); } } else { return null; } }
/** * This method will return a list of results for the given CriteriaQuery and parameters * * @param context * The standard DSpace context object * @param criteriaQuery * The CriteriaQuery to be used to find the list of results * @param cacheable * A boolean value indicating whether this query should be cached or not * @param clazz * The class on which the CriteriaQuery will search * @param maxResults * The maximum amount of results to be returned * @param offset * The offset to be used for the CriteriaQuery * @param distinct * A boolean value indicating whether this list should be distinct or not * @return A list of results determined by the CriteriaQuery and parameters * @throws SQLException */ public List<T> list(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset, boolean distinct) throws SQLException { criteriaQuery.distinct(distinct); @SuppressWarnings("unchecked") List<T> result = (List<T>) executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset); return result; }
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaQuery<T> criteria = getCriteriaQuery(criteriaBuilder, clazz); Root root = criteria.from(clazz); criteria.select(root); criteria.where(criteriaBuilder.equal(root.get(entry.getKey()), entry.getValue())); return executeCriteriaQuery(context, criteria, cacheable, maxResults, offset);
/** * Create a parsed query from a query expression. * * @param context current DSpace context. * @param query textual form of the query. * @return parsed form of the query. * @throws SQLException */ public Query createQuery(Context context, String query) throws SQLException { return getHibernateSession(context).createQuery(query); }
@Override public T findUnique(Context context, String query) throws SQLException { @SuppressWarnings("unchecked") T result = (T) createQuery(context, query).getSingleResult(); return result; }
@Override public void delete(Context context, T t) throws SQLException { getHibernateSession(context).delete(t); }
/** * This method should always be used in order to retrieve a CriteriaBuilder for the given context * * @param context * The standard DSpace Context class for which a CriteriaBuilder will be made * @return A CriteriaBuilder that can be used to create the query * @throws SQLException */ public CriteriaBuilder getCriteriaBuilder(Context context) throws SQLException { return this.getHibernateSession(context).getCriteriaBuilder(); }
@Override public T create(Context context, T t) throws SQLException { getHibernateSession(context).persist(t); return t; }
/** * This method will return the count of items for this query as a long * * @param context * The standard DSpace Context object * @param criteriaQuery * The CriteriaQuery for which the amount of results will be retrieved * @param criteriaBuilder * The CriteriaBuilder that goes along with this CriteriaQuery * @param root * The root created for a DSpace class on which this query will search * @return A long value that depicts the amount of results this query has found * @throws SQLException */ public long countLong(Context context, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder, Root<T> root) throws SQLException { Expression<Long> countExpression = criteriaBuilder.countDistinct(root); criteriaQuery.select(countExpression); return (Long) this.getHibernateSession(context).createQuery(criteriaQuery).getSingleResult(); }
Query query = this.getHibernateSession(context).createQuery(criteriaQuery);