Example usage for org.hibernate.criterion Restrictions conjunction

List of usage examples for org.hibernate.criterion Restrictions conjunction

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions conjunction.

Prototype

public static Conjunction conjunction() 

Source Link

Document

Group expressions together in a single conjunction (A and B and C...).

Usage

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

License:Mozilla Public License

/**
 * Creates or that matches the input name with Provider-Person-Names (not voided)
 *
 * @param name/*from  w  ww.  ja v  a2 s  .  c om*/
 * @return Junction
 */
private Junction getNameSearchExpression(String name) {
    MatchMode mode = MatchMode.ANYWHERE;

    Conjunction and = Restrictions.conjunction();
    and.add(Restrictions.eq("personName.voided", false));

    Disjunction or = Restrictions.disjunction();
    and.add(or);

    or.add(Restrictions.ilike("personName.givenName", name, mode));
    or.add(Restrictions.ilike("personName.middleName", name, mode));
    or.add(Restrictions.ilike("personName.familyName", name, mode));
    or.add(Restrictions.ilike("personName.familyName2", name, mode));

    return and;
}

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

License:Mozilla Public License

/**
 * Adds attribute value criteria to the given criteria query
 * /*from  www .  j  a  va2 s . c  om*/
 * @param criteria the criteria
 * @param serializedAttributeValues the serialized attribute values
 * @param <AT> the attribute type
 */
public static <AT extends AttributeType> void addAttributeCriteria(Criteria criteria,
        Map<AT, String> serializedAttributeValues) {
    Conjunction conjunction = Restrictions.conjunction();
    int a = 0;

    for (Map.Entry<AT, String> entry : serializedAttributeValues.entrySet()) {
        String alias = "attributes" + (a++);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Location.class)
                .setProjection(Projections.id());
        detachedCriteria.createAlias("attributes", alias);
        detachedCriteria.add(Restrictions.eq(alias + ".attributeType", entry.getKey()));
        detachedCriteria.add(Restrictions.eq(alias + ".valueReference", entry.getValue()));
        detachedCriteria.add(Restrictions.eq(alias + ".voided", false));

        conjunction.add(Property.forName("id").in(detachedCriteria));
    }

    criteria.add(conjunction);
}

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

License:Mozilla Public License

/**
 * @param query defines search parameters
 * @param matchExactly/*from  ww w.  j a  v a2 s .  c om*/
 * @param orderByNames
 * @param includeVoided true/false whether or not to included voided patients
 * @return criteria for searching by name OR identifier OR searchable attributes
 */
