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;
}
Aggregations