OwnerProductCurator extends AbstractHibernateCurator<OwnerProduct> { @Transactional public void updateOwnerProductReferences(Owner owner, Map<String, String> productUuidMap) { if (productUuidMap == null || productUuidMap.isEmpty()) { return; } Session session = this.currentSession(); Map<String, Object> criteria = new HashMap<>(); Map<Object, Object> uuidMap = Map.class.cast(productUuidMap); criteria.put("product_uuid", productUuidMap.keySet()); criteria.put("owner_id", owner.getId()); int count = this.bulkSQLUpdate(OwnerProduct.DB_TABLE, "product_uuid", uuidMap, criteria); log.debug("{} owner-product relations updated", count); count = this.bulkSQLUpdate(Pool.DB_TABLE, "product_uuid", uuidMap, criteria); criteria.remove("product_uuid"); criteria.put("derived_product_uuid", productUuidMap.keySet()); count += this.bulkSQLUpdate(Pool.DB_TABLE, "derived_product_uuid", uuidMap, criteria); log.debug("{} pools updated", count); List<String> ids = session.createSQLQuery("SELECT id FROM cp_pool WHERE owner_id = :ownerId") .setParameter("ownerId", owner.getId()) .list(); if (ids != null && !ids.isEmpty()) { criteria.clear(); criteria.put("product_uuid", productUuidMap.keySet()); criteria.put("pool_id", ids); count = this.bulkSQLUpdate("cp2_pool_provided_products", "product_uuid", uuidMap, criteria); log.debug("{} provided products updated", count); count = this.bulkSQLUpdate("cp2_pool_derprov_products", "product_uuid", uuidMap, criteria); log.debug("{} derived provided products updated", count); } else { log.debug("0 provided products updated"); log.debug("0 derived provided products updated"); } ids = session.createSQLQuery("SELECT id FROM cp_activation_key WHERE owner_id = :ownerId") .setParameter("ownerId", owner.getId()) .list(); if (ids != null && !ids.isEmpty()) { criteria.clear(); criteria.put("product_uuid", productUuidMap.keySet()); criteria.put("key_id", ids); count = this.bulkSQLUpdate("cp2_activation_key_products", "product_uuid", uuidMap, criteria); log.debug("{} activation keys updated", count); } else { log.debug("0 activation keys updated"); } } 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 testUpdateOwnerProductReferences() { Owner owner = this.createOwner(); Product original = this.createProduct(); Product updated = this.createProduct(); this.createOwnerProductMapping(owner, original); ActivationKey key = TestUtil.createActivationKey(owner, null); key.setProducts(Util.asSet(original)); Pool pool1 = TestUtil.createPool(owner, original); Pool pool2 = TestUtil.createPool(owner); pool2.addProvidedProduct(original); Pool pool3 = TestUtil.createPool(owner); pool3.setDerivedProduct(original); Pool pool4 = TestUtil.createPool(owner); pool4.setDerivedProvidedProducts(Arrays.asList(original)); this.activationKeyCurator.create(key); this.poolCurator.create(pool1); this.productCurator.create(pool2.getProduct()); this.poolCurator.create(pool2); this.productCurator.create(pool3.getProduct()); this.poolCurator.create(pool3); this.productCurator.create(pool4.getProduct()); this.poolCurator.create(pool4); assertTrue(this.isProductMappedToOwner(original, owner)); assertFalse(this.isProductMappedToOwner(updated, owner)); Map<String, String> uuidMap = new HashMap<>(); uuidMap.put(original.getUuid(), updated.getUuid()); this.ownerProductCurator.updateOwnerProductReferences(owner, uuidMap); assertFalse(this.isProductMappedToOwner(original, owner)); assertTrue(this.isProductMappedToOwner(updated, owner)); this.activationKeyCurator.refresh(key); Collection<Product> products = key.getProducts(); assertEquals(1, products.size()); assertEquals(updated.getUuid(), products.iterator().next().getUuid()); this.poolCurator.refresh(pool1); assertEquals(updated.getUuid(), pool1.getProduct().getUuid()); this.poolCurator.refresh(pool2); assertNotEquals(updated.getUuid(), pool2.getProduct().getUuid()); products = pool2.getProvidedProducts(); assertEquals(1, products.size()); assertEquals(updated.getUuid(), products.iterator().next().getUuid()); this.poolCurator.refresh(pool3); assertNotEquals(updated.getUuid(), pool3.getProduct().getUuid()); assertEquals(updated.getUuid(), pool3.getDerivedProduct().getUuid()); this.poolCurator.refresh(pool4); assertNotEquals(updated.getUuid(), pool4.getProduct().getUuid()); products = pool4.getDerivedProvidedProducts(); assertEquals(1, products.size()); assertEquals(updated.getUuid(), products.iterator().next().getUuid()); }