All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.commerce.common.collection.AbstractProductCollection Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2015 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.commerce.common.collection;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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.ProductCollectionManagerImpl;
import com.day.cq.commons.jcr.JcrConstants;

import aQute.bnd.annotation.ConsumerType;

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;

/**
 * An abstract implementation of a {@link ProductCollection}
 *
 */
//made visible for cq-commerce-social, to prevent making com.adobe.cq.commerce.impl.collection visible
@ConsumerType
public abstract class AbstractProductCollection implements ProductCollection {

    protected final Logger log = LoggerFactory.getLogger(AbstractProductCollection.class);

    protected Resource resource;
    protected ValueMap properties;
    protected ModifiableValueMap modifiableProperties;
    protected ResourceResolver resolver;
    protected ResourceCollection resourceCollection;
    protected ResourceCollectionManager resourceCollectionManager;
    protected ProductCollectionManager productCollectionManager;

    public AbstractProductCollection(Resource resource) {
        this.resource = resource;
        this.properties = resource.adaptTo(ValueMap.class);
        this.modifiableProperties = resource.adaptTo(ModifiableValueMap.class);
        this.resolver = resource.getResourceResolver();
        this.resourceCollectionManager = resolver.adaptTo(ResourceCollectionManager.class);
        this.resourceCollection = resourceCollectionManager.getCollection(resource);
        this.productCollectionManager = resolver.adaptTo(ProductCollectionManager.class);
    }

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

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

    @Override
    public String getType() {
        return properties.get(ProductCollectionManagerImpl.PN_PRODUCT_COLLECTION_TYPE, "");
    }

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

    @Override
    public Map getProperties() {
        Map props = new HashMap();
        props.putAll(properties);
        return props;
    }

    @Override
    public  T getProperty(String key, Class type) {
        return properties.get(key, type);
    }

    @Override
    public  T getProperty(String key, T defaultValue) {
        return properties.get(key, defaultValue);    }

    @Override
    public void setProperties(Map properties) throws CommerceException {
        modifiableProperties.putAll(properties);
        // update the modification infos
        modifiableProperties.put(JcrConstants.JCR_LAST_MODIFIED_BY,  resolver.getUserID());
        modifiableProperties.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
        try {
            resolver.commit();
        } catch (PersistenceException e) {
            throw new CommerceException("Failed to set the collection properties", e);
        }
        log.debug("Properties were set for collection at {}", getPath());
    }

    @Override
    public void setProperty(String key, Object value) throws CommerceException {
        modifiableProperties.put(key, value);
        // update the modification infos
        modifiableProperties.put(JcrConstants.JCR_LAST_MODIFIED_BY,  resolver.getUserID());
        modifiableProperties.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
        try {
            resolver.commit();
        } catch (PersistenceException e) {
            throw new CommerceException("Failed to set the collection property", e);
        }
        log.debug("Property {} was set for collection at {}", key, getPath());
    }

    @Override
    public boolean contains(Product product) {
        Iterator products = getProducts();
        while (products.hasNext()) {
            Product p = products.next();
            if (p.getPath().equals(product.getPath())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator getDirectReferences() {
        List references = new ArrayList();
        Iterator resources = resourceCollection.getResources();
        while (resources.hasNext()) {
            Resource r = resources.next();
            String path = r.getPath();
            references.add(path);
        }
        return references.iterator();
    }

    @Override
    public boolean hasDirectReference(String path) {
        Iterator references = getDirectReferences();
        while (references.hasNext()) {
            String ref = references.next();
            if (ref.equals(path)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void add(String path) throws CommerceException {
        checkItemType(path);
        Resource res = resolver.getResource(path);
        boolean added = false;
        try {
            added = resourceCollection.add(res);
            // update the modification infos
            modifiableProperties.put(JcrConstants.JCR_LAST_MODIFIED_BY,  resolver.getUserID());
            modifiableProperties.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
            resolver.commit();
        } catch (PersistenceException e) {
            throw new CommerceException("Cannot add item " + path + " to collection " + this.getPath());
        }
        if (!added) {
            throw new CommerceException("Cannot add item " + path + " to collection " + this.getPath());
        }
    }

    @Override
    public void remove(String path) throws CommerceException {
        Resource productRes = resolver.getResource(path);
        boolean removed = false;
        try {
            // update the modification infos
            modifiableProperties.put(JcrConstants.JCR_LAST_MODIFIED_BY,  resolver.getUserID());
            modifiableProperties.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
            removed = resourceCollection.remove(productRes);
            resolver.commit();
        } catch (PersistenceException e) {
            throw new CommerceException("Cannot remove item " + path + " from collection " + this.getPath());
        }
        if (!removed) {
            throw new CommerceException("Cannot remove item " + path + " from collection " + this.getPath());
        }
    }

    @Override
    public void orderBefore(String srcPath, String destPath) {
        Resource srcProductRes = resolver.getResource(srcPath);
        Resource destProductRes = resolver.getResource(destPath);
        resourceCollection.orderBefore(srcProductRes, destProductRes);
        try {
            resolver.commit();
        } catch (PersistenceException e) {
            log.error("Could not order item {} before item {}", srcPath, destPath);
        }
    }

    /**
     * Throws an IllegalArgumentException if the type of the resource identified by its path
     * is not supported by the implementation.
     */
    public void checkItemType(String path) {
        Resource res = resolver.getResource(path);
        if (res == null) {
            throw new IllegalArgumentException("There is no resource at " + path);
        }
        Product product = res.adaptTo(Product.class);
        if (product == null) {
            throw new IllegalArgumentException("Cannot add item " + path + " to collection " + this.getPath()
                    + " as it is not a product.");
        }
    }


    //-------------< Object overrides >-------------------------------------------------------

    // enable comparing 2 products based on their paths

    @Override
    public boolean equals(final Object obj) {
        return obj instanceof ProductCollection && ((ProductCollection) obj).getPath().equals(getPath());
    }

    @Override
    public int hashCode() {
        return getPath().hashCode();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy