Search in sources :

Example 1 with SimpleExpression

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

the class HibernateConceptDAO method getDrugs.

/**
 * @see org.openmrs.api.db.ConceptDAO#getDrugs(java.lang.String, org.openmrs.Concept, boolean)
 */
@Override
@SuppressWarnings("unchecked")
public List<Drug> getDrugs(String drugName, Concept concept, boolean includeRetired) throws DAOException {
    Criteria searchCriteria = sessionFactory.getCurrentSession().createCriteria(Drug.class, "drug");
    if (!includeRetired) {
        searchCriteria.add(Restrictions.eq("drug.retired", false));
    }
    if (concept != null) {
        searchCriteria.add(Restrictions.eq("drug.concept", concept));
    }
    if (drugName != null) {
        SimpleExpression eq = Restrictions.eq("drug.name", drugName);
        if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) {
            eq = eq.ignoreCase();
        }
        searchCriteria.add(eq);
    }
    return (List<Drug>) searchCriteria.list();
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) SimpleExpression(org.hibernate.criterion.SimpleExpression)

Example 2 with SimpleExpression

use of org.hibernate.criterion.SimpleExpression in project candlepin by candlepin.

the class PoolCurator method lookupOversubscribedBySubscriptionIds.

/**
 * Attempts to find pools which are over subscribed after the creation or
 * modification of the given entitlement. To do this we search for only the
 * pools related to the subscription ID which could have changed, the two
 * cases where this can happen are:
 * 1. Bonus pool (not derived from any entitlement) after a bind. (in cases
 * such as exporting to downstream)
 * 2. A derived pool whose source entitlement just had it's quantity
 * reduced.
 * This has to be done carefully to avoid potential performance problems
 * with virt_bonus on-site subscriptions where one pool is created per
 * physical entitlement.
 *
 * @param ownerId Owner - The owner of the entitlements being passed in. Scoping
 *        this to a single owner prevents performance problems in large datasets.
 * @param subIdMap Map where key is Subscription ID of the pool, and value
 *        is the Entitlement just created or modified.
 * @return Pools with too many entitlements for their new quantity.
 */
@SuppressWarnings("unchecked")
public List<Pool> lookupOversubscribedBySubscriptionIds(String ownerId, Map<String, Entitlement> subIdMap) {
    List<Criterion> subIdMapCriteria = new ArrayList<>();
    Criterion[] exampleCriteria = new Criterion[0];
    for (Entry<String, Entitlement> entry : subIdMap.entrySet()) {
        SimpleExpression subscriptionExpr = Restrictions.eq("sourceSub.subscriptionId", entry.getKey());
        Junction subscriptionJunction = Restrictions.and(subscriptionExpr).add(Restrictions.or(Restrictions.isNull("sourceEntitlement"), Restrictions.eqOrIsNull("sourceEntitlement", entry.getValue())));
        subIdMapCriteria.add(subscriptionJunction);
    }
    return currentSession().createCriteria(Pool.class).createAlias("sourceSubscription", "sourceSub").add(Restrictions.eq("owner.id", ownerId)).add(Restrictions.ge("quantity", 0L)).add(Restrictions.gtProperty("consumed", "quantity")).add(Restrictions.or(subIdMapCriteria.toArray(exampleCriteria))).list();
}
Also used : Criterion(org.hibernate.criterion.Criterion) ArrayList(java.util.ArrayList) Junction(org.hibernate.criterion.Junction) SimpleExpression(org.hibernate.criterion.SimpleExpression)

Example 3 with SimpleExpression

use of org.hibernate.criterion.SimpleExpression 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)

Aggregations

SimpleExpression (org.hibernate.criterion.SimpleExpression)3 ArrayList (java.util.ArrayList)2 List (java.util.List)1 Criteria (org.hibernate.Criteria)1 Conjunction (org.hibernate.criterion.Conjunction)1 Criterion (org.hibernate.criterion.Criterion)1 Junction (org.hibernate.criterion.Junction)1 AdministrationService (org.openmrs.api.AdministrationService)1