OwnerProductCurator extends AbstractHibernateCurator<OwnerProduct> { @Transactional @SuppressWarnings("checkstyle:indentation") public void removeOwnerProductReferences(Owner owner, Collection<String> productUuids) { if (productUuids != null && !productUuids.isEmpty()) { log.info("Removing owner-product references for owner: {}, {}", owner, productUuids); Session session = this.currentSession(); Long poolCount = (Long) session.createCriteria(Pool.class) .createAlias("providedProducts", "providedProd", JoinType.LEFT_OUTER_JOIN) .createAlias("derivedProvidedProducts", "derivedProvidedProd", JoinType.LEFT_OUTER_JOIN) .add(Restrictions.eq("owner", owner)) .add(Restrictions.or( CPRestrictions.in("product.uuid", productUuids), CPRestrictions.in("derivedProduct.uuid", productUuids), CPRestrictions.in("providedProd.uuid", productUuids), CPRestrictions.in("derivedProvidedProd.uuid", productUuids))) .setProjection(Projections.count("id")) .uniqueResult(); if (poolCount != null && poolCount.longValue() > 0) { throw new IllegalStateException( "One or more products are currently used by one or more pools"); } Map<String, Object> criteria = new HashMap<>(); criteria.put("product_uuid", productUuids); criteria.put("owner_id", owner.getId()); int count = this.bulkSQLDelete(OwnerProduct.DB_TABLE, criteria); log.info("{} owner-product relations removed", count); String sql = "SELECT id FROM " + ActivationKey.DB_TABLE + " WHERE owner_id = :ownerId"; List<String> ids = session.createSQLQuery(sql) .setParameter("ownerId", owner.getId()) .list(); if (ids != null && !ids.isEmpty()) { criteria.clear(); criteria.put("key_id", ids); criteria.put("product_uuid", productUuids); count = this.bulkSQLDelete("cp2_activation_key_products", criteria); log.info("{} activation key products removed", count); } else { log.info("0 activation key products removed"); } } } OwnerProductCurator(); @Transactional OwnerProduct getOwnerProductByProductId(Owner owner, String productId); Product getProductById(Owner owner, String productId); @Transactional Product getProductById(String ownerId, String productId); @Transactional Product getProductByIdUsingOwnerKey(String ownerKey, String productId); CandlepinQuery<Owner> getOwnersByProduct(Product product); CandlepinQuery<Owner> getOwnersByProduct(String productId); Collection<String> getProductUuidsByOwner(Owner owner); Collection<String> getProductUuidsByOwner(String ownerId); CandlepinQuery<Product> getProductsByOwner(Owner owner); CandlepinQuery<Product> getProductsByOwner(String ownerId); CandlepinQuery<Product> getProductsByIds(Owner owner, Collection<String> productIds); CandlepinQuery<Product> getProductsByIds(String ownerId, Collection<String> productIds); CandlepinQuery<Product> getProductsByIdsUsingOwnerKey(String ownerKey,
        Collection<String> productIds); @Transactional long getOwnerCount(Product product); @Transactional boolean productExists(Owner owner, String productId); @Transactional Set<String> filterUnknownProductIds(Owner owner, Collection<String> productIds); @Transactional boolean isProductMappedToOwner(Product product, Owner owner); @Transactional boolean mapProductToOwner(Product product, Owner owner); @Transactional int mapProductToOwners(Product product, Owner... owners); @Transactional int mapOwnerToProducts(Owner owner, Product... products); @Transactional boolean removeOwnerFromProduct(Product product, Owner owner); @Transactional int clearOwnersForProduct(Product product); @Transactional int clearProductsForOwner(Owner owner); CandlepinQuery<Product> getOrphanedProducts(); @SuppressWarnings("checkstyle:indentation") CandlepinQuery<Product> getProductsByVersions(Owner owner, Map<String, Integer> productVersions); @Transactional void updateOwnerProductReferences(Owner owner, Map<String, String> productUuidMap); @Transactional @SuppressWarnings("checkstyle:indentation") void removeOwnerProductReferences(Owner owner, Collection<String> productUuids);  }
@Test public void testRemoveOwnerProductReferences() { Owner owner = this.createOwner(); Product original = this.createProduct(); this.createOwnerProductMapping(owner, original); ActivationKey key = TestUtil.createActivationKey(owner, null); key.setProducts(Util.asSet(original)); this.activationKeyCurator.create(key); assertTrue(this.isProductMappedToOwner(original, owner)); this.ownerProductCurator.removeOwnerProductReferences(owner, Arrays.asList(original.getUuid())); assertFalse(this.isProductMappedToOwner(original, owner)); this.activationKeyCurator.refresh(key); Collection<Product> products = key.getProducts(); assertEquals(0, products.size()); }