Criteria prepareCriteria(String query, Boolean matchExactly, boolean orderByNames, boolean includeVoided) {
    addAliasForName(criteria, orderByNames);

    if (matchExactly == null) {
        criteria.add(Restrictions.conjunction().add(prepareCriterionForName(query, null, includeVoided))
                .add(Restrictions.not(prepareCriterionForName(query, true, includeVoided)))
                .add(Restrictions.not(prepareCriterionForName(query, false, includeVoided))));
    } else if (!matchExactly) {
        criteria.add(prepareCriterionForName(query, false, includeVoided));
    } else {
        personSearchCriteria.addAliasForAttribute(criteria);
        addAliasForIdentifiers(criteria);

        criteria.add(Restrictions.disjunction().add(prepareCriterionForName(query, true, includeVoided))
                .add(prepareCriterionForAttribute(query, includeVoided)).add(prepareCriterionForIdentifier(
                        query, new ArrayList<PatientIdentifierType>(), false, includeVoided)));
    }

    if (!includeVoided) {
        criteria.add(Restrictions.eq("voided", false));
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

    log.debug(criteria.toString());

    return criteria;
}

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

License:Mozilla Public License

/**
 * Utility method to add identifier expression to an existing criteria
 *
 * @param identifier/*w  ww.  j a  va 2s .  c  o m*/
 * @param identifierTypes
 * @param matchIdentifierExactly
 * @param includeVoided true/false whether or not to included voided patients
 */
private Criterion prepareCriterionForIdentifier(String identifier, List<PatientIdentifierType> identifierTypes,
        boolean matchIdentifierExactly, boolean includeVoided) {

    identifier = HibernateUtil.escapeSqlWildcards(identifier, sessionFactory);
    Conjunction conjunction = Restrictions.conjunction();

    if (!includeVoided) {
        conjunction.add(Restrictions.eq("ids.voided", false));
    }
    // do the identifier restriction
    if (identifier != null) {
        // if the user wants an exact search, match on that.
        if (matchIdentifierExactly) {
            SimpleExpression matchIdentifier = Restrictions.eq("ids.identifier", identifier);
            if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) {
                matchIdentifier.ignoreCase();
            }
            conjunction.add(matchIdentifier);
        } else {
            AdministrationService adminService = Context.getAdministrationService();
            String regex = adminService
                    .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "");
            String patternSearch = adminService
                    .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_PATTERN, "");

            // remove padding from identifier search string
            if (Pattern.matches("^\\^.{1}\\*.*$", regex)) {
                identifier = removePadding(identifier, regex);
            }

            if (org.springframework.util.StringUtils.hasLength(patternSearch)) {
                conjunction.add(splitAndGetSearchPattern(identifier, patternSearch));
            }
            // if the regex is empty, default to a simple "like" search or if
            // we're in hsql world, also only do the simple like search (because
            // hsql doesn't know how to deal with 'regexp'
            else if ("".equals(regex) || HibernateUtil.isHSQLDialect(sessionFactory)) {
                conjunction.add(getCriterionForSimpleSearch(identifier, adminService));
            }
            // if the regex is present, search on that
            else {
                regex = replaceSearchString(regex, identifier);
                conjunction.add(Restrictions.sqlRestriction("identifier regexp ?", regex, StringType.INSTANCE));
            }
        }
    }

    // TODO add a junit test for patientIdentifierType restrictions   

    // do the type restriction
    if (!CollectionUtils.isEmpty(identifierTypes)) {
        criteria.add(Restrictions.in("ids.identifierType", identifierTypes));
    }

    return conjunction;
}

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

License:Mozilla Public License

/**
 * Utility method to add name expressions to criteria.
 *
 * @param name/*from  ww w.j a  va2 s.c om*/
 * @param matchExactly
 * @param includeVoided true/false whether or not to included voided patients
 */
private Criterion prepareCriterionForName(String name, Boolean matchExactly, boolean includeVoided) {
    name = HibernateUtil.escapeSqlWildcards(name, sessionFactory);

    Conjunction conjunction = Restrictions.conjunction();
    String[] nameParts = getQueryParts(name);
    if (nameParts.length > 0) {
        StringBuilder multiName = new StringBuilder(nameParts[0]);

        for (int i = 0; i < nameParts.length; i++) {
            String singleName = nameParts[i];

            if (singleName != null && singleName.length() > 0) {
                Criterion singleNameCriterion = getCriterionForName(singleName, matchExactly, includeVoided);
                Criterion criterion = singleNameCriterion;

                if (i > 0) {
                    multiName.append(" ");
                    multiName.append(singleName);
                    Criterion multiNameCriterion = getCriterionForName(multiName.toString(), matchExactly,
                            includeVoided);
                    criterion = Restrictions.or(singleNameCriterion, multiNameCriterion);
                }

                conjunction.add(criterion);
            }
        }
    }

    return conjunction;
}

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

License:Mozilla Public License

