Example usage for org.hibernate.criterion Subqueries eq

List of usage examples for org.hibernate.criterion Subqueries eq

Introduction

In this page you can find the example usage for org.hibernate.criterion Subqueries eq.

Prototype

public static Criterion eq(Object value, DetachedCriteria dc) 

Source Link

Document

Creates a criterion which checks that the value of a given literal as being equal to the value in the subquery result.

Usage

From source file:com.qcadoo.model.api.search.SearchSubqueries.java

License:Open Source License

public static SearchCriterion eq(final Object value, final SearchCriteriaBuilder criteria) {
    return new SearchCriterionImpl(
            Subqueries.eq(value, ((SearchCriteria) criteria).getHibernateDetachedCriteria()));
}

From source file:de.escidoc.core.aa.business.filter.RoleFilter.java

License:Open Source License

/**
 * Evaluate a CQL term node.//from w w w  .j ava  2s .c o m
 *
 * @param node CQL node
 * @return Hibernate query reflecting the given CQL query
 * @throws InvalidSearchQueryException thrown if the given search query could not be translated into a SQL query
 */
@Override
protected Criterion evaluate(final CQLTermNode node) throws InvalidSearchQueryException {
    Criterion result = null;
    final Object[] parts = criteriaMap.get(node.getIndex());
    final String value = node.getTerm();

    if (parts != null && !specialCriteriaNames.contains(node.getIndex())) {
        result = evaluate(node.getRelation(), (String) parts[1], value, (Integer) parts[0] == COMPARE_LIKE);
    } else {
        final String columnName = node.getIndex();

        if (columnName != null) {
            if ("limited".equals(columnName)) {
                result = Boolean.parseBoolean(value) ? Restrictions.isNotEmpty("scopeDefs")
                        : Restrictions.isEmpty("scopeDefs");
            } else if ("granted".equals(columnName)) {
                final DetachedCriteria subQuery = DetachedCriteria.forClass(RoleGrant.class, "rg");

                subQuery.setProjection(Projections.rowCount());
                subQuery.add(Restrictions.eqProperty("escidocRole.id", "r.id"));

                result = Boolean.parseBoolean(value) ? Subqueries.lt(0L, subQuery)
                        : Subqueries.eq(0L, subQuery);
            } else if (columnName.equals(Constants.FILTER_CREATION_DATE)
                    || columnName.equals(Constants.FILTER_PATH_CREATION_DATE)) {
                result = evaluate(node.getRelation(), "creationDate",
                        value != null && value.length() > 0 ? new Date(new DateTime(value).getMillis()) : null,
                        false);
            } else {
                throw new InvalidSearchQueryException("unknown filter criteria: " + columnName);
            }
        }
    }
    return result;
}

From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateEscidocRoleDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *
 * @see EscidocRoleDaoInterface #retrieveRoles(java.util.Map, int, int, java.lang.String,
 *      de.escidoc.core.common.util.list.ListSorting)
 *//* ww w  .ja  v  a  2s . c  o  m*/
@Override
public List<EscidocRole> retrieveRoles(final Map<String, Object> criterias, final int offset,
        final int maxResults, final String orderBy, final ListSorting sorting)
        throws SqlDatabaseSystemException {

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EscidocRole.class, "r");
    detachedCriteria.add(Restrictions.ne("id", EscidocRole.DEFAULT_USER_ROLE_ID));

    if (criterias != null && !criterias.isEmpty()) {
        // ids
        final Set<String> roleIds = mergeSets((Set<String>) criterias.remove(Constants.DC_IDENTIFIER_URI),
                (Set<String>) criterias.remove(Constants.FILTER_PATH_ID));
        if (roleIds != null && !roleIds.isEmpty()) {
            detachedCriteria.add(Restrictions.in("id", roleIds.toArray()));
        }

        // limited
        final String limited = (String) criterias.remove("limited");
        if (limited != null) {
            if (Boolean.parseBoolean(limited)) {
                detachedCriteria.add(Restrictions.isNotEmpty("scopeDefs"));
            } else {
                detachedCriteria.add(Restrictions.isEmpty("scopeDefs"));
            }
        }

        // granted
        final String granted = (String) criterias.remove("granted");
        if (granted != null) {
            final DetachedCriteria subQuery = DetachedCriteria.forClass(RoleGrant.class, "rg");
            subQuery.setProjection(Projections.rowCount());
            subQuery.add(Restrictions.eqProperty("escidocRole.id", "r.id"));

            if (Boolean.parseBoolean(granted)) {
                detachedCriteria.add(Subqueries.lt(0, subQuery));
            } else {
                detachedCriteria.add(Subqueries.eq(0, subQuery));
            }
        }

        for (final Entry<String, Object[]> stringEntry : criteriaMap.entrySet()) {
            final Object criteriaValue = criterias.remove(stringEntry.getKey());
            if (criteriaValue != null) {
                final Object[] parts = stringEntry.getValue();
                if (parts[0].equals(COMPARE_EQ)) {
                    detachedCriteria.add(Restrictions.eq((String) parts[1], criteriaValue));
                } else {
                    detachedCriteria.add(Restrictions.like((String) parts[1], criteriaValue));
                }
            }
        }
    }

    if (orderBy != null) {
        if (sorting == ListSorting.ASCENDING) {
            detachedCriteria.addOrder(Order.asc(propertiesNamesMap.get(orderBy)));
        } else if (sorting == ListSorting.DESCENDING) {
            detachedCriteria.addOrder(Order.desc(propertiesNamesMap.get(orderBy)));
        }
    }

    if (criterias != null && criterias.isEmpty()) {

        final List<EscidocRole> result;
        try {
            result = getHibernateTemplate().findByCriteria(detachedCriteria, offset, maxResults);
        } catch (final DataAccessException e) {
            throw new SqlDatabaseSystemException(e);
        }

        return result;
    } else {
        // unsupported filter criteria has been found, therefore the result
        // list must be empty.
        return new ArrayList<EscidocRole>(0);
    }
}

