PoolCurator extends AbstractHibernateCurator<Pool> { public Set<String> getUnentitledStackDerivedPoolIds(Iterable<String> entitlementIds) { Set<String> output = new HashSet<>(); String sql = "SELECT ss.derivedpool_id " + "FROM cp_pool_source_stack ss " + "LEFT JOIN (SELECT e.consumer_id, ppa.value AS stack_id, e.id AS entitlement_id " + " FROM cp_entitlement e " + " JOIN cp_pool p ON p.id = e.pool_id " + " JOIN cp2_product_attributes ppa ON ppa.product_uuid = p.product_uuid " + " LEFT JOIN cp_pool_source_stack ss ON ss.derivedpool_id = p.id " + " WHERE ss.id IS NULL " + " AND p.sourceentitlement_id IS NULL " + " AND ppa.name = :stackid_attrib_name "; if (entitlementIds != null && entitlementIds.iterator().hasNext()) { sql += " AND e.id NOT IN (:eids) " + ") ec ON ec.consumer_id = ss.sourceconsumer_id AND ec.stack_id = ss.sourcestackid " + "WHERE ec.entitlement_id IS NULL"; javax.persistence.Query query = this.getEntityManager() .createNativeQuery(sql) .setParameter("stackid_attrib_name", Product.Attributes.STACKING_ID); int blockSize = Math.min(this.getQueryParameterLimit() - 1, this.getInBlockSize()); for (List<String> block : Iterables.partition(entitlementIds, blockSize)) { query.setParameter("eids", block); output.addAll(query.getResultList()); } } else { sql += ") ec ON ec.consumer_id = ss.sourceconsumer_id AND ec.stack_id = ss.sourcestackid " + "WHERE ec.entitlement_id IS NULL"; javax.persistence.Query query = this.getEntityManager() .createNativeQuery(sql) .setParameter("stackid_attrib_name", Product.Attributes.STACKING_ID); output.addAll(query.getResultList()); } return output; } @Inject  PoolCurator(ConsumerCurator consumerCurator, ConsumerTypeCurator consumerTypeCurator); @Transactional CandlepinQuery<Pool> listByOwner(Owner owner); @Transactional CandlepinQuery<Pool> listByOwner(Owner owner, Date activeOn); @Transactional CandlepinQuery<Pool> listByOwnerAndType(Owner owner, PoolType type); CandlepinQuery<Pool> listBySourceEntitlement(Entitlement e); Set<Pool> listBySourceEntitlements(Iterable<Entitlement> ents); @Transactional List<Pool> listByConsumer(Consumer c); @Transactional List<Pool> listByOwnerAndProduct(Owner owner, String productId); List<Pool> listExpiredPools(); @Transactional @SuppressWarnings("checkstyle:indentation") List<Pool> listExpiredPools(int blockSize); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, Owner o, String productId, Date activeOn); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, Owner o, Collection<String> productIds,
        Date activeOn); @SuppressWarnings("unchecked") @Transactional List<Pool> listAvailableEntitlementPools(Consumer c, String ownerId, Collection<String> productIds,
        Date activeOn); @Transactional List<Pool> listByFilter(PoolFilterBuilder filters); @Transactional Page<List<Pool>> listAvailableEntitlementPools(Consumer c, String ownerId, String productId,
        String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @Transactional Page<List<Pool>> listAvailableEntitlementPools(Consumer c, Owner o, String productId,
        String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @Transactional List<String> listEntitledConsumerUuids(String poolId); @Transactional @SuppressWarnings({"unchecked", "checkstyle:indentation", "checkstyle:methodlength"}) // TODO: Remove the methodlength suppression once this method is cleaned up Page<List<Pool>> listAvailableEntitlementPools(Consumer consumer, String ownerId,
        Collection<String> productIds, String subscriptionId, Date activeOn, PoolFilterBuilder filters,
        PageRequest pageRequest, boolean postFilter, boolean addFuture, boolean onlyFuture, Date after); @SuppressWarnings("unchecked") @Transactional boolean hasActiveEntitlementPools(String ownerId, Date date); @Transactional List<Pool> listPoolsRestrictedToUser(String username); @SuppressWarnings("unchecked") List<Entitlement> retrieveOrderedEntitlementsOf(Collection<Pool> existingPools); @SuppressWarnings("unchecked") List<String> retrieveOrderedEntitlementIdsOf(Collection<Pool> pools); void deactivatePool(Pool pool); @Transactional @SuppressWarnings("unchecked") List<Entitlement> entitlementsIn(Pool entitlementPool); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionId(Owner owner, String subId); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionIds(Owner owner, Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getBySubscriptionIds(String ownerId, Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getOversubscribedBySubscriptionIds(String ownerId, Map<String, Entitlement> subIdMap); @Transactional Pool replicate(Pool pool); @SuppressWarnings("checkstyle:indentation") int getNoOfDependentEntitlements(String entitlementId); @SuppressWarnings("unchecked") List<EntitlementCertificate> retrieveEntCertsOfPoolsWithSourceEntitlement(String entId); Filter disableConsumerFilter(); List<ActivationKey> getActivationKeysForPool(Pool p); Set<String> retrieveServiceLevelsForOwner(Owner owner, boolean exempt); Set<String> retrieveServiceLevelsForOwner(String ownerId, boolean exempt); @Transactional void delete(Pool entity); void batchDelete(Collection<Pool> pools, Collection<String> alreadyDeletedPools); @SuppressWarnings("checkstyle:indentation") List<Pool> getSubPoolForStackIds(Consumer consumer, Collection stackIds); @SuppressWarnings("unchecked") List<Pool> getOwnerSubPoolsForStackId(Owner owner, String stackId); @SuppressWarnings("unchecked") List<Pool> getPoolsFromBadSubs(Owner owner, Collection<String> expectedSubIds); @SuppressWarnings("unchecked") Set<String> getDerivedPoolIdsForPools(Iterable<String> poolIds); @SuppressWarnings("unchecked") Collection<String> getEntitlementIdsForPools(Collection<String> poolIds); @SuppressWarnings("unchecked") Collection<String> getPoolIdsForSourceEntitlements(Collection<String> entIds); @SuppressWarnings("unchecked") Collection<String> getPoolIdsForEntitlements(Collection<String> entIds); @SuppressWarnings("unchecked") CandlepinQuery<Pool> getPoolsBySubscriptionId(String subId); @SuppressWarnings("unchecked") CandlepinQuery<Pool> getPoolsBySubscriptionIds(Collection<String> subIds); @SuppressWarnings("unchecked") List<Pool> getOwnersFloatingPools(Owner owner); CandlepinQuery<Pool> getMasterPools(); CandlepinQuery<Pool> getMasterPoolsForOwner(Owner owner); CandlepinQuery<Pool> getMasterPoolsForOwnerExcludingSubs(Owner owner,
        Collection<String> excludedSubIds); @SuppressWarnings("unchecked") Set<String> getAllKnownProductIds(); @SuppressWarnings("unchecked") Set<String> getAllKnownProductIdsForOwner(Owner owner); @SuppressWarnings("checkstyle:indentation") Pool findDevPool(Consumer consumer); boolean exists(Pool pool); void calculateConsumedForOwnersPools(Owner owner); void calculateExportedForOwnersPools(Owner owner); void markCertificatesDirtyForPoolsWithProducts(Owner owner, Collection<String> productIds); Boolean provides(Pool pool, String providedProductId); Boolean providesDerived(Pool pool, String derivedProvidedProductId); Map<String, Set<String>> getProvidedProductIds(Collection<Pool> pools); Map<String, Set<String>> getProvidedProductIdsByPoolIds(Collection<String> poolIds); Map<String, Set<String>> getDerivedProvidedProductIds(Collection<Pool> pools); Map<String, Set<String>> getDerivedProvidedProductIdsByPoolIds(Collection<String> poolIds); @Transactional void removeCdn(Cdn cdn); void clearPoolSourceEntitlementRefs(Iterable<String> poolIds); Set<String> getExistingPoolIdsByIds(Iterable<String> poolIds); Map<String, Set<String>> getConsumerStackDerivedPoolIdMap(Iterable<String> stackIds); Set<String> getUnentitledStackDerivedPoolIds(Iterable<String> entitlementIds); static final int EXPIRED_POOL_BLOCK_SIZE; }
@Test @SuppressWarnings("checkstyle:methodlength") public void testGetUnentitledStackDerivedPoolIds() { Owner owner1 = this.createOwner("owner-1"); Owner owner2 = this.createOwner("owner-2"); Consumer consumer1 = this.createConsumer(owner1); Consumer consumer2 = this.createConsumer(owner1); Consumer consumer3 = this.createConsumer(owner2); Date startDate = TestUtil.createDate(2010, 3, 2); Date endDate = TestUtil.createDate(Calendar.getInstance().get(Calendar.YEAR) + 1, 3, 2); String stackId1 = "123"; Product stackingProduct1 = TestUtil.createProduct(); stackingProduct1.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct1.setAttribute(Product.Attributes.STACKING_ID, stackId1); stackingProduct1 = this.createProduct(stackingProduct1, owner1); Product stackingProduct1b = TestUtil.createProduct(); stackingProduct1b.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct1b.setAttribute(Product.Attributes.STACKING_ID, stackId1); stackingProduct1b = this.createProduct(stackingProduct1b, owner2); String stackId2 = "456"; Product stackingProduct2 = TestUtil.createProduct(); stackingProduct2.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct2.setAttribute(Product.Attributes.STACKING_ID, stackId2); stackingProduct2 = this.createProduct(stackingProduct2, owner1); Product stackingProduct2b = TestUtil.createProduct(); stackingProduct2b.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct2b.setAttribute(Product.Attributes.STACKING_ID, stackId2); stackingProduct2b = this.createProduct(stackingProduct2b, owner2); String stackId3 = "789"; Product stackingProduct3 = TestUtil.createProduct(); stackingProduct3.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct3.setAttribute(Product.Attributes.STACKING_ID, stackId3); stackingProduct3 = this.createProduct(stackingProduct3, owner1); Product stackingProduct3b = TestUtil.createProduct(); stackingProduct3b.setAttribute(Product.Attributes.VIRT_LIMIT, "3"); stackingProduct3b.setAttribute(Product.Attributes.STACKING_ID, stackId3); stackingProduct3b = this.createProduct(stackingProduct3b, owner2); Pool pool1 = createPool(owner1, stackingProduct1, 20L, startDate, endDate); pool1.setSourceStack(new SourceStack(consumer1, stackId1)); pool1.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer1.getUuid()); poolCurator.create(pool1); Pool pool1a = createPool(owner1, stackingProduct1, 20L, startDate, endDate); poolCurator.create(pool1a); Entitlement ent1a = new Entitlement(pool1a, consumer1, owner, 5); ent1a.setId("test-entitlement-id-1a"); entitlementCurator.create(ent1a); Pool pool1b = createPool(owner1, stackingProduct1, 20L, startDate, endDate); poolCurator.create(pool1b); Entitlement ent1b = new Entitlement(pool1b, consumer1, owner, 5); ent1b.setId("test-entitlement-id-1b"); entitlementCurator.create(ent1b); Pool pool2 = createPool(owner1, stackingProduct2, 20L, startDate, endDate); pool2.setSourceStack(new SourceStack(consumer1, stackId2)); pool2.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer1.getUuid()); poolCurator.create(pool2); Pool pool2a = createPool(owner1, stackingProduct2, 20L, startDate, endDate); poolCurator.create(pool2a); Entitlement ent2a = new Entitlement(pool2a, consumer1, owner, 5); ent2a.setId("test-entitlement-id-2a"); entitlementCurator.create(ent2a); Pool pool3 = createPool(owner1, stackingProduct3, 20L, startDate, endDate); pool3.setSourceStack(new SourceStack(consumer2, stackId3)); pool3.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer2.getUuid()); poolCurator.create(pool3); Pool pool3a = createPool(owner1, stackingProduct3, 20L, startDate, endDate); poolCurator.create(pool3a); Entitlement ent3a = new Entitlement(pool3a, consumer2, owner, 5); ent3a.setId("test-entitlement-id-3a"); entitlementCurator.create(ent3a); Pool pool4 = createPool(owner1, stackingProduct1, 20L, startDate, endDate); pool4.setSourceStack(new SourceStack(consumer2, stackId1)); pool4.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer2.getUuid()); poolCurator.create(pool4); Pool pool4a = createPool(owner1, stackingProduct1, 20L, startDate, endDate); poolCurator.create(pool4a); Entitlement ent4a = new Entitlement(pool4a, consumer2, owner, 5); ent4a.setId("test-entitlement-id-4a"); entitlementCurator.create(ent4a); Pool pool5 = createPool(owner1, stackingProduct2, 20L, startDate, endDate); pool5.setSourceStack(new SourceStack(consumer2, stackId2)); pool5.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer2.getUuid()); poolCurator.create(pool5); Pool pool5a = createPool(owner1, stackingProduct2, 20L, startDate, endDate); poolCurator.create(pool5a); Entitlement ent5a = new Entitlement(pool5a, consumer2, owner, 5); ent5a.setId("test-entitlement-id-5a"); entitlementCurator.create(ent5a); Pool pool6 = createPool(owner2, stackingProduct3b, 20L, startDate, endDate); pool6.setSourceStack(new SourceStack(consumer3, stackId3)); pool6.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer3.getUuid()); poolCurator.create(pool6); Pool pool6a = createPool(owner2, stackingProduct3b, 20L, startDate, endDate); poolCurator.create(pool6a); Entitlement ent6a = new Entitlement(pool6a, consumer3, owner, 5); ent6a.setId("test-entitlement-id-6a"); entitlementCurator.create(ent6a); Pool pool7 = createPool(owner2, stackingProduct1b, 20L, startDate, endDate); pool7.setSourceStack(new SourceStack(consumer3, stackId1)); pool7.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer3.getUuid()); poolCurator.create(pool7); Pool pool8 = createPool(owner2, stackingProduct2b, 20L, startDate, endDate); poolCurator.create(pool8); Entitlement ent8 = new Entitlement(pool8, consumer3, owner, 5); ent8.setId("test-entitlement-id-8"); entitlementCurator.create(ent8); Set<String> output; output = this.poolCurator.getUnentitledStackDerivedPoolIds(null); assertNotNull(output); assertEquals(1, output.size()); assertEquals(output, Util.asSet(pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds(Collections.<String>emptyList()); assertNotNull(output); assertEquals(1, output.size()); assertEquals(output, Util.asSet(pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds( Arrays.asList(ent1a.getId(), ent1b.getId(), ent2a.getId(), ent3a.getId())); assertNotNull(output); assertEquals(4, output.size()); assertEquals(output, Util.asSet(pool1.getId(), pool2.getId(), pool3.getId(), pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds(Arrays.asList(ent1a.getId())); assertNotNull(output); assertEquals(1, output.size()); assertEquals(output, Util.asSet(pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds(Arrays.asList(ent1b.getId())); assertNotNull(output); assertEquals(1, output.size()); assertEquals(output, Util.asSet(pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds( Arrays.asList(ent1a.getId(), ent1b.getId(), ent2a.getId(), ent3a.getId(), "bad_ent_id")); assertNotNull(output); assertEquals(4, output.size()); assertEquals(output, Util.asSet(pool1.getId(), pool2.getId(), pool3.getId(), pool7.getId())); output = this.poolCurator.getUnentitledStackDerivedPoolIds(Arrays.asList("bad_ent_id")); assertNotNull(output); assertEquals(1, output.size()); assertEquals(output, Util.asSet(pool7.getId())); }