Search in sources :

Example 11 with PoolFilterBuilder

use of org.candlepin.model.PoolFilterBuilder in project candlepin by candlepin.

the class CriteriaRules method availableEntitlementCriteria.

/**
 * Create a List of JPA criterion that can filter out pools that are not
 * applicable to consumer. Helps to scale down large numbers of pools
 * specifically with virt_limit subscriptions.
 *
 * @param consumer The consumer we are filtering pools for
 * @return List of Criterion
 */
@SuppressWarnings("checkstyle:indentation")
public List<Criterion> availableEntitlementCriteria(Consumer consumer) {
    // avoid passing in a consumerCurator just to get the host
    // consumer UUID
    Consumer hostConsumer = null;
    if (consumer.getFact("virt.uuid") != null) {
        hostConsumer = consumerCurator.getHost(consumer);
    }
    List<Criterion> criteriaFilters = new LinkedList<>();
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    // or a manifest consumer
    if (ctype.isManifest()) {
        DetachedCriteria requiresHost = DetachedCriteria.forClass(Pool.class, "pool2").createAlias("pool2.attributes", "attrib").add(Restrictions.eqProperty("pool2.id", "id")).add(Restrictions.eq("attrib.indicies", Pool.Attributes.REQUIRES_HOST)).setProjection(Projections.id());
        // we do want everything else
        criteriaFilters.add(Subqueries.notExists(requiresHost));
    } else if (!consumer.isGuest()) {
        PoolFilterBuilder filterBuilder = new PoolFilterBuilder();
        filterBuilder.addAttributeFilter("virt_only", "true");
        criteriaFilters.add(Restrictions.not(filterBuilder.getCriteria()));
    } else {
        // add criteria for filtering out pools that are not for this guest
        if (consumer.hasFact("virt.uuid")) {
            // need a default value in case there is no host
            String hostUuid = "";
            if (hostConsumer != null) {
                hostUuid = hostConsumer.getUuid();
            }
            // Note: looking for pools that are not for this guest
            // we do want everything else
            DetachedCriteria wrongRequiresHost = DetachedCriteria.forClass(Pool.class, "pool2").createAlias("pool2.attributes", "attrib").add(Restrictions.eqProperty("pool2.id", "id")).add(Restrictions.eq("attrib.indicies", Pool.Attributes.REQUIRES_HOST)).add(Restrictions.ne("attrib.elements", hostUuid).ignoreCase()).setProjection(Projections.id());
            criteriaFilters.add(Subqueries.notExists(wrongRequiresHost));
        }
    // no virt.uuid, we can't try to filter
    }
    return criteriaFilters;
}
Also used : Consumer(org.candlepin.model.Consumer) Criterion(org.hibernate.criterion.Criterion) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) ConsumerType(org.candlepin.model.ConsumerType) LinkedList(java.util.LinkedList)

Aggregations

PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)11 LinkedList (java.util.LinkedList)10 Pool (org.candlepin.model.Pool)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 PageRequest (org.candlepin.common.paging.PageRequest)7 Test (org.junit.Test)6 Product (org.candlepin.model.Product)5 Date (java.util.Date)4 Consumer (org.candlepin.model.Consumer)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 Owner (org.candlepin.model.Owner)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Entitlement (org.candlepin.model.Entitlement)2 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)2