From source file:eu.cloud4soa.relational.persistence.BreachRepository.java

License:Apache License

public List<Breach> retrieveAllForOffering(String offeringId) {
    DetachedCriteria pass_criteria = DetachedCriteria.forClass(Paas.class)
            .add(Restrictions.eq("url", offeringId));

    DetachedCriteria acc_criteria = DetachedCriteria.forClass(Account.class)
            .add(Property.forName("paas").in(pass_criteria));

    DetachedCriteria app_criteria = DetachedCriteria.forClass(ApplicationInstance.class)
            .add(Property.forName("account").in(acc_criteria)).setProjection(Projections.property("id"));

    try {//from   www.ja  v a2  s .  c  o m
        return (List<Breach>) getSession().createCriteria(Breach.class)
                .add(Subqueries.eq("applicationInstanceUriId", app_criteria)).list();
    } catch (Exception e) {
        return new ArrayList<Breach>();
    }
}

From source file:org.openmrs.api.db.hibernate.HibernateFormDAO.java

License:Mozilla Public License

/**
 * Convenience method to create the same hibernate criteria object for both getForms and
 * getFormCount/*ww w. jav  a2s  . c o  m*/
 *
 * @param partialName
 * @param published
 * @param encounterTypes
 * @param retired
 * @param containingAnyFormField
 * @param containingAllFormFields
 * @param fields
 * @return
 */
private Criteria getFormCriteria(String partialName, Boolean published,
        Collection<EncounterType> encounterTypes, Boolean retired, Collection<FormField> containingAnyFormField,
        Collection<FormField> containingAllFormFields, Collection<Field> fields) {

    Criteria crit = sessionFactory.getCurrentSession().createCriteria(Form.class, "form");

    if (StringUtils.isNotEmpty(partialName)) {
        crit.add(Restrictions.or(Restrictions.like("name", partialName, MatchMode.START),
                Restrictions.like("name", " " + partialName, MatchMode.ANYWHERE)));
    }
    if (published != null) {
        crit.add(Restrictions.eq("published", published));
    }

    if (!encounterTypes.isEmpty()) {
        crit.add(Restrictions.in("encounterType", encounterTypes));
    }

    if (retired != null) {
        crit.add(Restrictions.eq("retired", retired));
    }

    // TODO junit test
    if (!containingAnyFormField.isEmpty()) {
        // Convert form field persistents to integers
        Set<Integer> anyFormFieldIds = new HashSet<Integer>();
        for (FormField ff : containingAnyFormField) {
            anyFormFieldIds.add(ff.getFormFieldId());
        }

        DetachedCriteria subquery = DetachedCriteria.forClass(FormField.class, "ff");
        subquery.setProjection(Projections.property("ff.form.formId"));
        subquery.add(Restrictions.in("ff.formFieldId", anyFormFieldIds));
        crit.add(Subqueries.propertyIn("form.formId", subquery));
    }

    //select * from form where len(containingallformfields) = (select count(*) from form_field ff where ff.form_id = form_id and form_field_id in (containingallformfields);
    if (!containingAllFormFields.isEmpty()) {

        // Convert form field persistents to integers
        Set<Integer> allFormFieldIds = new HashSet<Integer>();
        for (FormField ff : containingAllFormFields) {
            allFormFieldIds.add(ff.getFormFieldId());
        }
        DetachedCriteria subquery = DetachedCriteria.forClass(FormField.class, "ff");
        subquery.setProjection(Projections.count("ff.formFieldId"));
        subquery.add(Restrictions.eqProperty("ff.form", "form"));
        subquery.add(Restrictions.in("ff.formFieldId", allFormFieldIds));

        crit.add(Subqueries.eq(Long.valueOf(containingAllFormFields.size()), subquery));
    }

    // get all forms (dupes included) that have this field on them
    if (!fields.isEmpty()) {
        Criteria crit2 = crit.createCriteria("formFields", "ff");
        crit2.add(Restrictions.eqProperty("ff.form.formId", "form.formId"));
        crit2.add(Restrictions.in("ff.field", fields));
    }

    return crit;
}

From source file:org.openmrs.api.db.hibernate.HibernateLocationDAO.java

License:Mozilla Public License

/**
 * @see org.openmrs.api.db.LocationDAO#getLocationsHavingAllTags(java.util.List)
 *///www . j  ava 2s.  com
@Override
public List<Location> getLocationsHavingAllTags(List<LocationTag> tags) {
    tags.removeAll(Collections.singleton(null));

    DetachedCriteria numberOfMatchingTags = DetachedCriteria.forClass(Location.class, "alias")
            .createAlias("alias.tags", "locationTag")
            .add(Restrictions.in("locationTag.locationTagId", getLocationTagIds(tags)))
            .setProjection(Projections.rowCount())
            .add(Restrictions.eqProperty("alias.locationId", "outer.locationId"));

    return sessionFactory.getCurrentSession().createCriteria(Location.class, "outer")
            .add(Restrictions.eq("retired", false))
            .add(Subqueries.eq(new Long(tags.size()), numberOfMatchingTags)).list();
}