ProductUpdateActionFactory.java 4.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.commerce.common.AbstractJcrProduct
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.ActionConfig
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveActionFactory
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.pim.impl;

import com.adobe.cq.commerce.common.AbstractJcrProduct;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.ActionConfig;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveStatus;
import javax.jcr.Node;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Property(name="service.description", value={"MSM action which updates product pages in a live copy of a product catalog"})
public class ProductUpdateActionFactory
implements LiveActionFactory<LiveAction> {
    protected static final Logger log = LoggerFactory.getLogger(ProductUpdateActionFactory.class);
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{ProductUpdateAction.class.getSimpleName(), "productUpdate"};

    public String createsAction() {
        return LIVE_ACTION_NAME[0];
    }

    public ProductUpdateAction createAction(Resource resource) {
        return new ProductUpdateAction();
    }

    private static class ProductUpdateAction
    implements LiveAction {
        private ProductUpdateAction() {
        }

        public void execute(Resource source, Resource target, LiveRelationship relation, boolean autoSave, boolean reset) throws WCMException {
            if (source != null && AbstractJcrProduct.isABaseProduct((Resource)source) && target != null && !relation.getStatus().isCancelled()) {
                try {
                    Node sourceNode = (Node)source.adaptTo(Node.class);
                    Node targetNode = (Node)target.adaptTo(Node.class);
                    if (sourceNode != null && targetNode != null && sourceNode.hasProperty("productData")) {
                        JcrUtil.copy((javax.jcr.Property)sourceNode.getProperty("productData"), (Node)targetNode, (String)null);
                        if (autoSave) {
                            targetNode.getSession().save();
                        }
                    }
                }
                catch (Exception e) {
                    ProductUpdateActionFactory.log.error("Failed to update product.", (Throwable)e);
                }
            }
        }

        public String getName() {
            return this.getClass().getSimpleName();
        }

        public String getTitle() {
            return this.getName();
        }

        public void write(JSONWriter jsonWriter) throws JSONException {
            jsonWriter.object();
            jsonWriter.key("name").value((Object)this.getName());
            jsonWriter.key("title").value((Object)this.getTitle());
            jsonWriter.endObject();
        }

        public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave) throws WCMException {
            throw new UnsupportedOperationException();
        }

        public void execute(ResourceResolver resolver, LiveRelationship relation, ActionConfig config, boolean autoSave, boolean isResetRollout) throws WCMException {
            throw new UnsupportedOperationException();
        }

        public int getRank() {
            throw new UnsupportedOperationException();
        }

        public String[] getPropertiesNames() {
            throw new UnsupportedOperationException();
        }

        public String getParameterName() {
            return null;
        }
    }

}