ClassificationCategoryImpl.java 14.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.RangeIterator
 *  com.day.cq.search.Predicate
 *  com.day.cq.search.SimpleSearch
 *  com.day.cq.search.result.Hit
 *  com.day.cq.search.result.SearchResult
 *  com.day.cq.tagging.TagManager
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.query.Query
 *  javax.jcr.query.QueryManager
 *  javax.jcr.query.QueryResult
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.impl.classification;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationAttribute;
import com.adobe.cq.commerce.api.classification.ClassificationCategory;
import com.adobe.cq.commerce.impl.classification.ClassificationAttributeImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationImpl;
import com.day.cq.commons.RangeIterator;
import com.day.cq.search.Predicate;
import com.day.cq.search.SimpleSearch;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.tagging.TagManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClassificationCategoryImpl
implements ClassificationCategory {
    protected final Logger log = LoggerFactory.getLogger(ClassificationCategoryImpl.class);
    protected Resource resource;
    protected ValueMap properties;
    protected ModifiableValueMap modifiableProperties;
    protected ResourceResolver resolver;
    private static final String DEFAULT_PRODUCTS_START_PATH = "/etc/commerce/products";
    protected static final String NN_ROOT_CATEGORY = "rootcategory";
    protected static final String NN_ATTRIBUTES = "attributes";
    protected static final String NN_CATEGORIES = "categories";
    protected static final String NN_PRODUCTS = "products";
    protected static final String COMMERCE_TYPE_CLASSIFICATION = "classification";
    protected static final String COMMERCE_TYPE_ATTRIBUTE = "classification.attribute";
    protected static final String COMMERCE_TYPE_CATEGORY = "classification.category";
    protected static final String PN_BASE_PATH = "basePath";
    protected static final String PN_MATCH_TAGS = "matchTags";
    protected static final String PN_QUERY_TEXT = "search";
    protected static final String PN_QUERY_TYPE = "searchType";

    public ClassificationCategoryImpl(Resource resource) throws CommerceException {
        if (!this.isCategory(resource)) {
            throw new CommerceException("Resource is not a classification category.");
        }
        this.resource = resource;
        this.properties = (ValueMap)resource.adaptTo(ValueMap.class);
        this.modifiableProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
        this.resolver = resource.getResourceResolver();
    }

    @Override
    public String getTitle() {
        return (String)this.properties.get("jcr:title", (Object)"");
    }

    @Override
    public String getDescription() {
        return (String)this.properties.get("jcr:description", (Object)"");
    }

    @Override
    public String getPath() {
        return this.resource.getPath();
    }

    @Override
    public Classification getClassification() {
        ClassificationCategory tmpCategory;
        for (tmpCategory = this; tmpCategory != null && !tmpCategory.isRootCategory(); tmpCategory = tmpCategory.getParentCategory()) {
        }
        if (tmpCategory == null) {
            return null;
        }
        Resource rootCategoryRes = this.resolver.getResource(tmpCategory.getPath());
        Resource classificationRes = rootCategoryRes.getParent();
        try {
            return new ClassificationImpl(classificationRes);
        }
        catch (CommerceException e) {
            this.log.error("The resource at " + classificationRes.getPath() + " is not a classification", (Throwable)e);
            return null;
        }
    }

    @Override
    public boolean isRootCategory() {
        Resource parentRes = this.resource.getParent();
        return this.isCommerceType(parentRes, "classification");
    }

    @Override
    public List<ClassificationAttribute> getDirectAttributes() {
        ArrayList<ClassificationAttribute> attributes = new ArrayList<ClassificationAttribute>();
        Resource attributesRes = this.resource.getChild("attributes");
        if (attributesRes != null) {
            Iterator resources = attributesRes.listChildren();
            while (resources.hasNext()) {
                Resource r = (Resource)resources.next();
                try {
                    ClassificationAttributeImpl attribute = new ClassificationAttributeImpl(r);
                    attributes.add(attribute);
                }
                catch (CommerceException e) {
                    this.log.error("The resource at " + r.getPath() + " is not an attribute", (Throwable)e);
                }
            }
        }
        return attributes;
    }

    @Override
    public List<ClassificationAttribute> getAttributes() {
        List<ClassificationAttribute> attributes = this.getDirectAttributes();
        if (this.isRootCategory()) {
            return attributes;
        }
        ClassificationCategory parentCategory = this.getParentCategory();
        while (!parentCategory.isRootCategory()) {
            attributes.addAll(parentCategory.getDirectAttributes());
            parentCategory = parentCategory.getParentCategory();
        }
        attributes.addAll(parentCategory.getDirectAttributes());
        return attributes;
    }

    @Override
    public ClassificationAttribute getAttribute(String name) {
        Resource attributeRes = this.resource.getChild("attributes/" + name);
        try {
            return new ClassificationAttributeImpl(attributeRes);
        }
        catch (CommerceException e) {
            this.log.error("There is no attribute with name: " + name, (Throwable)e);
            return null;
        }
    }

    @Override
    public ClassificationCategory getParentCategory() {
        ClassificationCategoryImpl parentCategory = null;
        Resource categoriesRes = this.resource.getParent();
        if (categoriesRes != null && categoriesRes.getName().equals("categories")) {
            Resource parentCategoryRes = categoriesRes.getParent();
            try {
                parentCategory = new ClassificationCategoryImpl(parentCategoryRes);
            }
            catch (CommerceException e) {
                this.log.error("The resource at " + parentCategoryRes.getPath() + " is not a classification category", (Throwable)e);
            }
        }
        return parentCategory;
    }

    @Override
    public List<ClassificationCategory> getSubCategories() {
        ArrayList<ClassificationCategory> categories = new ArrayList<ClassificationCategory>();
        Resource categoriesRes = this.resource.getChild("categories");
        if (categoriesRes == null) {
            return null;
        }
        Iterator resources = categoriesRes.listChildren();
        while (resources.hasNext()) {
            Resource r = (Resource)resources.next();
            try {
                ClassificationCategoryImpl category = new ClassificationCategoryImpl(r);
                categories.add(category);
            }
            catch (CommerceException e) {
                this.log.error("The resource at " + r.getPath() + " is not a category", (Throwable)e);
            }
        }
        return categories;
    }

    @Override
    public ClassificationCategory getSubCategory(String name) {
        Resource categoryRes = this.resource.getChild("categories/" + name);
        try {
            return new ClassificationCategoryImpl(categoryRes);
        }
        catch (CommerceException e) {
            this.log.error("There is no category with name: " + name, (Throwable)e);
            return null;
        }
    }

    @Override
    public Iterable<Product> getDirectProducts() {
        List products = new ArrayList<Product>();
        Resource productsRes = this.resource.getChild("products");
        if (productsRes == null) {
            return products;
        }
        ValueMap properties = (ValueMap)productsRes.adaptTo(ValueMap.class);
        String basePath = (String)properties.get("basePath", (Object)"/etc/commerce/products");
        String[] matchTags = (String[])properties.get("matchTags", (Object)new String[0]);
        String searchQuery = (String)properties.get("search", (Object)"");
        String queryType = (String)properties.get("searchType", (Object)"simple");
        if (matchTags.length == 0 && searchQuery.length() == 0) {
            return products;
        }
        products = matchTags.length > 0 ? this.findProductsByTag(basePath, matchTags) : (queryType.equals("simple") ? this.findProductsBySearch(basePath, searchQuery) : this.findProductsByQuery(queryType, searchQuery));
        return products;
    }

    @Override
    public Iterable<Product> getProducts() {
        Iterable<Product> directProducts = this.getDirectProducts();
        if (directProducts.iterator().hasNext()) {
            return directProducts;
        }
        HashSet<Product> products = new HashSet<Product>();
        List<ClassificationCategory> subCategories = this.getSubCategories();
        if (subCategories.isEmpty()) {
            return products;
        }
        for (ClassificationCategory subCategory : subCategories) {
            Iterable<Product> subCategoryProducts = subCategory.getProducts();
            for (Product subCategoryProduct : subCategoryProducts) {
                products.add(subCategoryProduct);
            }
        }
        return products;
    }

    @Override
    public boolean hasDirectProduct(Product product) {
        Iterable<Product> directProducts = this.getDirectProducts();
        for (Product p : directProducts) {
            if (!p.getPath().equals(product.getPath())) continue;
            return true;
        }
        return false;
    }

    @Override
    public boolean hasProduct(Product product) {
        Iterable<Product> products = this.getProducts();
        for (Product p : products) {
            if (!p.getPath().equals(product.getPath())) continue;
            return true;
        }
        return false;
    }

    private boolean isCategory(Resource resource) {
        return this.isCommerceType(resource, "classification.category");
    }

    private boolean isCommerceType(Resource res, String value) {
        ValueMap properties = (ValueMap)res.adaptTo(ValueMap.class);
        String commerceType = (String)properties.get("cq:commerceType", String.class);
        return StringUtils.equals((String)commerceType, (String)value);
    }

    private List<Product> findProductsByTag(String basePath, String[] matchTags) {
        TagManager tagManager = (TagManager)this.resolver.adaptTo(TagManager.class);
        RangeIterator results = tagManager.find(basePath, matchTags);
        ArrayList<Product> products = new ArrayList<Product>();
        while (results != null && results.hasNext()) {
            Resource r = (Resource)results.next();
            Product p = (Product)r.adaptTo(Product.class);
            if (p == null) continue;
            products.add(p);
        }
        return products;
    }

    private List<Product> findProductsBySearch(String basePath, String searchString) {
        ArrayList<Product> products = new ArrayList<Product>();
        Resource root = this.resolver.getResource(basePath);
        if (root == null) {
            this.log.error("Product base path {} not found.", (Object)basePath);
            return products;
        }
        try {
            SimpleSearch search = (SimpleSearch)root.adaptTo(SimpleSearch.class);
            if (search != null) {
                search.setQuery(searchString);
                search.setSearchIn(basePath);
                search.setHitsPerPage(100000);
                Predicate typePredicate = new Predicate("type", "type");
                typePredicate.set("type", "nt:unstructured");
                search.addPredicate(typePredicate);
                SearchResult result = search.getResult();
                if (result != null) {
                    for (Hit hit : result.getHits()) {
                        Product p = (Product)hit.getResource().adaptTo(Product.class);
                        if (p == null || p.getBaseProduct() != p) continue;
                        products.add(p);
                    }
                }
            }
        }
        catch (Exception e) {
            this.log.error("Product data search failed", (Throwable)e);
        }
        return products;
    }

    private List<Product> findProductsByQuery(String queryType, String query) {
        Session session = (Session)this.resolver.adaptTo(Session.class);
        ArrayList<Product> products = new ArrayList<Product>();
        try {
            Query jcrQuery = session.getWorkspace().getQueryManager().createQuery(query, queryType);
            NodeIterator iterator = jcrQuery.execute().getNodes();
            while (iterator != null && iterator.hasNext()) {
                Resource r = this.resolver.getResource(iterator.nextNode().getPath());
                Product p = (Product)r.adaptTo(Product.class);
                if (p == null) continue;
                products.add(p);
            }
        }
        catch (Exception e) {
            this.log.error("Product data search failed", (Throwable)e);
        }
        return products;
    }
}