ReferencesUpdateActionFactory.java 9.87 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.commons.ReferenceSearch
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveCopy
 *  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.FilteredActionFactoryBase
 *  com.day.cq.wcm.msm.commons.ItemFilterImpl
 *  com.day.cq.wcm.msm.commons.ItemFilterUtil
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  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.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.util.Text
 *  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
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveCopy;
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.FilteredActionFactoryBase;
import com.day.cq.wcm.msm.commons.ItemFilterImpl;
import com.day.cq.wcm.msm.commons.ItemFilterUtil;
import com.day.cq.wcm.msm.impl.LiveCopyManagerImpl;
import com.day.cq.wcm.msm.impl.LiveCopyServiceImpl;
import com.day.cq.wcm.msm.impl.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, label="%cq.wcm.msm.impl.actions.referencesupdate.label", description="%cq.wcm.msm.impl.actions.referencesupdate.description")
@Service
public class ReferencesUpdateActionFactory
extends FilteredActionFactoryBase<ReferencesUpdateAction> {
    @org.apache.felix.scr.annotations.Property(name="liveActionName", propertyPrivate=1)
    private static final String[] LIVE_ACTION_NAME = new String[]{ReferencesUpdateAction.class.getSimpleName(), "referencesUpdate"};
    @org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludednodetypes")
    private static final String[] DEFAULT_FILTER_NODE_TYPES = ItemFilterUtil.EMPTY_PROPERTIES;
    @org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludedparagraphitems")
    private static final String[] DEFAULT_EXCLUDED_PARAGRAPH_ITEMS = ItemFilterUtil.EMPTY_PROPERTIES;
    @org.apache.felix.scr.annotations.Property(name="cq.wcm.msm.action.excludedprops")
    private static final String[] DEFAULT_EXCLUDED_PROPERTIES = ItemFilterUtil.EMPTY_PROPERTIES;
    private static final boolean NESTED_DEFAULT = false;
    @org.apache.felix.scr.annotations.Property(boolValue={0})
    static final String PN_ADAPT_NESTED = "cq.wcm.msm.impl.action.referencesupdate.prop_updateNested";
    private static final Logger log = LoggerFactory.getLogger(ReferencesUpdateActionFactory.class);
    @Reference
    private LiveCopyServiceImpl liveCopyService = null;
    @Reference
    private RolloutManager rolloutManager = null;
    private boolean updateNestedLiveCopies;

    protected ReferencesUpdateAction newActionInstance(ValueMap config) {
        return new ReferencesUpdateAction(config, this, this.liveCopyService);
    }

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

    @Activate
    @Modified
    protected void configure(ComponentContext context) {
        this.setupFilter(context, this.rolloutManager);
        this.updateNestedLiveCopies = PropertiesUtil.toBoolean(context.getProperties().get("cq.wcm.msm.impl.action.referencesupdate.prop_updateNested"), (boolean)false);
    }

    protected void bindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        this.liveCopyService = liveCopyServiceImpl;
    }

    protected void unbindLiveCopyService(LiveCopyServiceImpl liveCopyServiceImpl) {
        if (this.liveCopyService == liveCopyServiceImpl) {
            this.liveCopyService = null;
        }
    }

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

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

    static final class ReferencesUpdateAction
    extends FilteredAction {
        private static final Collection<? extends String> DEFAULT_EXCLUSION = Collections.singleton("cq:master");
        private final LiveCopyServiceImpl liveCopyService;
        private final Boolean updateNested;

        private ReferencesUpdateAction(ValueMap config, ReferencesUpdateActionFactory factory, LiveCopyServiceImpl lcService) {
            super(config, factory.getPagePropertyFilter(), factory.getComponentFilter(), (BaseActionFactory)factory);
            this.liveCopyService = lcService;
            this.updateNested = config == null ? factory.updateNestedLiveCopies : (Boolean)config.get("cq.wcm.msm.impl.action.referencesupdate.prop_updateNested", (Object)factory.updateNestedLiveCopies);
        }

        protected boolean doHandle(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            return Utils.resourceHasNode(source) && Utils.resourceHasNode(target);
        }

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            Node masterNode = Utils.getWorkingNode((Node)source.adaptTo(Node.class));
            Node slaveNode = Utils.getWorkingNode((Node)target.adaptTo(Node.class));
            Iterator mpi = this.getFilteredProperties(masterNode);
            if (mpi.hasNext()) {
                Set<String> excluded = this.buildExclusionSet(slaveNode, relation.getStatus().getCanceledProperties());
                ArrayList<LiveCopy> all = new ArrayList<LiveCopy>();
                if (this.updateNested()) {
                    LiveCopyManagerImpl lcManager = this.liveCopyService.createLiveCopyManager(target.getResourceResolver());
                    ReferencesUpdateAction.collectRoots(lcManager, relation.getLiveCopy(), all);
                }
                all.add(relation.getLiveCopy());
                for (LiveCopy liveCopy : all) {
                    new ReferenceSearch().adjustReferences(slaveNode, liveCopy.getBlueprintPath(), liveCopy.getPath(), true, excluded);
                }
                log.debug("Executed references adjustment for {}", (Object)slaveNode.getPath());
            }
        }

        boolean updateNested() {
            return this.updateNested;
        }

        private Set<String> buildExclusionSet(Node slaveNode, List<String> canceledProperties) throws RepositoryException {
            HashSet<String> exclusion = new HashSet<String>();
            ItemFilterImpl itemFilter = this.getFilter(slaveNode);
            PropertyIterator itr = slaveNode.getProperties();
            while (itr.hasNext()) {
                Property prop = itr.nextProperty();
                if (!itemFilter.excludes(prop)) continue;
                exclusion.add(prop.getName());
            }
            exclusion.addAll(canceledProperties);
            exclusion.addAll(DEFAULT_EXCLUSION);
            return exclusion;
        }

        private static void collectRoots(LiveCopyManagerImpl lcManager, LiveCopy liveCopy, ArrayList<LiveCopy> all) throws WCMException, RepositoryException {
            LiveCopyManagerImpl.LiveCopyImpl rootLc;
            String rootPath = liveCopy.getPath();
            String parent = Text.getRelativeParent((String)rootPath, (int)1);
            while ((rootLc = lcManager.getContainingLiveCopy(parent)) != null && !(parent = Text.getRelativeParent((String)(rootPath = rootLc.getPath()), (int)1)).equals("/")) {
            }
            LiveCopyManagerImpl.LiveCopyImpl previous = null;
            for (LiveCopyManagerImpl.LiveCopyImpl lc : lcManager.getLiveCopiesInBranch(rootPath)) {
                if (previous == null) {
                    all.add(lc);
                } else if (Text.isDescendant((String)previous.getPath(), (String)lc.getPath())) {
                    String relPath = Utils.relativePath(previous.getPath(), lc.getPath());
                    if (!Utils.appendPath(previous.getBlueprintPath(), relPath).equals(lc.getBlueprintPath())) {
                        all.add(lc);
                    }
                } else {
                    all.add(lc);
                }
                previous = lc;
            }
        }
    }

}