MarkLiveRelationshipActionFactory.java 4.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  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.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl.msm;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchExclusionConfig;
import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchLiveActionBase;
import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchLiveActionFactoryBase;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
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.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
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.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class MarkLiveRelationshipActionFactory
extends LaunchLiveActionFactoryBase<LaunchLiveActionBase> {
    protected static final Logger log = LoggerFactory.getLogger(MarkLiveRelationshipActionFactory.class);
    @Reference
    private LiveRelationshipManager liveRelationshipManager = null;
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{MarkLiveRelationshipLiveAction.class.getSimpleName(), "markLiveRelationship"};

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

    @Override
    protected MarkLiveRelationshipLiveAction createAction(ValueMap config, LaunchExclusionConfig exclusionConfig) {
        return new MarkLiveRelationshipLiveAction(config, exclusionConfig, this.liveRelationshipManager);
    }

    protected void bindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.liveRelationshipManager = liveRelationshipManager;
    }

    protected void unbindLiveRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.liveRelationshipManager == liveRelationshipManager) {
            this.liveRelationshipManager = null;
        }
    }

    private static class MarkLiveRelationshipLiveAction
    extends LaunchLiveActionBase {
        private final LiveRelationshipManager liveRelationshipManager;

        private MarkLiveRelationshipLiveAction(ValueMap config, LaunchExclusionConfig exclusionConfig, LiveRelationshipManager relManager) {
            super(config, exclusionConfig);
            this.liveRelationshipManager = relManager;
        }

        @Override
        protected LiveAction newInstance(ValueMap config) {
            return new MarkLiveRelationshipLiveAction(config, this.getExclusionConfig(), this.liveRelationshipManager);
        }

        @Override
        protected boolean handles(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws WCMException, RepositoryException {
            Resource launchResource;
            if (relation.getStatus().isSourceExisting() && !relation.getStatus().isTargetExisting() && (launchResource = LaunchUtils.getLaunchResource(source)) != null) {
                Launch launch = (Launch)launchResource.adaptTo(Launch.class);
                return launch != null && launch.isLiveCopy();
            }
            return false;
        }

        @Override
        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean autoSave, boolean resetRollout) throws RepositoryException, WCMException {
            Node sourceNode = (Node)source.adaptTo(Node.class);
            if (!sourceNode.isNodeType("cq:LiveSyncConfig") && !sourceNode.isNodeType("cq:LiveRelationship") && sourceNode.canAddMixin("cq:LiveRelationship")) {
                sourceNode.addMixin("cq:LiveRelationship");
            }
        }
    }

}