SourceVersionActionFactory.java 3.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.Revision
 *  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
 *  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.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Revision;
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 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.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
public class SourceVersionActionFactory
extends BaseActionFactory<BaseAction> {
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{SourceVersionActionFactory.class.getSimpleName(), "sourceVersion"};

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

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

    private static final class SourceVersionAction
    extends BaseAction {
        private static final Logger log = LoggerFactory.getLogger(SourceVersionAction.class);

        public SourceVersionAction(ValueMap config, BaseActionFactory<BaseAction> factory) {
            super(config, factory);
        }

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

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            assert (source != null);
            PageManager pm = (PageManager)source.getResourceResolver().adaptTo(PageManager.class);
            Page p = pm.getContainingPage(source.getPath());
            if (p != null) {
                pm.createRevision(p);
            } else {
                log.debug("Can not create Version for {}: is not Page", (Object)source.getPath());
            }
        }
    }

}