private Criterion getCriterionForShortName(String name, boolean includeVoided) {
    Criterion criterion = Restrictions.disjunction()
            .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.givenName"))
                    .add(Restrictions.eq("name.givenName", name).ignoreCase()))
            .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.middleName"))
                    .add(Restrictions.eq("name.middleName", name).ignoreCase()))
            .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.familyName"))
                    .add(Restrictions.eq("name.familyName", name).ignoreCase()))
            .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.familyName2"))
                    .add(Restrictions.eq("name.familyName2", name).ignoreCase()));

    if (!includeVoided) {
        return Restrictions.conjunction().add(Restrictions.eq("name.voided", false)).add(criterion);
    }/*from  w ww  .  ja v a2  s .  com*/
    return criterion;
}

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

License:Mozilla Public License

private Criterion getCriterionForLongName(String name, boolean includeVoided) {
    MatchMode matchMode = getMatchMode();
    Criterion criterion = Restrictions.disjunction().add(Restrictions.like("name.givenName", name, matchMode))
            .add(Restrictions.like("name.middleName", name, matchMode))
            .add(Restrictions.like("name.familyName", name, matchMode))
            .add(Restrictions.like("name.familyName2", name, matchMode));

    if (!includeVoided) {
        return Restrictions.conjunction().add(Restrictions.eq("name.voided", false)).add(criterion);
    }//from  ww w .j  a  va2  s  . c o  m
    return criterion;
}

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

License:Mozilla Public License

private Criterion getCriterionForNoExactName(String name, boolean includeVoided) {
    MatchMode matchMode = getMatchMode();

    Criterion criterion = Restrictions.conjunction()
            .add(Restrictions.disjunction()
                    .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.givenName"))
                            .add(Restrictions.like("name.givenName", name, matchMode)))
                    .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.middleName"))
                            .add(Restrictions.like("name.middleName", name, matchMode)))
                    .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.familyName"))
                            .add(Restrictions.like("name.familyName", name, matchMode)))
                    .add(Restrictions.conjunction().add(Restrictions.isNotNull("name.familyName2"))
                            .add(Restrictions.like("name.familyName2", name, matchMode))))
            .add(Restrictions.disjunction().add(Restrictions.isNull("name.givenName"))
                    .add(Restrictions.ne("name.givenName", name)))
            .add(Restrictions.disjunction().add(Restrictions.isNull("name.middleName"))
                    .add(Restrictions.ne("name.middleName", name)))
            .add(Restrictions.disjunction().add(Restrictions.isNull("name.familyName"))
                    .add(Restrictions.ne("name.familyName", name)))
            .add(Restrictions.disjunction().add(Restrictions.isNull("name.familyName2"))
                    .add(Restrictions.ne("name.familyName2", name)));

    if (!includeVoided) {
        return Restrictions.conjunction().add(Restrictions.eq("name.voided", false)).add(criterion);
    }//from   w  w w  . java  2s.c o m
    return criterion;
}

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

License:Mozilla Public License

private Criterion prepareCriterionForAttribute(String query, boolean includeVoided) {
    query = HibernateUtil.escapeSqlWildcards(query, sessionFactory);

    Conjunction conjunction = Restrictions.conjunction();
    MatchMode matchMode = personSearchCriteria.getAttributeMatchMode();

    String[] queryParts = getQueryParts(query);
    for (String queryPart : queryParts) {
        conjunction.add(personSearchCriteria.prepareCriterionForAttribute(queryPart, includeVoided, matchMode));
    }//  w  ww. j  a v a  2  s.  co  m

    return conjunction;
}

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

License:Mozilla Public License

Criterion prepareCriterionForAttribute(String value, Boolean voided, MatchMode matchMode) {
    if (voided == null || !voided) {
        return Restrictions.conjunction().add(Restrictions.eq("attributeType.searchable", true))
                .add(Restrictions.eq("attribute.voided", false))
                .add(Restrictions.ilike("attribute.value", value, matchMode));
    } else {/*from  w w  w.j ava 2 s.  c  o  m*/
        return Restrictions.conjunction().add(Restrictions.eq("attributeType.searchable", true))
                .add(Restrictions.ilike("attribute.value", value, matchMode));
    }
}