EntitlementCurator extends AbstractHibernateCurator<Entitlement> { public Collection<String> getDependentEntitlementIdsForPools(Consumer consumer, Iterable<String> poolIds) { Set<String> entitlementIds = new HashSet<>(); if (consumer != null && poolIds != null && poolIds.iterator().hasNext()) { ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer); String querySql = "SELECT DISTINCT e.id " + "FROM cp2_pool_provided_products ppp1 " + "JOIN cp2_products p ON p.uuid = ppp1.product_uuid " + "JOIN cp2_content_modified_products cmp ON cmp.element = p.product_id " + "JOIN cp2_product_content pc ON pc.content_uuid = cmp.content_uuid " + "JOIN cp2_pool_provided_products ppp2 ON ppp2.product_uuid = pc.product_uuid " + "JOIN cp_entitlement e ON e.pool_id = ppp2.pool_id " + "WHERE e.consumer_id = :consumer_id " + " AND ppp1.pool_id IN (:pool_ids) "; int blockSize = Math.min(this.getInBlockSize(), this.getQueryParameterLimit() - 1); Query query = getEntityManager().createNativeQuery(querySql) .setParameter("consumer_id", consumer.getId()); for (List<String> block : Iterables.partition(poolIds, blockSize)) { query.setParameter("pool_ids", block); entitlementIds.addAll(query.getResultList()); } if (ctype.isManifest()) { querySql = "SELECT DISTINCT e.id " + "FROM cp2_pool_derprov_products ppp1 " + "JOIN cp2_products p ON p.uuid = ppp1.product_uuid " + "JOIN cp2_content_modified_products cmp ON cmp.element = p.product_id " + "JOIN cp2_product_content pc ON pc.content_uuid = cmp.content_uuid " + "JOIN cp2_pool_provided_products ppp2 ON ppp2.product_uuid = pc.product_uuid " + "JOIN cp_entitlement e ON e.pool_id = ppp2.pool_id " + "WHERE e.consumer_id = :consumer_id " + " AND ppp1.pool_id IN (:pool_ids) "; query = getEntityManager().createNativeQuery(querySql) .setParameter("consumer_id", consumer.getId()); for (List<String> block : Iterables.partition(poolIds, blockSize)) { query.setParameter("pool_ids", block); entitlementIds.addAll(query.getResultList()); } } } return entitlementIds; } @Inject  EntitlementCurator(OwnerProductCurator ownerProductCurator, ProductCurator productCurator,
        ConsumerTypeCurator consumerTypeCurator, CandlepinQueryFactory cpQueryFactory); @Transactional Set<Entitlement> bulkUpdate(Set<Entitlement> entitlements); List<Entitlement> listByConsumer(Consumer consumer); List<Entitlement> listByConsumer(Consumer consumer, EntitlementFilterBuilder filters); List<Entitlement> listByConsumerAndPoolId(Consumer consumer, String poolId); Page<List<Entitlement>> listByConsumer(Consumer consumer, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listByOwner(Owner owner, String productId,
        EntitlementFilterBuilder filters, PageRequest pageRequest); Page<List<Entitlement>> listAll(EntitlementFilterBuilder filters, PageRequest pageRequest); CandlepinQuery<Entitlement> listByOwner(Owner owner); CandlepinQuery<Entitlement> listByEnvironment(Environment environment); CandlepinQuery<Entitlement> listByEnvironment(String environmentId); CandlepinQuery<Entitlement> listByConsumerAndDate(Consumer consumer, Date activeOn); List<Entitlement> listDirty(Consumer consumer); Set<String> listEntitledProductIds(Consumer consumer, Pool pool); int markDependentEntitlementsDirty(Iterable<String> entitlementIds); Set<String> filterDistributorEntitlementIds(Iterable<String> entsIdsToFilter); @Transactional int markEntitlementsDirty(Iterable<String> entitlementIds); @Deprecated Map<Consumer, List<Entitlement>> getDistinctConsumers(List<Entitlement> entsToRevoke); @Transactional void delete(Entitlement entity); void batchDelete(Collection<Entitlement> entitlements); int batchDeleteByIds(Collection<String> entitlementIds); void unlinkEntitlements(Collection<Entitlement> entitlements); @Transactional Entitlement findByCertificateSerial(Long serial); @Transactional Entitlement replicate(Entitlement ent); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackId(Consumer consumer, String stackId); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByStackIds(Consumer consumer, Collection stackIds); @SuppressWarnings("unchecked") CandlepinQuery<Entitlement> findByPoolAttribute(Consumer consumer, String attributeName,
        String value); CandlepinQuery<Entitlement> findByPoolAttribute(String attributeName, String value); Entitlement findUpstreamEntitlementForStack(Consumer consumer, String stackId); Collection<String> getDependentEntitlementIdsForPools(Consumer consumer,
        Iterable<String> poolIds);  }
