SmartListImpl.java 7.41 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  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.osgi.service.event.Event
 *  org.osgi.service.event.EventAdmin
 */
package com.adobe.cq.commerce.impl.smartlist;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.smartlist.SmartList;
import com.adobe.cq.commerce.api.smartlist.SmartListEntry;
import com.adobe.cq.commerce.impl.collection.AbstractProductCollection;
import com.adobe.cq.commerce.impl.smartlist.SmartListEntryImpl;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
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.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class SmartListImpl
extends AbstractProductCollection
implements SmartList {
    protected static final String TYPE_VALUE = "product-based";
    protected static final String PN_SMART_LIST_OWNER = "userIdentifier";
    protected static final String PN_SMART_LIST_DEFAULT = "smartlist_default_b";
    protected static final String PN_SMART_LIST_PRIVACY = "smartlist_privacy_s";
    protected static final String SMART_LIST_RESOURCE_TYPE_VALUE = "commerce/smartlist";
    protected static final String SMART_LIST_EVENT_BASE = "com/adobe/cq/commerce/smartlist/";
    private EventAdmin eventAdmin;

    public SmartListImpl(Resource resource, EventAdmin eventAdmin) throws CommerceException {
        super(resource);
        if (!resource.isResourceType("commerce/smartlist")) {
            throw new CommerceException("Resource is not a SmartListImpl.");
        }
        this.eventAdmin = eventAdmin;
    }

    @Override
    public void add(String path) throws CommerceException {
        this.checkItemType(path);
        Product product = (Product)this.resolver.getResource(path).adaptTo(Product.class);
        this.add(new SmartListEntryImpl(product));
    }

    @Override
    public void add(SmartListEntry smartListEntry) throws CommerceException {
        if (smartListEntry == null) {
            throw new IllegalArgumentException("Cannot add undefined item");
        }
        Product product = smartListEntry.getProduct();
        if (product == null) {
            throw new IllegalArgumentException("Cannot add smart list entry without product");
        }
        Resource productRes = this.resolver.getResource(product.getPath());
        boolean added = false;
        try {
            if (!this.resourceCollection.contains(productRes)) {
                added = this.resourceCollection.add(productRes, smartListEntry.getProperties());
            } else {
                ModifiableValueMap productResProperties = this.resourceCollection.getProperties(productRes);
                int newQuantity = smartListEntry.getQuantity();
                if (productResProperties.containsKey((Object)"quantity")) {
                    newQuantity += ((Integer)productResProperties.get("quantity", Integer.class)).intValue();
                }
                productResProperties.putAll(smartListEntry.getProperties());
                productResProperties.put((Object)"quantity", (Object)newQuantity);
                added = true;
            }
            this.resolver.commit();
            this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_ADDED", this.resourceCollection.getPath(), smartListEntry.getProduct().getPath());
        }
        catch (PersistenceException e) {
            throw new CommerceException("Cannot add item " + product.getPath() + " to smart list " + this.getPath());
        }
        if (!added) {
            throw new CommerceException("Cannot add item " + product.getPath() + " to smart list " + this.getPath());
        }
    }

    @Override
    public void update(String path, Map<String, Object> properties) throws CommerceException {
        Resource productRes = this.resolver.getResource(path);
        try {
            ModifiableValueMap productResProperties = this.resourceCollection.getProperties(productRes);
            productResProperties.putAll(properties);
            this.resolver.commit();
            this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_MODIFIED", this.resourceCollection.getPath(), path);
        }
        catch (PersistenceException e) {
            throw new CommerceException("Cannot update item " + path + " on collection " + this.getPath());
        }
    }

    @Override
    public void remove(String path) throws CommerceException {
        super.remove(path);
        this.logEvent("com/adobe/cq/commerce/smartlist/PRODUCT_DELETED", this.resourceCollection.getPath(), path);
    }

    @Override
    public String getPath() {
        return StringUtils.substringAfterLast((String)super.getPath(), (String)"/");
    }

    @Override
    public Iterator<SmartListEntry> getSmartListEntries() {
        ArrayList<SmartListEntryImpl> smartListEntries = new ArrayList<SmartListEntryImpl>();
        Iterator resources = this.resourceCollection.getResources();
        while (resources.hasNext()) {
            Resource r = (Resource)resources.next();
            Product p = (Product)r.adaptTo(Product.class);
            if (p == null) continue;
            smartListEntries.add(new SmartListEntryImpl(p, (ValueMap)this.resourceCollection.getProperties(r)));
        }
        return smartListEntries.iterator();
    }

    @Override
    public Iterator<Product> getProducts() {
        ArrayList<Product> products = new ArrayList<Product>();
        Iterator resources = this.resourceCollection.getResources();
        while (resources.hasNext()) {
            Resource r = (Resource)resources.next();
            Product p = (Product)r.adaptTo(Product.class);
            if (p == null) continue;
            products.add(p);
        }
        return products.iterator();
    }

    @Override
    public String getOwner() {
        return (String)((Object)this.getProperty("userIdentifier", String.class));
    }

    @Override
    public boolean isDefault() {
        return this.getProperty("smartlist_default_b", Boolean.FALSE);
    }

    @Override
    public SmartList.Privacy getPrivacy() {
        return SmartList.Privacy.valueOf(this.getProperty("smartlist_privacy_s", SmartList.Privacy.PERSONAL.name()));
    }

    protected void logEvent(String eventName, String smartListPath, String productPath) {
        if (this.eventAdmin != null) {
            Hashtable<String, String> eventProperties = new Hashtable<String, String>();
            eventProperties.put("smartlist", smartListPath);
            eventProperties.put("userid", this.resolver.getUserID());
            if (StringUtils.isNotEmpty((String)productPath)) {
                eventProperties.put("product", productPath);
            }
            this.eventAdmin.postEvent(new Event(eventName, eventProperties));
        }
    }
}