OwnerContentCurator extends AbstractHibernateCurator<OwnerContent> { @SuppressWarnings("checkstyle:indentation") public CandlepinQuery<Content> getContentByVersions(Owner owner, Map<String, Integer> contentVersions) { if (contentVersions == null || contentVersions.isEmpty()) { return this.cpQueryFactory.<Content>buildQuery(); } Disjunction disjunction = Restrictions.disjunction(); Criteria uuidCriteria = this.createSecureCriteria("oc") .createAlias("oc.content", "c") .add(disjunction) .setProjection(Projections.distinct(Projections.property("c.uuid"))); for (Map.Entry<String, Integer> entry : contentVersions.entrySet()) { disjunction.add(Restrictions.and( Restrictions.eq("c.id", entry.getKey()), Restrictions.eq("c.entityVersion", entry.getValue()) )); } if (owner != null) { uuidCriteria.add(Restrictions.not(Restrictions.eq("oc.owner", owner))); } List<String> uuids = uuidCriteria.list(); if (uuids != null && !uuids.isEmpty()) { DetachedCriteria criteria = this.createSecureDetachedCriteria(Content.class, null) .add(CPRestrictions.in("uuid", uuids)); return this.cpQueryFactory.<Content>buildQuery(this.currentSession(), criteria); } return this.cpQueryFactory.<Content>buildQuery(); } 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 testGetContentByVersions() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Content content1 = this.createContent("p1", "p1", owner1); Content content2 = this.createContent("p1", "p1", owner2); Content content3 = this.createContent("p1", "p1", owner3); Content content4 = this.createContent("p2", "p2", owner2); List<Content> contentList1 = this.ownerContentCurator.getContentByVersions(owner1, Collections.<String, Integer>singletonMap(content1.getId(), content1.getEntityVersion())).list(); List<Content> contentList2 = this.ownerContentCurator.getContentByVersions(owner2, Collections.<String, Integer>singletonMap(content2.getId(), content2.getEntityVersion())).list(); assertEquals(2, contentList1.size()); assertEquals(2, contentList2.size()); List<String> uuidList1 = new LinkedList<>(); for (Content content : contentList1) { uuidList1.add(content.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Content content : contentList2) { uuidList2.add(content.getUuid()); } assertEquals(Arrays.asList(content2.getUuid(), content3.getUuid()), uuidList1); assertEquals(Arrays.asList(content1.getUuid(), content3.getUuid()), uuidList2); }
@Test public void testGetContentByVersionsNoOwner() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Content content1 = this.createContent("p1", "p1", owner1); Content content2 = this.createContent("p1", "p1", owner2); Content content3 = this.createContent("p1", "p1", owner3); Content content4 = this.createContent("p2", "p2", owner2); List<Content> contentList1 = this.ownerContentCurator.getContentByVersions(null, Collections.<String, Integer>singletonMap(content1.getId(), content1.getEntityVersion())).list(); List<Content> contentList2 = this.ownerContentCurator.getContentByVersions(null, Collections.<String, Integer>singletonMap(content2.getId(), content2.getEntityVersion())).list(); assertEquals(3, contentList1.size()); assertEquals(3, contentList2.size()); List<String> uuidList1 = new LinkedList<>(); for (Content content : contentList1) { uuidList1.add(content.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Content content : contentList2) { uuidList2.add(content.getUuid()); } assertEquals(Arrays.asList(content1.getUuid(), content2.getUuid(), content3.getUuid()), uuidList1); assertEquals(Arrays.asList(content1.getUuid(), content2.getUuid(), content3.getUuid()), uuidList2); }
@Test public void testGetContentByVersionsMultipleVersions() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Content p1 = this.createContent("p1", "p1", owner1); Content p2 = this.createContent("p1", "p1", owner2); Content p3 = this.createContent("p1", "p1", owner3); Content p4 = this.createContent("p2", "p2", owner1); Content p5 = this.createContent("p2", "p2", owner2); Content p6 = this.createContent("p2", "p2", owner3); Content p7 = this.createContent("p3", "p3", owner1); Content p8 = this.createContent("p3", "p3", owner2); Content p9 = this.createContent("p3", "p3", owner3); Map<String, Integer> versions = new HashMap<>(); versions.put(p1.getId(), p1.getEntityVersion()); versions.put(p4.getId(), p4.getEntityVersion()); versions.put("bad_id", p7.getEntityVersion()); List<Content> contentList1 = this.ownerContentCurator.getContentByVersions(owner1, versions).list(); List<Content> contentList2 = this.ownerContentCurator.getContentByVersions(owner2, versions).list(); List<Content> contentList3 = this.ownerContentCurator.getContentByVersions(null, versions).list(); assertEquals(4, contentList1.size()); assertEquals(4, contentList2.size()); assertEquals(6, contentList3.size()); List<String> uuidList1 = new LinkedList<>(); for (Content content : contentList1) { uuidList1.add(content.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Content content : contentList2) { uuidList2.add(content.getUuid()); } List<String> uuidList3 = new LinkedList<>(); for (Content content : contentList3) { uuidList3.add(content.getUuid()); } assertEquals(Arrays.asList(p2.getUuid(), p3.getUuid(), p5.getUuid(), p6.getUuid()), uuidList1); assertEquals(Arrays.asList(p1.getUuid(), p3.getUuid(), p4.getUuid(), p6.getUuid()), uuidList2); assertEquals( Arrays.asList(p1.getUuid(), p2.getUuid(), p3.getUuid(), p4.getUuid(), p5.getUuid(), p6.getUuid()), uuidList3 ); }
@Test public void testGetContentByVersionsNoVersionInfo() { Owner owner1 = this.createOwner(); List<Content> contentList1 = this.ownerContentCurator.getContentByVersions(owner1, null).list(); assertEquals(0, contentList1.size()); List<Content> contentList2 = this.ownerContentCurator.getContentByVersions(owner1, Collections.<String, Integer>emptyMap()).list(); assertEquals(0, contentList2.size()); List<Content> contentList3 = this.ownerContentCurator.getContentByVersions(null, null).list(); assertEquals(0, contentList3.size()); List<Content> contentList4 = this.ownerContentCurator.getContentByVersions(null, Collections.<String, Integer>emptyMap()).list(); assertEquals(0, contentList4.size()); }