ContentUpdateActionFactory.java 9.56 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.commons.BaseActionFactory
 *  com.day.cq.wcm.msm.commons.FilteredAction
 *  com.day.cq.wcm.msm.commons.ItemFilterImpl
 *  com.day.cq.wcm.msm.commons.ItemFilterUtil
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.nodetype.NodeType
 *  javax.jcr.nodetype.PropertyDefinition
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Modified
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.commons.BaseActionFactory;
import com.day.cq.wcm.msm.commons.FilteredAction;
import com.day.cq.wcm.msm.commons.ItemFilterImpl;
import com.day.cq.wcm.msm.commons.ItemFilterUtil;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.PropertyDefinition;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
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.api.resource.ValueMap;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component(metatype=1, label="%cq.wcm.msm.impl.actions.contentupdate.label", description="%cq.wcm.msm.impl.actions.contentupdate.description")
@Service
public class ContentUpdateActionFactory
extends BaseActionFactory<FilteredAction> {
    @Deprecated
    @Property(name="cq.wcm.msm.action.rank", propertyPrivate=1)
    private static final int RANK = 303;
    @Deprecated
    @Property(name="cq.wcm.msm.action.title", propertyPrivate=1)
    private static final String TITLE = "Update content";
    @Deprecated
    @Property(name="cq.wcm.msm.action.parameter", propertyPrivate=1)
    private static final String PARAMETER = "msm:actionContentUpdate";
    @Deprecated
    @Property(name="actiontype", propertyPrivate=1)
    private static final String ACTION_TYPE = "contentupdate";
    @Property(name="cq.wcm.msm.action.excludednodetypes")
    private static final String[] DEFAULT_FILTER_NODE_TYPES = ItemFilterUtil.EMPTY_PROPERTIES;
    @Property(name="cq.wcm.msm.action.excludedparagraphitems")
    private static final String[] DEFAULT_EXCLUDED_PARAGRAPH_ITEMS = ItemFilterUtil.EMPTY_PROPERTIES;
    @Property(name="cq.wcm.msm.action.excludedprops")
    private static final String[] DEFAULT_EXCLUDED_PROPERTIES = ItemFilterUtil.EMPTY_PROPERTIES;
    public static final String IGNORED_MIXINS = "cq.wcm.msm.action.ignoredMixin";
    @Property(name="cq.wcm.msm.action.ignoredMixin", unbounded=PropertyUnbounded.ARRAY)
    public static final String[] EXCLUDED_MIXINS_DEFAULT = new String[]{".*"};
    @Property(name="liveActionName", propertyPrivate=1)
    private static final String[] LIVE_ACTION_NAME = new String[]{ContentUpdateAction.class.getSimpleName(), "contentUpdate"};
    @Reference
    private RolloutManager rolloutManager = null;
    private Dictionary properties;
    private ItemFilterImpl excludedMixinFilter;

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

    @Activate
    @Modified
    protected void configure(ComponentContext context) {
        this.properties = context.getProperties();
        this.excludedMixinFilter = ItemFilterUtil.createFilter((String[])PropertiesUtil.toStringArray(context.getProperties().get("cq.wcm.msm.action.ignoredMixin"), (String[])EXCLUDED_MIXINS_DEFAULT), (String[])null, (String[])null, (RolloutManager)this.getRolloutManager());
    }

    protected Dictionary getFactoryConfig() {
        return this.properties;
    }

    protected ContentUpdateAction newActionInstance(ValueMap config) {
        return new ContentUpdateAction(config, this);
    }

    private RolloutManager getRolloutManager() {
        return this.rolloutManager;
    }

    private ItemFilterImpl getExcludedMixins() {
        return this.excludedMixinFilter;
    }

    protected void bindRolloutManager(RolloutManager rolloutManager) {
        this.rolloutManager = rolloutManager;
    }

    protected void unbindRolloutManager(RolloutManager rolloutManager) {
        if (this.rolloutManager == rolloutManager) {
            this.rolloutManager = null;
        }
    }

    private static final class ContentUpdateAction
    extends FilteredAction {
        private final ItemFilterImpl mixinFilter;

        public ContentUpdateAction(ValueMap config, ContentUpdateActionFactory factory) {
            super(config, ItemFilterUtil.createPageFilter((ValueMap)config, (RolloutManager)factory.rolloutManager), ItemFilterUtil.createComponentFilter((ValueMap)config, (RolloutManager)factory.rolloutManager), (BaseActionFactory)factory);
            this.mixinFilter = factory.getExcludedMixins();
        }

        protected boolean doHandle(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            return relation.getStatus().isSourceExisting() && source != null && target != null && Utils.getWorkingNode((Node)source.adaptTo(Node.class)) != null && Utils.getWorkingNode((Node)target.adaptTo(Node.class)) != null;
        }

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            boolean updated = false;
            Node masterNode = Utils.getWorkingNode((Node)source.adaptTo(Node.class));
            Node slaveNode = Utils.getWorkingNode((Node)target.adaptTo(Node.class));
            NodeType[] masterMixins = masterNode.getMixinNodeTypes();
            ArrayList<NodeType> slaveMixins = new ArrayList<NodeType>(Arrays.asList(slaveNode.getMixinNodeTypes()));
            for (NodeType masterMixin : masterMixins) {
                if (!this.mixinFilter.excludesNodeType(masterMixin.getName()) && !slaveNode.isNodeType(masterMixin.getName())) {
                    slaveNode.addMixin(masterMixin.getName());
                    updated = true;
                }
                slaveMixins.remove((Object)masterMixin);
            }
            for (NodeType slaveMixin : slaveMixins) {
                String name = slaveMixin.getName();
                if (this.mixinFilter.excludesNodeType(name)) continue;
                slaveNode.removeMixin(name);
                updated = true;
            }
            HashSet<String> inspectedProperties = new HashSet<String>();
            Iterator mpi = this.getFilteredProperties(masterNode);
            while (mpi.hasNext()) {
                javax.jcr.Property property = (javax.jcr.Property)mpi.next();
                inspectedProperties.add(property.getName());
                if (relation.getStatus().getCanceledProperties().contains(property.getName())) continue;
                if (!relation.getStatus().isPage() && property.getName().equals("jcr:lastModified")) {
                    JcrUtil.setProperty((Node)slaveNode, (String)"jcr:lastModified", (Object)Calendar.getInstance());
                } else {
                    JcrUtil.copy((javax.jcr.Property)property, (Node)slaveNode, (String)null);
                }
                updated = true;
            }
            Iterator pItr = this.getFilteredProperties(slaveNode);
            while (pItr.hasNext()) {
                javax.jcr.Property prop = (javax.jcr.Property)pItr.next();
                if (inspectedProperties.remove(prop.getName()) || prop.getDefinition().isAutoCreated() || prop.getDefinition().isProtected() || relation.getStatus().getCanceledProperties().contains(prop.getName())) continue;
                prop.remove();
                updated = true;
            }
            if (updated) {
                Page page;
                PageManager pm = (PageManager)source.getResourceResolver().adaptTo(PageManager.class);
                Page page2 = page = pm == null ? null : pm.getContainingPage(slaveNode.getPath());
                if (page != null) {
                    pm.touch((Node)page.adaptTo(Node.class), true, Calendar.getInstance(), false);
                }
            }
        }
    }

}