OwnerContentCurator extends AbstractHibernateCurator<OwnerContent> { @Transactional public void removeOwnerContentReferences(Owner owner, Collection<String> contentUuids) { if (contentUuids != null && !contentUuids.isEmpty()) { log.info("Removing owner-content references for owner: {}, {}", owner, contentUuids); Session session = this.currentSession(); Map<String, Object> criteria = new HashMap<>(); criteria.put("owner_id", owner.getId()); criteria.put("content_uuid", contentUuids); int count = this.bulkSQLDelete(OwnerContent.DB_TABLE, criteria); log.info("{} owner-content relations removed", count); String sql = "SELECT id FROM " + Environment.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("environment_id", ids); criteria.put("content_uuid", contentUuids); count = this.bulkSQLDelete(EnvironmentContent.DB_TABLE, criteria); log.info("{} environment-content relations updated", count); } else { log.info("0 environment-content relations updated"); } } } OwnerContentCurator(); Content getContentById(Owner owner, String contentId); @Transactional Content getContentById(String ownerId, String contentId); CandlepinQuery<Owner> getOwnersByContent(Content content); CandlepinQuery<Owner> getOwnersByContent(String contentId); Collection<String> getContentUuidsByOwner(Owner owner); Collection<String> getContentUuidsByOwner(String ownerId); CandlepinQuery<Content> getContentByOwner(Owner owner); CandlepinQuery<Content> getContentByOwner(String ownerId); CandlepinQuery<Content> getContentByIds(Owner owner, Collection<String> contentIds); CandlepinQuery<Content> getContentByIds(String ownerId, Collection<String> contentIds); @Transactional long getOwnerCount(Content content); @Transactional boolean contentExists(Owner owner, String contentId); @Transactional boolean isContentMappedToOwner(Content content, Owner owner); @Transactional boolean mapContentToOwner(Content content, Owner owner); @Transactional int mapContentToOwners(Content content, Owner... owners); @Transactional int mapOwnerToContent(Owner owner, Content... content); @Transactional boolean removeOwnerFromContent(Content content, Owner owner); @Transactional int clearOwnersForContent(Content content); @Transactional int clearContentForOwner(Owner owner); @SuppressWarnings("checkstyle:indentation") CandlepinQuery<Content> getContentByVersions(Owner owner, Map<String, Integer> contentVersions); CandlepinQuery<Content> getOrphanedContent(); @Transactional void updateOwnerContentReferences(Owner owner, Map<String, String> contentUuidMap); @Transactional void removeOwnerContentReferences(Owner owner, Collection<String> contentUuids);  }
@Test public void testRemoveOwnerContentReferences() { Owner owner1 = this.createOwner("owner1"); Owner owner2 = this.createOwner("owner2"); Content original = this.createContent("c1", "c1", owner1); Content unmodified = this.createContent("c1", "c1", owner2); assertTrue(original.getUuid() != unmodified.getUuid()); Environment environment1 = this.createEnvironment( owner1, "test_env-1", "test_env-1", null, null, Arrays.asList(original) ); Environment environment2 = this.createEnvironment( owner2, "test_env-2", "test_env-2", null, null, Arrays.asList(unmodified) ); assertTrue(this.isContentMappedToOwner(original, owner1)); assertTrue(this.isContentMappedToOwner(unmodified, owner2)); this.ownerContentCurator.removeOwnerContentReferences(owner1, Arrays.asList(original.getUuid())); assertFalse(this.isContentMappedToOwner(original, owner1)); assertTrue(this.isContentMappedToOwner(unmodified, owner2)); this.environmentCurator.evict(environment1); this.environmentCurator.evict(environment2); environment1 = this.environmentCurator.get(environment1.getId()); environment2 = this.environmentCurator.get(environment2.getId()); assertEquals(0, environment1.getEnvironmentContent().size()); assertEquals(1, environment2.getEnvironmentContent().size()); assertEquals(unmodified.getUuid(), environment2.getEnvironmentContent().iterator().next().getContent() .getUuid()); }