PoolCurator extends AbstractHibernateCurator<Pool> { public Map<String, Set<String>> getDerivedProvidedProductIdsByPoolIds(Collection<String> poolIds) { Map<String, Set<String>> providedProductMap = new HashMap<>(); if (poolIds != null && !poolIds.isEmpty()) { StringBuilder builder = new StringBuilder("SELECT p.id, dpp.id FROM Pool p JOIN p.derivedProvidedProducts dpp WHERE"); javax.persistence.Query query = null; int blockSize = getInBlockSize(); int blockCount = (int) Math.ceil(poolIds.size() / (float) blockSize); if (blockCount > 1) { Iterable<List<String>> blocks = Iterables.partition(poolIds, blockSize); for (int i = 0; i < blockCount; ++i) { if (i != 0) { builder.append(" OR"); } builder.append(" p.id IN (:block").append(i).append(')'); } query = this.getEntityManager().createQuery(builder.toString()); int i = -1; for (List<String> block : blocks) { query.setParameter("block" + ++i, block); } } else { builder.append(" p.id IN (:pids)"); query = this.getEntityManager().createQuery(builder.toString()) .setParameter("pids", poolIds); } for (Object[] cols : (List<Object[]>) query.getResultList()) { Set<String> providedProducts = providedProductMap.get((String) cols[0]); if (providedProducts == null) { providedProducts = new HashSet<>(); providedProductMap.put((String) cols[0], providedProducts); } providedProducts.add((String) cols[1]); } } return providedProductMap; } @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 public void testFetchingPoolDerivedProvidedProductIdsByPoolIds() { Owner owner = this.createOwner(); List<Pool> pools = new ArrayList<>(); List<Product> products = new ArrayList<>(); int poolsToCreate = 5; int productsPerPool = 5; int productsToAttach = 3; for (int i = 0; i < poolsToCreate; ++i) { for (int p = 0; p < productsPerPool; ++p) { String name = String.format("prod-%d", productsPerPool * i + p); products.add(this.createProduct(name, name, owner)); } Product product = this.createProduct(owner); Pool pool = this.createPool(owner, product); for (int p = productsPerPool * i; p < i * productsPerPool + productsToAttach; ++p) { pool.addDerivedProvidedProduct(products.get(p)); } pools.add(this.poolCurator.merge(pool)); } Map<String, Set<String>> expectedPoolProductMap = new HashMap<>(); for (int i : Arrays.asList(0, 2, 4)) { Set productIds = new HashSet<String>(); Pool pool = pools.get(i); for (int j = productsPerPool * i; j < productsPerPool * i + productsToAttach; ++j) { productIds.add(products.get(j).getId()); } expectedPoolProductMap.put(pool.getId(), productIds); } Map<String, Set<String>> actualPoolProductMap = this.poolCurator .getDerivedProvidedProductIdsByPoolIds(expectedPoolProductMap.keySet()); assertNotNull(actualPoolProductMap); assertEquals(expectedPoolProductMap, actualPoolProductMap); }
@Test public void testFetchingPoolDerivedProvidedProductIdsByIds() { Owner owner = this.createOwner(); List<Pool> pools = new ArrayList<>(); List<Product> products = new ArrayList<>(); int poolsToCreate = 5; int productsPerPool = 5; int productsToAttach = 3; for (int i = 0; i < poolsToCreate; ++i) { for (int p = 0; p < productsPerPool; ++p) { String name = String.format("prod-%d", productsPerPool * i + p); products.add(this.createProduct(name, name, owner)); } Product product = this.createProduct(owner); Pool pool = this.createPool(owner, product); for (int p = productsPerPool * i; p < i * productsPerPool + productsToAttach; ++p) { pool.addDerivedProvidedProduct(products.get(p)); } pools.add(this.poolCurator.merge(pool)); } Map<String, Set<String>> expectedPoolProductMap = new HashMap<>(); for (int i : Arrays.asList(0, 2, 4)) { Set productIds = new HashSet<String>(); Pool pool = pools.get(i); for (int j = productsPerPool * i; j < productsPerPool * i + productsToAttach; ++j) { productIds.add(products.get(j).getId()); } expectedPoolProductMap.put(pool.getId(), productIds); } this.poolCurator.flush(); Map<String, Set<String>> actualPoolProductMap = this.poolCurator .getDerivedProvidedProductIdsByPoolIds(expectedPoolProductMap.keySet()); assertNotNull(actualPoolProductMap); assertEquals(expectedPoolProductMap, actualPoolProductMap); }