CommerceAdapterFactory.java 14.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.social.ugc.api.UgcSearch
 *  com.adobe.cq.social.ugcbase.SocialUtils
 *  com.day.cq.commons.inherit.ComponentInheritanceValueMap
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.service.event.EventAdmin
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.impl;

import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceProvider;
import com.adobe.cq.commerce.api.CommerceService;
import com.adobe.cq.commerce.api.CommerceServiceFactory;
import com.adobe.cq.commerce.api.PaymentMethod;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.api.ShippingMethod;
import com.adobe.cq.commerce.api.asset.ProductAssetHandlerProvider;
import com.adobe.cq.commerce.api.asset.ProductAssetManager;
import com.adobe.cq.commerce.api.classification.Classification;
import com.adobe.cq.commerce.api.classification.ClassificationManager;
import com.adobe.cq.commerce.api.collection.ProductCollection;
import com.adobe.cq.commerce.api.collection.ProductCollectionManager;
import com.adobe.cq.commerce.api.promotion.Promotion;
import com.adobe.cq.commerce.api.promotion.PromotionHandler;
import com.adobe.cq.commerce.api.promotion.PromotionManager;
import com.adobe.cq.commerce.api.promotion.Voucher;
import com.adobe.cq.commerce.api.smartlist.SmartList;
import com.adobe.cq.commerce.api.smartlist.SmartListManager;
import com.adobe.cq.commerce.impl.JcrPaymentMethodImpl;
import com.adobe.cq.commerce.impl.JcrShippingMethodImpl;
import com.adobe.cq.commerce.impl.asset.ProductAssetManagerImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationImpl;
import com.adobe.cq.commerce.impl.classification.ClassificationManagerImpl;
import com.adobe.cq.commerce.impl.collection.ProductCollectionManagerImpl;
import com.adobe.cq.commerce.impl.promotion.JcrPromotionImpl;
import com.adobe.cq.commerce.impl.promotion.JcrVoucherImpl;
import com.adobe.cq.commerce.impl.smartlist.SmartListImpl;
import com.adobe.cq.commerce.impl.smartlist.SmartListManagerImpl;
import com.adobe.cq.social.ugc.api.UgcSearch;
import com.adobe.cq.social.ugcbase.SocialUtils;
import com.day.cq.commons.inherit.ComponentInheritanceValueMap;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.event.EventAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(specVersion="1.1")
@Service
@Property(name="service.description", value={"Default adapter factory for commerce entities"})
public class CommerceAdapterFactory
implements AdapterFactory,
CommerceProvider {
    private final Logger log = LoggerFactory.getLogger(CommerceAdapterFactory.class);
    private static final Class<SlingHttpServletRequest> REQUEST_CLASS = SlingHttpServletRequest.class;
    private static final Class<Resource> RESOURCE_CLASS = Resource.class;
    private static final Class<ResourceResolver> RESOURCE_RESOLVER_CLASS = ResourceResolver.class;
    private static final Class<CommerceService> COMMERCE_SERVICE_CLASS = CommerceService.class;
    private static final Class<Product> PRODUCT_CLASS = Product.class;
    private static final Class<Promotion> PROMOTION_CLASS = Promotion.class;
    private static final Class<PromotionHandler> PROMOTION_HANDLER_CLASS = PromotionHandler.class;
    private static final Class<PromotionManager> PROMOTION_MANAGER_CLASS = PromotionManager.class;
    private static final Class<ProductAssetManager> PRODUCT_ASSET_MANAGER_CLASS = ProductAssetManager.class;
    private static final Class<Voucher> VOUCHER_CLASS = Voucher.class;
    private static final Class<PaymentMethod> PAYMENT_METHOD_CLASS = PaymentMethod.class;
    private static final Class<ShippingMethod> SHIPPING_METHOD_CLASS = ShippingMethod.class;
    private static final Class<ProductCollection> PRODUCT_COLLECTION_CLASS = ProductCollection.class;
    private static final Class<ProductCollectionManager> PRODUCT_COLLECTION_MANAGER_CLASS = ProductCollectionManager.class;
    private static final Class<SmartListManager> SMART_LIST_MANAGER_CLASS = SmartListManager.class;
    private static final Class<SmartList> SMART_LIST_CLASS = SmartList.class;
    private static final Class<Classification> CLASSIFICATION_CLASS = Classification.class;
    private static final Class<ClassificationManager> CLASSIFICATION_MANAGER_CLASS = ClassificationManager.class;
    private static final String COMMERCE_PROVIDER_PROPERTY = "cq:commerceProvider";
    @Reference(referenceInterface=CommerceServiceFactory.class, bind="bindFactory", unbind="unbindFactory", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
    private Map<String, CommerceServiceFactory> factories = Collections.synchronizedMap(new HashMap());
    @Property(name="adapters")
    protected static final String[] ADAPTER_CLASSES = new String[]{COMMERCE_SERVICE_CLASS.getName(), PRODUCT_CLASS.getName(), PROMOTION_CLASS.getName(), PROMOTION_HANDLER_CLASS.getName(), PROMOTION_MANAGER_CLASS.getName(), VOUCHER_CLASS.getName(), PAYMENT_METHOD_CLASS.getName(), SHIPPING_METHOD_CLASS.getName(), PRODUCT_ASSET_MANAGER_CLASS.getName(), PRODUCT_COLLECTION_CLASS.getName(), PRODUCT_COLLECTION_MANAGER_CLASS.getName(), SMART_LIST_CLASS.getName(), SMART_LIST_MANAGER_CLASS.getName(), CLASSIFICATION_CLASS.getName(), CLASSIFICATION_MANAGER_CLASS.getName()};
    @Property(name="adaptables")
    protected static final String[] ADAPTABLE_CLASSES = new String[]{RESOURCE_CLASS.getName(), RESOURCE_RESOLVER_CLASS.getName(), REQUEST_CLASS.getName()};
    @Reference
    private PromotionManager promotionManager;
    @Reference
    private ProductAssetHandlerProvider productAssetHandlerProvider;
    @Reference
    private SocialUtils socialUtils;
    @Reference
    private UgcSearch ugcSearch;
    @Reference
    EventAdmin eventAdmin;

    protected void bindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
        this.factories.put((String)properties.get("commerceProvider"), factory);
    }

    protected void unbindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
        this.factories.remove((String)properties.get("commerceProvider"));
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (adaptable instanceof Resource) {
            return this.getAdapter((Resource)adaptable, type);
        }
        if (adaptable instanceof ResourceResolver) {
            return this.getAdapter((ResourceResolver)adaptable, type);
        }
        if (adaptable instanceof SlingHttpServletRequest) {
            return this.getAdapter((SlingHttpServletRequest)adaptable, type);
        }
        this.log.warn("Unable to handle adaptable {}", (Object)adaptable.getClass().getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(Resource resource, Class<AdapterType> type) {
        if (type == PRODUCT_CLASS || type == COMMERCE_SERVICE_CLASS) {
            CommerceServiceFactory factory;
            Page page = ((PageManager)resource.getResourceResolver().adaptTo(PageManager.class)).getContainingPage(resource);
            ComponentInheritanceValueMap properties = page != null ? new HierarchyNodeInheritanceValueMap(page.getContentResource()) : new ComponentInheritanceValueMap(resource);
            String provider = (String)properties.getInherited("cq:commerceProvider", String.class);
            if (provider == null && !this.factories.isEmpty()) {
                provider = this.factories.keySet().iterator().next();
            }
            if ((factory = this.factories.get(provider)) != null) {
                try {
                    CommerceService cs = factory.getCommerceService(resource);
                    if (type == COMMERCE_SERVICE_CLASS) {
                        return (AdapterType)cs;
                    }
                    return (AdapterType)cs.getProduct(resource.getPath());
                }
                catch (CommerceException e) {
                    this.log.error("Could not adapt resource: ", (Throwable)e);
                }
            }
        } else if (type == PROMOTION_CLASS) {
            try {
                return (AdapterType)new JcrPromotionImpl(resource);
            }
            catch (CommerceException e) {
                this.log.error("Could not adapt resource: ", (Throwable)e);
            }
        } else if (type == VOUCHER_CLASS) {
            try {
                return (AdapterType)new JcrVoucherImpl(resource);
            }
            catch (CommerceException e) {
                this.log.error("Could not adapt resource: ", (Throwable)e);
            }
        } else if (type == PROMOTION_HANDLER_CLASS) {
            String promoType;
            Promotion promo = (Promotion)resource.adaptTo(Promotion.class);
            if (promo != null && (promoType = promo.getType()) != null) {
                return (AdapterType)this.promotionManager.getHandler(promoType);
            }
        } else if (type == PAYMENT_METHOD_CLASS) {
            try {
                return (AdapterType)new JcrPaymentMethodImpl(resource);
            }
            catch (CommerceException e) {
                this.log.error("Could not adapt resource: ", (Throwable)e);
            }
        } else if (type == SHIPPING_METHOD_CLASS) {
            try {
                return (AdapterType)new JcrShippingMethodImpl(resource);
            }
            catch (CommerceException e) {
                this.log.error("Could not adapt resource: ", (Throwable)e);
            }
        } else {
            if (type == PRODUCT_COLLECTION_CLASS) {
                ProductCollectionManager productCollectionManager = (ProductCollectionManager)resource.getResourceResolver().adaptTo(ProductCollectionManager.class);
                return (AdapterType)productCollectionManager.getCollection(resource.getPath());
            }
            if (type == SMART_LIST_CLASS) {
                try {
                    return (AdapterType)new SmartListImpl(resource, this.eventAdmin);
                }
                catch (CommerceException e) {
                    this.log.error("Could not adapt resource: ", (Throwable)e);
                }
            } else if (type == CLASSIFICATION_CLASS) {
                try {
                    return (AdapterType)new ClassificationImpl(resource);
                }
                catch (CommerceException e) {
                    this.log.error("Could not adapt resource: ", (Throwable)e);
                }
            }
        }
        this.log.debug("Unable to adapt resource to type {}", (Object)type.getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(ResourceResolver resolver, Class<AdapterType> type) {
        if (type == PROMOTION_MANAGER_CLASS) {
            return (AdapterType)this.promotionManager;
        }
        if (type == PRODUCT_ASSET_MANAGER_CLASS) {
            return (AdapterType)new ProductAssetManagerImpl(resolver, this.productAssetHandlerProvider);
        }
        if (type == PRODUCT_COLLECTION_MANAGER_CLASS) {
            return (AdapterType)new ProductCollectionManagerImpl(resolver);
        }
        if (type == CLASSIFICATION_MANAGER_CLASS) {
            return (AdapterType)new ClassificationManagerImpl(resolver);
        }
        this.log.warn("Unable to adapt resolver to requested type {}", (Object)type.getName());
        return null;
    }

    private <AdapterType> AdapterType getAdapter(SlingHttpServletRequest request, Class<AdapterType> type) {
        if (type == SMART_LIST_MANAGER_CLASS) {
            try {
                return (AdapterType)new SmartListManagerImpl(request, this.socialUtils, this.ugcSearch, this.eventAdmin);
            }
            catch (CommerceException e) {
                this.log.error("Could not adapt resource resolver: ", (Throwable)e);
                return null;
            }
        }
        this.log.warn("Unable to adapt resolver to requested type {}", (Object)type.getName());
        return null;
    }

    @Override
    public CommerceServiceFactory getServiceFactory(String name) {
        return this.factories.get(name);
    }

    protected void bindPromotionManager(PromotionManager promotionManager) {
        this.promotionManager = promotionManager;
    }

    protected void unbindPromotionManager(PromotionManager promotionManager) {
        if (this.promotionManager == promotionManager) {
            this.promotionManager = null;
        }
    }

    protected void bindProductAssetHandlerProvider(ProductAssetHandlerProvider productAssetHandlerProvider) {
        this.productAssetHandlerProvider = productAssetHandlerProvider;
    }

    protected void unbindProductAssetHandlerProvider(ProductAssetHandlerProvider productAssetHandlerProvider) {
        if (this.productAssetHandlerProvider == productAssetHandlerProvider) {
            this.productAssetHandlerProvider = null;
        }
    }

    protected void bindSocialUtils(SocialUtils socialUtils) {
        this.socialUtils = socialUtils;
    }

    protected void unbindSocialUtils(SocialUtils socialUtils) {
        if (this.socialUtils == socialUtils) {
            this.socialUtils = null;
        }
    }

    protected void bindUgcSearch(UgcSearch ugcSearch) {
        this.ugcSearch = ugcSearch;
    }

    protected void unbindUgcSearch(UgcSearch ugcSearch) {
        if (this.ugcSearch == ugcSearch) {
            this.ugcSearch = null;
        }
    }

    protected void bindEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    protected void unbindEventAdmin(EventAdmin eventAdmin) {
        if (this.eventAdmin == eventAdmin) {
            this.eventAdmin = null;
        }
    }
}