Search in sources :

Example 6 with Conjunction

use of org.hibernate.criterion.Conjunction in project openmrs-core by openmrs.

the class HibernateProviderDAO method prepareProviderCriteria.

/**
 * Creates a Provider Criteria based on name
 *
 * @param name represents provider name
 * @param includeRetired
 * @return Criteria represents the hibernate criteria to search
 */
private Criteria prepareProviderCriteria(String name, boolean includeRetired) {
    if (StringUtils.isBlank(name)) {
        name = "%";
    }
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Provider.class).createAlias("person", "p", JoinType.LEFT_OUTER_JOIN);
    if (!includeRetired) {
        criteria.add(Restrictions.eq("retired", false));
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.createAlias("p.names", "personName", JoinType.LEFT_OUTER_JOIN);
    Disjunction or = Restrictions.disjunction();
    or.add(Restrictions.ilike("identifier", name, getMatchMode()));
    or.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
    Conjunction and = Restrictions.conjunction();
    or.add(and);
    String[] splitNames = name.split(" ");
    for (String splitName : splitNames) {
        and.add(getNameSearchExpression(splitName));
    }
    criteria.add(or);
    return criteria;
}
Also used : Disjunction(org.hibernate.criterion.Disjunction) Conjunction(org.hibernate.criterion.Conjunction) Criteria(org.hibernate.Criteria) Provider(org.openmrs.Provider)

Example 7 with Conjunction

use of org.hibernate.criterion.Conjunction in project openmrs-core by openmrs.

the class PatientSearchCriteria method prepareCriterionForAttribute.

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));
    }
    return conjunction;
}
Also used : MatchMode(org.hibernate.criterion.MatchMode) Conjunction(org.hibernate.criterion.Conjunction)

Example 8 with Conjunction

use of org.hibernate.criterion.Conjunction in project openmrs-core by openmrs.

the class PatientSearchCriteria method prepareCriterionForIdentifier.

/**
 * Utility method to add identifier expression to an existing criteria
 *
 * @param identifier
 * @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));
            } else // hsql doesn't know how to deal with 'regexp'
            if ("".equals(regex) || HibernateUtil.isHSQLDialect(sessionFactory)) {
                conjunction.add(getCriterionForSimpleSearch(identifier, adminService));
            } else // if the regex is present, search on that
            {
                regex = replaceSearchString(regex, identifier);
                conjunction.add(Restrictions.sqlRestriction("identifier regexp ?", regex, StringType.INSTANCE));
            }
        }
    }
    // do the type restriction
    if (!CollectionUtils.isEmpty(identifierTypes)) {
        criteria.add(Restrictions.in("ids.identifierType", identifierTypes));
    }
    return conjunction;
}
Also used : AdministrationService(org.openmrs.api.AdministrationService) Conjunction(org.hibernate.criterion.Conjunction) SimpleExpression(org.hibernate.criterion.SimpleExpression)

Example 9 with Conjunction

use of org.hibernate.criterion.Conjunction in project openmrs-core by openmrs.

the class PatientSearchCriteria method prepareCriterionForName.

/**
 * Utility method to add name expressions to criteria.
 *
 * @param name
 * @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;
}
Also used : Criterion(org.hibernate.criterion.Criterion) Conjunction(org.hibernate.criterion.Conjunction)

Example 10 with Conjunction

use of org.hibernate.criterion.Conjunction in project openmrs-core by openmrs.

the class HibernateEncounterDAO method createEncounterByQueryCriteria.

/**
 * Utility method that returns a criteria for searching for patient encounters that match the
 * specified search phrase
 *
 * @param query patient name or identifier
 * @param patientId the patient id
 * @param includeVoided Specifies whether voided encounters should be included
 * @param orderByNames specifies whether the encounters should be ordered by person names
 * @return Criteria
 */
