OwnerCurator extends AbstractHibernateCurator<Owner> { public CandlepinQuery<Owner> getOwnersByActiveProduct(Collection<String> productIds) { DetachedCriteria poolIdQuery = DetachedCriteria.forClass(Pool.class, "pool") .createAlias("pool.providedProducts", "providedProducts") .add(CPRestrictions.in("providedProducts.id", productIds)) .setProjection(Property.forName("pool.id")); DetachedCriteria ownerIdQuery = DetachedCriteria.forClass(Entitlement.class, "e") .add(Subqueries.propertyIn("e.pool.id", poolIdQuery)) .createCriteria("pool").add(Restrictions.gt("endDate", new Date())) .setProjection(Property.forName("e.owner.id")); DetachedCriteria distinctQuery = DetachedCriteria.forClass(Owner.class, "o2") .add(Subqueries.propertyIn("o2.id", ownerIdQuery)) .setProjection(Projections.distinct(Projections.property("o2.key"))); DetachedCriteria criteria = DetachedCriteria.forClass(Owner.class, "o") .add(Subqueries.propertyIn("o.key", distinctQuery)); return this.cpQueryFactory.<Owner>buildQuery(this.currentSession(), criteria); } OwnerCurator(); Owner findOwnerById(String ownerId); @Transactional Owner replicate(Owner owner); @Transactional @Override Owner create(Owner entity); @Transactional Owner getByKeySecure(String key); Owner lockAndLoadByKey(String key); @Transactional Owner getByKey(String key); @Transactional CandlepinQuery<Owner> getByKeys(Collection<String> keys); Owner getByUpstreamUuid(String upstreamUuid); CandlepinQuery<Owner> getOwnersByActiveProduct(Collection<String> productIds); @SuppressWarnings("checkstyle:indentation") CandlepinQuery<Owner> getOwnersWithProducts(Collection<String> productIds); CandlepinQuery<String> getConsumerIds(Owner owner); @SuppressWarnings("unchecked") CandlepinQuery<String> getConsumerIds(String ownerId); CandlepinQuery<String> getConsumerUuids(Owner owner); @SuppressWarnings("unchecked") CandlepinQuery<String> getConsumerUuids(String ownerId);  }
@Test public void testGetMultipleOwnersByMultipleActiveProducts() { Owner owner = createOwner(); Owner owner2 = createOwner(); Product product = this.createProduct(owner); Product provided = this.createProduct(owner); Product product2 = this.createProduct(owner2); Product provided2 = this.createProduct(owner2); associateProductToOwner(owner, product, provided); associateProductToOwner(owner2, product2, provided2); List<String> productIds = new ArrayList<>(); productIds.add(provided.getId()); productIds.add(provided2.getId()); List<Owner> results = ownerCurator.getOwnersByActiveProduct(productIds).list(); assertEquals(2, results.size()); }
@Test public void testGetOwnerByActiveProduct() { Owner owner = createOwner(); Product product = this.createProduct(owner); Product provided = this.createProduct(owner); associateProductToOwner(owner, product, provided); List<String> productIds = new ArrayList<>(); productIds.add(provided.getId()); List<Owner> results = ownerCurator.getOwnersByActiveProduct(productIds).list(); assertEquals(1, results.size()); assertEquals(owner, results.get(0)); }