OwnerProductCurator extends AbstractHibernateCurator<OwnerProduct> { @SuppressWarnings("checkstyle:indentation") public CandlepinQuery<Product> getProductsByVersions(Owner owner, Map<String, Integer> productVersions) { if (productVersions == null || productVersions.isEmpty()) { return this.cpQueryFactory.<Product>buildQuery(); } Disjunction disjunction = Restrictions.disjunction(); Criteria uuidCriteria = this.createSecureCriteria("op") .createAlias("op.product", "p") .add(disjunction) .setProjection(Projections.distinct(Projections.property("p.uuid"))); for (Map.Entry<String, Integer> entry : productVersions.entrySet()) { disjunction.add(Restrictions.and( Restrictions.eq("p.id", entry.getKey()), Restrictions.eq("p.entityVersion", entry.getValue()) )); } if (owner != null) { uuidCriteria.add(Restrictions.not(Restrictions.eq("op.owner", owner))); } List<String> uuids = uuidCriteria.list(); if (uuids != null && !uuids.isEmpty()) { DetachedCriteria criteria = this.createSecureDetachedCriteria(Product.class, null) .add(CPRestrictions.in("uuid", uuids)); return this.cpQueryFactory.<Product>buildQuery(this.currentSession(), criteria); } return this.cpQueryFactory.<Product>buildQuery(); } 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 testGetProductsByVersions() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Product product1 = this.createProduct("p1", "p1", owner1); Product product2 = this.createProduct("p1", "p1", owner2); Product product3 = this.createProduct("p1", "p1", owner3); Product product4 = this.createProduct("p2", "p2", owner2); List<Product> productList1 = this.ownerProductCurator.getProductsByVersions(owner1, Collections.<String, Integer>singletonMap(product1.getId(), product1.getEntityVersion())).list(); List<Product> productList2 = this.ownerProductCurator.getProductsByVersions(owner2, Collections.<String, Integer>singletonMap(product2.getId(), product2.getEntityVersion())).list(); assertEquals(2, productList1.size()); assertEquals(2, productList2.size()); List<String> uuidList1 = new LinkedList<>(); for (Product product : productList1) { uuidList1.add(product.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Product product : productList2) { uuidList2.add(product.getUuid()); } assertEquals(Arrays.asList(product2.getUuid(), product3.getUuid()), uuidList1); assertEquals(Arrays.asList(product1.getUuid(), product3.getUuid()), uuidList2); }
@Test public void testGetProductsByVersionsNoOwner() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Product product1 = this.createProduct("p1", "p1", owner1); Product product2 = this.createProduct("p1", "p1", owner2); Product product3 = this.createProduct("p1", "p1", owner3); Product product4 = this.createProduct("p2", "p2", owner2); List<Product> productList1 = this.ownerProductCurator.getProductsByVersions(null, Collections.<String, Integer>singletonMap(product1.getId(), product1.getEntityVersion())).list(); List<Product> productList2 = this.ownerProductCurator.getProductsByVersions(null, Collections.<String, Integer>singletonMap(product2.getId(), product2.getEntityVersion())).list(); assertEquals(3, productList1.size()); assertEquals(3, productList2.size()); List<String> uuidList1 = new LinkedList<>(); for (Product product : productList1) { uuidList1.add(product.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Product product : productList2) { uuidList2.add(product.getUuid()); } assertEquals(Arrays.asList(product1.getUuid(), product2.getUuid(), product3.getUuid()), uuidList1); assertEquals(Arrays.asList(product1.getUuid(), product2.getUuid(), product3.getUuid()), uuidList2); }
@Test public void testGetProductsByVersionsMultipleVersions() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); Product p1 = this.createProduct("p1", "p1", owner1); Product p2 = this.createProduct("p1", "p1", owner2); Product p3 = this.createProduct("p1", "p1", owner3); Product p4 = this.createProduct("p2", "p2", owner1); Product p5 = this.createProduct("p2", "p2", owner2); Product p6 = this.createProduct("p2", "p2", owner3); Product p7 = this.createProduct("p3", "p3", owner1); Product p8 = this.createProduct("p3", "p3", owner2); Product p9 = this.createProduct("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<Product> productList1 = this.ownerProductCurator.getProductsByVersions(owner1, versions).list(); List<Product> productList2 = this.ownerProductCurator.getProductsByVersions(owner2, versions).list(); List<Product> productList3 = this.ownerProductCurator.getProductsByVersions(null, versions).list(); assertEquals(4, productList1.size()); assertEquals(4, productList2.size()); assertEquals(6, productList3.size()); List<String> uuidList1 = new LinkedList<>(); for (Product product : productList1) { uuidList1.add(product.getUuid()); } List<String> uuidList2 = new LinkedList<>(); for (Product product : productList2) { uuidList2.add(product.getUuid()); } List<String> uuidList3 = new LinkedList<>(); for (Product product : productList3) { uuidList3.add(product.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 testGetProductsByVersionsNoVersionInfo() { Owner owner1 = this.createOwner(); List<Product> productList1 = this.ownerProductCurator.getProductsByVersions(owner1, null).list(); assertEquals(0, productList1.size()); List<Product> productList2 = this.ownerProductCurator.getProductsByVersions(owner1, Collections.<String, Integer>emptyMap()).list(); assertEquals(0, productList2.size()); List<Product> productList3 = this.ownerProductCurator.getProductsByVersions(null, null).list(); assertEquals(0, productList3.size()); List<Product> productList4 = this.ownerProductCurator.getProductsByVersions(null, Collections.<String, Integer>emptyMap()).list(); assertEquals(0, productList4.size()); }