NotifyActionFactory.java 3.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.PageEvent
 *  com.day.cq.wcm.api.PageModification
 *  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.commons.lang.StringUtils
 *  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
 *  org.osgi.service.event.Event
 *  org.osgi.service.event.EventAdmin
 */
package com.day.cq.wcm.msm.impl.actions;

import com.day.cq.wcm.api.PageEvent;
import com.day.cq.wcm.api.PageModification;
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 com.day.cq.wcm.msm.impl.Utils;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
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;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

@Component(metatype=0)
@Service
public class NotifyActionFactory
extends BaseActionFactory<BaseAction> {
    @Reference
    private EventAdmin eventAdmin = null;
    @Property(name="liveActionName")
    private static final String[] LIVE_ACTION_NAME = new String[]{NotifyAction.class.getSimpleName(), "notify"};

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

    protected NotifyAction newActionInstance(ValueMap config) {
        return new NotifyAction(config, this, this.eventAdmin);
    }

    protected void bindEventAdmin(EventAdmin eventAdmin) {
        this.eventAdmin = eventAdmin;
    }

    protected void unbindEventAdmin(EventAdmin eventAdmin) {
        if (this.eventAdmin == eventAdmin) {
            this.eventAdmin = null;
        }
    }

    private static class NotifyAction
    extends BaseAction {
        private final EventAdmin eventAdmin;

        private NotifyAction(ValueMap config, BaseActionFactory<BaseAction> factory, EventAdmin eventAdmin) {
            super(config, factory);
            this.eventAdmin = eventAdmin;
        }

        protected boolean handles(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws WCMException, RepositoryException {
            return this.eventAdmin != null && target != null && relation.getStatus().isPage() && StringUtils.isNotEmpty((String)target.getResourceResolver().getUserID());
        }

        protected void doExecute(Resource source, Resource target, LiveRelationship relation, boolean resetRollout) throws RepositoryException, WCMException {
            assert (target != null);
            String userID = target.getResourceResolver().getUserID();
            ArrayList<PageModification> mods = new ArrayList<PageModification>();
            PageModification mod = PageModification.rolledout((String)Utils.getPagePath(target.getPath()), (String)userID);
            mods.add(mod);
            PageEvent event = new PageEvent(mods);
            this.eventAdmin.postEvent(event.toEvent());
        }
    }

}