@Test public void testGetDependentEntitlementIds() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); Consumer distributor = this.createDistributor(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> providedProducts = this.createProducts(owner, 3, "test_prov_prod"); List<Product> dependentProducts = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool = this.createPoolWithProducts(owner, "reqPool1", providedProducts); requiredPool.addProvidedProduct(requiredProducts.get(0)); this.poolCurator.merge(requiredPool); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProducts); assertNotNull(this.bind(consumer, requiredPool)); Entitlement dependentEnt = this.bind(consumer, dependentPool); assertNotNull(dependentEnt); Collection<String> poolIds = entitlementCurator.getDependentEntitlementIdsForPools(consumer, Arrays.asList(requiredPool.getId())); assertEquals(1, poolIds.size()); assertTrue(poolIds.contains(dependentEnt.getId())); ConsumerType ctype = this.consumerTypeCurator.getConsumerType(distributor); assertNotNull(ctype); assertTrue(ctype.isManifest()); assertNotNull(this.bind(distributor, requiredPool)); Entitlement distributorDependentEnt = this.bind(distributor, dependentPool); assertNotNull(distributorDependentEnt); poolIds = entitlementCurator.getDependentEntitlementIdsForPools(distributor, Arrays.asList(requiredPool.getId())); assertEquals(1, poolIds.size()); assertTrue(poolIds.contains(distributorDependentEnt.getId())); }
@Test public void testGetDependentEntitlementIdsForPoolsMatchesOnDerivedProvidedProductsForDistributor() { Owner owner = this.createOwner("test_owner"); Consumer consumer = this.createConsumer(owner); Consumer distributor = this.createDistributor(owner); List<Product> requiredProducts = this.createProducts(owner, 3, "test_req_prod"); List<Product> providedProducts = this.createProducts(owner, 3, "test_prov_prod"); List<Product> dependentProducts = this.createDependentProducts(owner, 1, "test_dep_prod_a", requiredProducts); Pool requiredPool = this.createPoolWithProducts(owner, "reqPool1", providedProducts); requiredPool.setDerivedProduct(providedProducts.get(0)); requiredPool.addDerivedProvidedProduct(requiredProducts.get(0)); this.poolCurator.merge(requiredPool); Pool dependentPool = this.createPoolWithProducts(owner, "depPool1", dependentProducts); assertNotNull(this.bind(consumer, requiredPool)); Entitlement dependentEnt = this.bind(consumer, dependentPool); assertNotNull(dependentEnt); Collection<String> poolIds = entitlementCurator.getDependentEntitlementIdsForPools(consumer, Arrays.asList(requiredPool.getId())); assertEquals(0, poolIds.size()); ConsumerType ctype = this.consumerTypeCurator.getConsumerType(distributor); assertNotNull(ctype); assertTrue(ctype.isManifest()); assertNotNull(this.bind(distributor, requiredPool)); Entitlement distributorDependentEnt = this.bind(distributor, dependentPool); assertNotNull(distributorDependentEnt); poolIds = entitlementCurator.getDependentEntitlementIdsForPools(distributor, Arrays.asList(requiredPool.getId())); assertEquals(1, poolIds.size()); assertTrue(poolIds.contains(distributorDependentEnt.getId())); }