AbstractProductImporter.java 10.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  aQute.bnd.annotation.ConsumerType
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.pim.common;

import aQute.bnd.annotation.ConsumerType;
import com.adobe.cq.commerce.pim.api.ProductImporter;
import com.adobe.cq.commerce.pim.common.AbstractImporter;
import com.day.cq.commons.jcr.JcrUtil;
import java.io.IOException;
import java.util.Calendar;
import java.util.Dictionary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(componentAbstract=1, metatype=1)
@Service
@ConsumerType
public abstract class AbstractProductImporter
extends AbstractImporter
implements ProductImporter {
    private static final Logger log = LoggerFactory.getLogger(AbstractProductImporter.class);
    protected String basePath = "/etc/commerce/products";
    private int BUCKET_MAX;
    private static final int DEFAULT_BUCKET_SIZE = 500;
    @Property(label="Bucket Size", description="Maximum products per section before bucketing, and maximum in each bucket", intValue={500})
    public static final String BUCKET_SIZE_PROP_NAME = "cq.commerce.productimporter.bucketsize";
    protected String NN_BUCKET = "bucket";
    protected String NT_BUCKET = "sling:Folder";
    private int productCount;
    private int variationCount;

    @Activate
    @Override
    protected void activate(ComponentContext ctx) throws Exception {
        super.activate(ctx);
        this.BUCKET_MAX = PropertiesUtil.toInteger(ctx.getProperties().get("cq.commerce.productimporter.bucketsize"), (int)500);
    }

    @Override
    public void importProducts(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        long startTime = System.currentTimeMillis();
        if (!this.validateInput(request, response)) {
            return;
        }
        ResourceResolver resourceResolver = request.getResourceResolver();
        Session session = (Session)resourceResolver.adaptTo(Session.class);
        String storeName = request.getParameter("storeName");
        String storePath = request.getParameter("storePath");
        String provider = request.getParameter("provider");
        this.initTicker(request.getParameter("tickertoken"), session);
        Boolean incrementalImport = false;
        if (request.getParameter("incrementalImport") != null) {
            incrementalImport = true;
        }
        this.productCount = 0;
        this.variationCount = 0;
        this.run(resourceResolver, storePath != null ? storePath : this.basePath, storeName, incrementalImport, provider);
        long millis = System.currentTimeMillis() - startTime;
        long seconds = millis / 1000;
        if (seconds > 120) {
            log.info("Imported " + this.productCount + " products in " + seconds / 60 + " minutes.");
        } else {
            log.info("Imported " + this.productCount + " products in " + seconds + " seconds.");
        }
        String summary = "" + this.productCount + " products and " + this.variationCount + " variants created/updated.";
        if (this.getErrorCount() > 0) {
            summary = summary + " " + this.getErrorCount() + " errors encountered.";
        }
        this.respondWithMessages(response, summary);
    }

    protected abstract boolean validateInput(SlingHttpServletRequest var1, SlingHttpServletResponse var2) throws IOException;

    private void demoteProductChildrenToBucket(Node parent, Session session) throws RepositoryException {
        Node bucket = JcrUtil.createUniqueNode((Node)parent, (String)this.NN_BUCKET, (String)this.NT_BUCKET, (Session)session);
        NodeIterator children = parent.getNodes();
        long productCount = 0;
        while (children.hasNext()) {
            Node child = (Node)children.next();
            if (!child.hasProperty("cq:commerceType") || !child.getProperty("cq:commerceType").getString().equals("product")) continue;
            String oldPath = child.getPath();
            String newPath = JcrUtil.copy((Node)child, (Node)bucket, (String)child.getName()).getPath();
            child.remove();
            this.updateLoggedEvents(oldPath, newPath);
            ++productCount;
        }
        bucket.setProperty("cq:importCount", productCount);
        parent.setProperty("cq:importCount", (Value)null);
    }

    protected Node createProduct(String path, Session session) throws RepositoryException {
        long count;
        String parentPath = Text.getRelativeParent((String)path, (int)1);
        Node parent = JcrUtil.createPath((String)parentPath, (boolean)false, (String)"sling:Folder", (String)"sling:Folder", (Session)session, (boolean)false);
        boolean bucketing = false;
        if (parent.hasProperty("cq:importBucket")) {
            parent = parent.getNode(parent.getProperty("cq:importBucket").getString());
            bucketing = true;
        }
        long l = count = parent.hasProperty("cq:importCount") ? parent.getProperty("cq:importCount").getLong() + 1 : 1;
        if (count > (long)this.BUCKET_MAX) {
            if (!bucketing) {
                this.demoteProductChildrenToBucket(parent, session);
            } else {
                parent = parent.getParent();
            }
            Node bucket = JcrUtil.createUniqueNode((Node)parent, (String)this.NN_BUCKET, (String)this.NT_BUCKET, (Session)session);
            parent.setProperty("cq:importBucket", bucket.getName());
            parent = bucket;
            count = 1;
        }
        parent.setProperty("cq:importCount", count);
        Node product = JcrUtil.createUniqueNode((Node)parent, (String)Text.getName((String)path), (String)"nt:unstructured", (Session)session);
        product.setProperty("cq:commerceType", "product");
        product.setProperty("sling:resourceType", "commerce/components/product");
        product.setProperty("jcr:lastModified", Calendar.getInstance());
        ++this.productCount;
        this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_ADDED", product.getPath());
        this.logMessage("Created product   " + product.getPath(), false);
        this.updateTicker(this.makeTickerMessage());
        this.checkpoint(session, false);
        return product;
    }

    protected String makeTickerMessage() {
        return "" + this.productCount + " products imported/updated";
    }

    protected void productUpdated(Node product) throws RepositoryException {
        ++this.productCount;
        this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", product.getPath());
        this.logMessage("Updated product   " + product.getPath(), false);
        this.updateTicker(this.makeTickerMessage());
        this.checkpoint(product.getSession(), false);
    }

    protected void productDeleted(Node product) throws RepositoryException {
        this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_DELETED", product.getPath());
        this.logMessage("Deleted product " + product.getPath(), false);
        this.updateTicker(this.makeTickerMessage());
        this.checkpoint(product.getSession(), false);
    }

    protected Node createVariant(Node parentProduct, String name) throws RepositoryException {
        Node variant = JcrUtil.createUniqueNode((Node)parentProduct, (String)name, (String)"nt:unstructured", (Session)parentProduct.getSession());
        variant.setProperty("cq:commerceType", "variant");
        variant.setProperty("sling:resourceType", "commerce/components/product");
        variant.setProperty("jcr:lastModified", Calendar.getInstance());
        ++this.variationCount;
        Node baseProduct = this.getBaseProduct(parentProduct);
        if (baseProduct != null) {
            this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
        }
        this.logMessage("Created variation " + variant.getPath(), false);
        this.checkpoint(parentProduct.getSession(), false);
        return variant;
    }

    protected void variantUpdated(Node variant) throws RepositoryException {
        ++this.variationCount;
        Node baseProduct = this.getBaseProduct(variant);
        if (baseProduct != null) {
            this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
        }
        this.logMessage("Updated variation " + variant.getPath(), false);
        this.updateTicker(this.makeTickerMessage());
        this.checkpoint(variant.getSession(), false);
    }

    protected Node createImage(Node product) throws RepositoryException {
        Node image = product.addNode("image", "nt:unstructured");
        image.setProperty("sling:resourceType", "commerce/components/product/image");
        image.setProperty("jcr:lastModified", Calendar.getInstance());
        Node baseProduct = this.getBaseProduct(product);
        if (baseProduct != null) {
            this.logEvent("com/adobe/cq/commerce/pim/PRODUCT_MODIFIED", baseProduct.getPath());
        }
        this.logMessage("Created image     " + image.getPath(), false);
        this.checkpoint(product.getSession(), false);
        return image;
    }

    protected Node getBaseProduct(Node node) throws RepositoryException {
        while (node != null && !node.getProperty("cq:commerceType").getString().equals("product")) {
            node = node.getParent();
        }
        return node;
    }
}