ProductCollectionManagerImpl.java 7.12 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.impl.collection;

import com.adobe.cq.commerce.api.CommerceException;
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.CollectionBasedProductCollection;
import com.adobe.cq.commerce.impl.collection.ProductBasedProductCollection;
import com.adobe.cq.commerce.impl.collection.QueryBasedProductCollection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProductCollectionManagerImpl
implements ProductCollectionManager {
    protected static final String DEFAULT_COLLECTION_ROOT = "/etc/commerce/collections";
    protected static final String PRODUCT_COLLECTION_RESOURCE_TYPE_VALUE = "commerce/collection";
    protected static final String PN_PRODUCT_COLLECTION_TYPE = "commerce.collection.type";
    private final Logger log = LoggerFactory.getLogger(ProductCollectionManagerImpl.class);
    private ResourceResolver resolver;
    private ResourceCollectionManager resourceCollectionManager;

    public ProductCollectionManagerImpl(ResourceResolver resolver) {
        this.resolver = resolver;
        this.resourceCollectionManager = (ResourceCollectionManager)resolver.adaptTo(ResourceCollectionManager.class);
    }

    @Override
    public ProductCollection getCollection(String path) {
        Resource resource = this.resolver.getResource(path);
        if (resource == null) {
            return null;
        }
        if (!resource.isResourceType("commerce/collection")) {
            return null;
        }
        ValueMap properties = (ValueMap)resource.adaptTo(ValueMap.class);
        String collectionType = (String)properties.get("commerce.collection.type", String.class);
        if ("query-based".equals(collectionType)) {
            return new QueryBasedProductCollection(resource);
        }
        if ("product-based".equals(collectionType)) {
            return new ProductBasedProductCollection(resource);
        }
        if ("collection-based".equals(collectionType)) {
            return new CollectionBasedProductCollection(resource);
        }
        return null;
    }

    @Override
    public Iterator<ProductCollection> getProductCollections(Product product) {
        ArrayList<ProductCollection> collections = new ArrayList<ProductCollection>();
        String query = "/jcr:root/etc/commerce/collections//element(*, nt:base)[@sling:resourceType='commerce/collection']";
        Iterator collectionResources = this.resolver.findResources(query, "xpath");
        while (collectionResources.hasNext()) {
            ProductCollection collection;
            Resource collectionRes = (Resource)collectionResources.next();
            if (collectionRes == null || (collection = (ProductCollection)collectionRes.adaptTo(ProductCollection.class)) == null || !collection.contains(product)) continue;
            collections.add(collection);
        }
        return collections.iterator();
    }

    @Override
    public ProductCollection createCollection(String parentPath, String name) throws CommerceException {
        return this.createCollection(parentPath, name, null);
    }

    @Override
    public ProductCollection createCollection(String parentPath, String name, Map<String, Object> properties) throws CommerceException {
        Resource parentResource = this.resolver.getResource(parentPath);
        try {
            if (properties == null) {
                properties = new HashMap<String, Object>();
            }
            properties.put("sling:resourceType", "commerce/collection");
            if (properties.get("commerce.collection.type") == null) {
                properties.put("commerce.collection.type", "product-based");
            }
            if (properties.get("jcr:primaryType") == null) {
                properties.put("jcr:primaryType", "nt:unstructured");
            }
            properties.put("jcr:lastModifiedBy", this.resolver.getUserID());
            properties.put("jcr:lastModified", Calendar.getInstance());
            ResourceCollection resourceCollection = this.resourceCollectionManager.createCollection(parentResource, name, properties);
            this.resolver.commit();
            return this.getCollection(resourceCollection.getPath());
        }
        catch (PersistenceException e) {
            throw new CommerceException("Cannot create a collection below " + parentPath, (Throwable)e);
        }
    }

    @Override
    public void updateCollection(ProductCollection collection, Map<String, Object> properties) throws CommerceException {
        if (collection == null) {
            throw new CommerceException("Cannot update the collection as it is undefined.");
        }
        Resource resource = this.resolver.getResource(collection.getPath());
        try {
            ModifiableValueMap currentProps = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
            currentProps.putAll(properties);
            currentProps.put((Object)"jcr:lastModifiedBy", (Object)this.resolver.getUserID());
            currentProps.put((Object)"jcr:lastModified", (Object)Calendar.getInstance());
            this.resolver.commit();
        }
        catch (PersistenceException e) {
            throw new CommerceException("Cannot delete the collection at " + collection.getPath(), (Throwable)e);
        }
    }

    @Override
    public void deleteCollection(ProductCollection collection) throws CommerceException {
        if (collection == null) {
            throw new CommerceException("Cannot delete the collection as it is undefined.");
        }
        Resource resource = this.resolver.getResource(collection.getPath());
        boolean deleted = false;
        try {
            deleted = this.resourceCollectionManager.deleteCollection(resource);
            this.resolver.commit();
        }
        catch (PersistenceException e) {
            throw new CommerceException("Cannot delete the collection at " + collection.getPath(), (Throwable)e);
        }
        if (!deleted) {
            throw new CommerceException("Cannot delete the collection at " + collection.getPath());
        }
    }
}