private Criteria createEncounterByQueryCriteria(String query, Integer patientId, boolean includeVoided, boolean orderByNames) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Encounter.class, "enc");
    if (!includeVoided) {
        criteria.add(Restrictions.eq("enc.voided", false));
    }
    criteria = criteria.createCriteria("patient", "pat");
    if (patientId != null) {
        criteria.add(Restrictions.eq("pat.patientId", patientId));
        if (StringUtils.isNotBlank(query)) {
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            // match on location.name, encounterType.name, form.name
            // provider.name, provider.identifier, provider.person.names
            MatchMode mode = MatchMode.ANYWHERE;
            criteria.createAlias("enc.location", "loc");
            criteria.createAlias("enc.encounterType", "encType");
            criteria.createAlias("enc.form", "form");
            criteria.createAlias("enc.encounterProviders", "enc_prov");
            criteria.createAlias("enc_prov.provider", "prov");
            criteria.createAlias("prov.person", "person", JoinType.LEFT_OUTER_JOIN);
            criteria.createAlias("person.names", "personName", JoinType.LEFT_OUTER_JOIN);
            Disjunction or = Restrictions.disjunction();
            or.add(Restrictions.ilike("loc.name", query, mode));
            or.add(Restrictions.ilike("encType.name", query, mode));
            or.add(Restrictions.ilike("form.name", query, mode));
            or.add(Restrictions.ilike("prov.name", query, mode));
            or.add(Restrictions.ilike("prov.identifier", query, mode));
            String[] splitNames = query.split(" ");
            Disjunction nameOr = Restrictions.disjunction();
            for (String splitName : splitNames) {
                nameOr.add(Restrictions.ilike("personName.givenName", splitName, mode));
                nameOr.add(Restrictions.ilike("personName.middleName", splitName, mode));
                nameOr.add(Restrictions.ilike("personName.familyName", splitName, mode));
                nameOr.add(Restrictions.ilike("personName.familyName2", splitName, mode));
            }
            // OUTPUT for provider criteria:
            // prov.name like '%query%' OR prov.identifier like '%query%'
            // OR ( personName.voided = false
            // AND (  personName.givenName like '%query%'
            // OR personName.middleName like '%query%'
            // OR personName.familyName like '%query%'
            // OR personName.familyName2 like '%query%'
            // )
            // )
            Conjunction personNameConjuction = Restrictions.conjunction();
            personNameConjuction.add(Restrictions.eq("personName.voided", false));
            personNameConjuction.add(nameOr);
            or.add(personNameConjuction);
            criteria.add(or);
        }
    } else {
        // As identifier could be all alpha, no heuristic here will work in determining intent of user for querying by name versus identifier
        // So search by both!
        criteria = new PatientSearchCriteria(sessionFactory, criteria).prepareCriteria(query, query, new ArrayList<>(), true, orderByNames, true);
    }
    return criteria;
}
Also used : MatchMode(org.hibernate.criterion.MatchMode) Disjunction(org.hibernate.criterion.Disjunction) Conjunction(org.hibernate.criterion.Conjunction) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) EncounterSearchCriteria(org.openmrs.parameter.EncounterSearchCriteria)

Aggregations

Conjunction (org.hibernate.criterion.Conjunction)10 Disjunction (org.hibernate.criterion.Disjunction)3 MatchMode (org.hibernate.criterion.MatchMode)3 Criteria (org.hibernate.Criteria)2 Criterion (org.hibernate.criterion.Criterion)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JobStatus (org.candlepin.pinsetter.core.model.JobStatus)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 SimpleExpression (org.hibernate.criterion.SimpleExpression)1 QueryParserException (org.hisp.dhis.query.QueryParserException)1 QueryPath (org.hisp.dhis.query.planner.QueryPath)1 Property (org.hisp.dhis.schema.Property)1 Schema (org.hisp.dhis.schema.Schema)1 Location (org.openmrs.Location)1 Provider (org.openmrs.Provider)1 AdministrationService (org.openmrs.api.AdministrationService)1 EncounterSearchCriteria (org.openmrs.parameter.EncounterSearchCriteria)1