CollectionBasedProductCollection.java
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
*/
package com.adobe.cq.commerce.impl.collection;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.collection.ProductCollection;
import com.adobe.cq.commerce.api.collection.ProductCollectionManager;
import com.adobe.cq.commerce.impl.collection.AbstractProductCollection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
public class CollectionBasedProductCollection
extends AbstractProductCollection {
protected static final String TYPE_VALUE = "collection-based";
public CollectionBasedProductCollection(Resource resource) {
super(resource);
}
@Override
public Iterator<Product> getProducts() {
List<ProductCollection> productBasedCollections = this.getAllProductBasedCollections(this);
ArrayList<Product> productList = new ArrayList<Product>();
for (ProductCollection productCollection : productBasedCollections) {
this.log.debug("product collection: {}", (Object)productCollection.getPath());
Iterator<Product> productIterator = productCollection.getProducts();
while (productIterator.hasNext()) {
Product p = productIterator.next();
if (productList.contains(p)) continue;
productList.add(p);
this.log.debug("product: {}", (Object)p.getPath());
}
}
return productList.iterator();
}
private List<ProductCollection> getAllProductBasedCollections(ProductCollection collection) {
ArrayList<ProductCollection> visitedCollections = new ArrayList<ProductCollection>();
ArrayList<ProductCollection> productBasedCollections = new ArrayList<ProductCollection>();
this.collectProductBasedCollections(collection, visitedCollections, productBasedCollections);
return productBasedCollections;
}
private void collectProductBasedCollections(ProductCollection collection, List<ProductCollection> visitedCollections, List<ProductCollection> productBasedCollections) {
this.log.debug("collection: {}", (Object)collection.getPath());
if (!visitedCollections.contains(collection)) {
visitedCollections.add(collection);
this.log.debug("collection added to visitedCollections");
if ("product-based".equals(collection.getType()) || "query-based".equals(collection.getType())) {
if (!productBasedCollections.contains(collection)) {
productBasedCollections.add(collection);
this.log.debug("collection added to productBasedCollections");
}
} else if ("collection-based".equals(collection.getType())) {
this.log.debug("iterate over the children");
Iterator<String> directRefs = collection.getDirectReferences();
while (directRefs.hasNext()) {
String directRef = directRefs.next();
ProductCollection childCollection = this.productCollectionManager.getCollection(directRef);
if (childCollection == null) continue;
this.collectProductBasedCollections(childCollection, visitedCollections, productBasedCollections);
}
}
}
}
@Override
public void checkItemType(String path) {
ProductCollection productCollection = this.productCollectionManager.getCollection(path);
if (productCollection == null) {
throw new IllegalArgumentException("Cannot add collection " + path + " to collection " + this.getPath() + " as it is not a product collection.");
}
}
}