LaunchLiveActionFactoryBase.java 6.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveAction
 *  com.day.cq.wcm.msm.api.LiveActionFactory
 *  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.PropertyUnbounded
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.cq.wcm.launches.impl.msm.utils;

import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchExclusionConfig;
import com.adobe.cq.wcm.launches.impl.msm.utils.LaunchLiveActionBase;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveAction;
import com.day.cq.wcm.msm.api.LiveActionFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component(metatype=0, componentAbstract=1)
public abstract class LaunchLiveActionFactoryBase<Type extends LaunchLiveActionBase>
implements LiveActionFactory<Type> {
    private static final String[] EMPTY_CONFIG = new String[0];
    public static final String PARAMETER_EXCLUDED_PROPERTIES = "cq.wcm.msm.action.excludedprops";
    @Property(name="cq.wcm.msm.action.excludedprops", unbounded=PropertyUnbounded.ARRAY)
    public static String[] EXCLUDED_PROPERTIES_DEFAULT = new String[]{"jcr:.*", "sling:.*", "cq:.*"};
    public static final String PARAMETER_EXCLUDED_PARAGRAPH_ITEMS = "cq.wcm.msm.action.excludedparagraphitems";
    @Property(name="cq.wcm.msm.action.excludedparagraphitems", unbounded=PropertyUnbounded.ARRAY)
    public static final String[] EXCLUDED_PARAGRAPH_ITEMS_DEFAULT = new String[]{"cq:propertyInheritanceCancelled"};
    public static final String PARAMETER_EXCLUDED_NODE_TYPES = "cq.wcm.msm.action.excludednodetypes";
    @Property(name="cq.wcm.msm.action.excludednodetypes", unbounded=PropertyUnbounded.ARRAY)
    public static final String[] EXCLUDED_NODE_TYPES_DEFAULT = new String[]{"cq:LiveSyncAction", "cq:LiveSyncConfig", "cq:BlueprintSyncConfig"};
    public static final String PARAMETER_RESERVED_PROPERTIES = "cq.wcm.msm.action.reservedproperties";
    @Property(name="cq.wcm.msm.action.reservedproperties", unbounded=PropertyUnbounded.ARRAY)
    public static final String[] RESERVED_PROPERTIES_DEFAULT = new String[]{"cq:isCancelledForChildren", "cq:lastRolledout", "cq:lastRolledoutBy"};
    private LaunchExclusionConfig exclusionConfig;
    private Dictionary props;

    @Activate
    @Modified
    protected void activate(ComponentContext context) {
        this.props = context.getProperties();
        String[] pageProperties = PropertiesUtil.toStringArray(context.getProperties().get("cq.wcm.msm.action.excludedprops"), (String[])EMPTY_CONFIG);
        String[] paragraphItems = PropertiesUtil.toStringArray(context.getProperties().get("cq.wcm.msm.action.excludedparagraphitems"), (String[])EMPTY_CONFIG);
        String[] nodeTypes = PropertiesUtil.toStringArray(context.getProperties().get("cq.wcm.msm.action.excludednodetypes"), (String[])EMPTY_CONFIG);
        List<String> reservedProperties = Arrays.asList(PropertiesUtil.toStringArray(context.getProperties().get("cq.wcm.msm.action.reservedproperties"), (String[])EMPTY_CONFIG));
        HashSet<String> excludedPageProperties = new HashSet<String>(Arrays.asList(pageProperties));
        excludedPageProperties.addAll(reservedProperties);
        HashSet<String> excludedParagraphItems = new HashSet<String>(Arrays.asList(paragraphItems));
        excludedParagraphItems.addAll(reservedProperties);
        HashSet<String> excludedNodeTypes = new HashSet<String>(Arrays.asList(nodeTypes));
        this.exclusionConfig = new LaunchExclusionConfig(excludedPageProperties, excludedParagraphItems, excludedNodeTypes);
    }

    public Type createAction(Resource resource) throws WCMException {
        final ValueMap resourceMap = resource != null ? (ValueMap)resource.adaptTo(ValueMap.class) : null;
        ValueMapDecorator map = new ValueMapDecorator(this.getLegacyProperties()){

            public <T> T get(String name, Class<T> type) {
                if (resourceMap != null && resourceMap.containsKey((Object)name)) {
                    return (T)resourceMap.get(name, type);
                }
                return (T)super.get(name, type);
            }

            public <T> T get(String name, T defaultValue) {
                if (resourceMap != null && resourceMap.containsKey((Object)name)) {
                    return (T)resourceMap.get(name, defaultValue);
                }
                return (T)super.get(name, defaultValue);
            }

            public Object get(Object key) {
                if (resourceMap != null && resourceMap.containsKey(key)) {
                    return resourceMap.get(key);
                }
                return super.get(key);
            }

            public boolean containsKey(Object key) {
                if (resourceMap != null && resourceMap.containsKey(key)) {
                    return true;
                }
                return super.containsKey(key);
            }
        };
        return this.createAction((ValueMap)map, this.exclusionConfig);
    }

    protected abstract Type createAction(ValueMap var1, LaunchExclusionConfig var2);

    private Map<String, Object> getLegacyProperties() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("cq.wcm.msm.action.rank", this.props.get("cq.wcm.msm.action.rank"));
        map.put("cq.wcm.msm.action.properties", PropertiesUtil.toStringArray(this.props.get("cq.wcm.msm.action.properties")));
        map.put("cq.wcm.msm.action.parameter", this.props.get("cq.wcm.msm.action.parameter"));
        map.put("cq.wcm.msm.action.title", this.props.get("cq.wcm.msm.action.title"));
        map.put("actiontype", this.props.get("actiontype"));
        return map;
    }

}