TargetActivateActionFactory.java 4.06 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.Replicator
 *  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.commons.BaseAction
 *  com.day.cq.wcm.msm.commons.BaseActionFactory
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
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.commons.BaseAction;
import com.day.cq.wcm.msm.commons.BaseActionFactory;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

@Component(metatype=0)
@Service
public class TargetActivateActionFactory
extends BaseActionFactory<BaseAction> {
    @Reference
    private Replicator replicator = null;
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{TargetActivateAction.class.getSimpleName(), "targetActivate"};

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

    protected TargetActivateAction newActionInstance(ValueMap config) {
        return new TargetActivateAction(config, this, this.replicator);
    }

    protected void bindReplicator(Replicator replicator) {
        this.replicator = replicator;
    }

    protected void unbindReplicator(Replicator replicator) {
        if (this.replicator == replicator) {
            this.replicator = null;
        }
    }

    private static final class TargetActivateAction
    extends BaseAction {
        private final Replicator replicator;

        private TargetActivateAction(ValueMap config, BaseActionFactory<BaseAction> factory, Replicator replicator) {
            super(config, factory);
            this.replicator = replicator;
        }

        protected boolean handles(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws WCMException, RepositoryException {
            return target != null && relation.getStatus().isPage() && this.replicator != null;
        }

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            assert (target != null);
            try {
                Page p;
                ResourceResolver resolver = target.getResourceResolver();
                PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
                Page page = p = pm != null ? pm.getContainingPage(target.getPath()) : null;
                if (p != null) {
                    Replicator localReplicator = this.replicator;
                    localReplicator.replicate((Session)resolver.adaptTo(Session.class), ReplicationActionType.ACTIVATE, p.getPath());
                }
            }
            catch (ReplicationException e) {
                throw new WCMException((Throwable)e);
            }
        }
    }

}