CollectionBasedProductCollection.java 3.79 KB
/*
 * 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.");
        }
    }
}