FrameworkComponentImpl.java 8.89 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  org.apache.commons.collections.Factory
 *  org.apache.commons.collections.map.MultiValueMap
 *  org.apache.commons.lang.ArrayUtils
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 */
package com.day.cq.analytics.sitecatalyst.impl;

import com.day.cq.analytics.impl.AnalyticsComponent;
import com.day.cq.analytics.impl.AnalyticsComponentService;
import com.day.cq.analytics.sitecatalyst.FrameworkComponent;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;

public class FrameworkComponentImpl
extends HierarchyNodeInheritanceValueMap
implements FrameworkComponent {
    private static final String RT_TARGET_CONTEXTHUB_STORE = "cq/analytics/components/mappings/cbcontexthubmappings";
    private static final String RT_ANALYTICS_CONTEXTHUB_STORE = "cq/analytics/components/mappings/cqcontexthubmappings";
    private MultiValueMap cqMappings = null;
    protected String[] storeTypes = new String[0];
    protected String[] trackedVars = new String[0];
    protected String[] trackedEvents = new String[0];
    protected String[] trackedVarsEvents = new String[0];
    protected String defaultStore;
    protected String defaultEventStore;
    protected Resource resource;
    protected ValueMap cqComp;
    private Set<String> keySet = null;
    private final Factory factory;
    private AnalyticsComponentService componentService;

    public FrameworkComponentImpl(Resource mappingComp, String[] storeTypes, AnalyticsComponentService componentService) {
        super(mappingComp);
        this.factory = new Factory(){

            public Object create() {
                return new HashSet<String>(){

                    @Override
                    public String toString() {
                        return StringUtils.join((Collection)this, (String)",");
                    }
                };
            }

        };
        this.resource = mappingComp;
        this.storeTypes = storeTypes;
        this.componentService = componentService;
        this.initFramework();
        this.trackedVarsEvents = (String[])ArrayUtils.addAll((Object[])this.qualifyCqVars(this.trackedVars, this.defaultStore), (Object[])this.qualifyCqVars(this.trackedEvents, this.defaultEventStore));
    }

    private void initFramework() {
        if (this.isContextHubStore(this.resource)) {
            this.defaultStore = (String)this.get("cq:defaultStore", (Object)"eventdata");
            this.defaultEventStore = (String)this.get("cq:defaultEventStore", (Object)"eventdata.events");
            this.trackedVars = (String[])this.get("cq:trackvars", (Object)new String[0]);
            this.trackedEvents = new String[0];
            this.cqComp = this.resource.getValueMap();
        } else {
            AnalyticsComponent component = this.componentService.getAnalyticsComponent((String)this.get("cq:componentPath", String.class));
            if (component != null) {
                this.defaultStore = component.getDefaultStore();
                this.defaultEventStore = component.getDefaultEventStore();
                this.trackedVars = component.getTrackedVars();
                this.trackedEvents = component.getTrackedEvents();
                this.cqComp = component.getAnalyticsProps();
            }
        }
    }

    private boolean isContextHubStore(Resource resource) {
        return resource.isResourceType("cq/analytics/components/mappings/cbcontexthubmappings") || resource.isResourceType("cq/analytics/components/mappings/cqcontexthubmappings");
    }

    protected String[] qualifyCqVars(String[] vars, String defaultStore) {
        String[] qualified = (String[])ArrayUtils.clone((Object[])vars);
        for (int i = 0; i < qualified.length; ++i) {
            String[] valueParts = qualified[i].split("\\.");
            if (valueParts.length != 0 && ArrayUtils.contains((Object[])this.storeTypes, (Object)qualified[i].split("\\.")[0])) continue;
            qualified[i] = defaultStore + "." + qualified[i].trim();
        }
        return qualified;
    }

    @Override
    public String getType() {
        return this.resource.getResourceType();
    }

    @Override
    public String getPath() {
        return this.resource.getPath();
    }

    @Override
    public String[] getTrackedVarsEvents() {
        return this.trackedVarsEvents;
    }

    @Override
    public MultiValueMap getCqMappings() {
        if (this.cqMappings == null) {
            this.cqMappings = MultiValueMap.decorate(new HashMap(), (Factory)this.factory);
            for (String scVar : this.inheritedKeySet()) {
                String cqVar = (String)this.getInherited(scVar, (Object)"");
                if (cqVar.length() > 0) {
                    this.cqMappings.put((Object)cqVar, (Object)scVar);
                    continue;
                }
                for (String var : this.getTrackedVarsEvents()) {
                    if (!this.cqMappings.containsValue((Object)var, (Object)scVar)) continue;
                    this.cqMappings.remove((Object)var, (Object)scVar);
                }
            }
        }
        return this.cqMappings;
    }

    @Override
    public JSONArray getScVars(String cqVar) {
        return new JSONArray((Collection)this.getCqMappings().get((Object)cqVar));
    }

    public <T> T getInherited(String scKey, Class<T> type) {
        Object value = super.getInherited(scKey, type);
        if (value == null) {
            value = this.cqComp.get((Object)scKey);
        }
        String[] valueParts = ((String)value).split("\\.");
        if (!(scKey.contains(":") || valueParts.length != 0 && ArrayUtils.contains((Object[])this.storeTypes, (Object)valueParts[0]))) {
            if (ArrayUtils.contains((Object[])this.trackedVars, (Object)value)) {
                value = this.defaultStore + "." + value;
            } else if (ArrayUtils.contains((Object[])this.trackedEvents, (Object)value)) {
                value = this.defaultEventStore + "." + value;
            }
        }
        return (T)((String)value).trim();
    }

    public Object get(Object key) {
        String cqVar = (String)super.get(key);
        if (cqVar == null) {
            return null;
        }
        String[] valueParts = cqVar.split("\\.");
        if (!(((String)key).contains(":") || valueParts.length != 0 && ArrayUtils.contains((Object[])this.storeTypes, (Object)cqVar.split("\\.")[0]))) {
            if (ArrayUtils.contains((Object[])this.trackedVars, (Object)cqVar)) {
                cqVar = this.defaultStore + "." + cqVar;
            } else if (ArrayUtils.contains((Object[])this.trackedEvents, (Object)cqVar)) {
                cqVar = this.defaultEventStore + "." + cqVar;
            }
        }
        return cqVar.trim();
    }

    public Set<String> keySet() {
        if (this.keySet == null) {
            this.keySet = new HashSet<String>(super.keySet());
            for (String key : super.keySet()) {
                if (!key.contains(":")) continue;
                this.keySet.remove(key);
            }
        }
        return this.keySet;
    }

    @Override
    public Set<String> inheritedKeySet() {
        String innerResource = FrameworkComponentImpl.getInnerPath((Resource)this.resource);
        HashSet<String> keySet = new HashSet<String>(this.keySet());
        keySet.addAll(this.cqComp.keySet());
        for (String key : this.cqComp.keySet()) {
            if (!key.contains(":")) continue;
            keySet.remove(key);
        }
        String fwPath = this.resource.getPath().split("jcr:content/" + innerResource)[0];
        Resource fwResource = this.resource.getResourceResolver().getResource(fwPath);
        if (fwResource != null) {
            Resource parentComp = null;
            for (Resource parentFw = fwResource.getParent(); parentFw != null && (parentComp = this.resource.getResourceResolver().getResource(parentFw.getPath() + "/" + "jcr:content" + "/" + innerResource)) == null; parentFw = parentFw.getParent()) {
            }
            if (parentComp != null) {
                keySet.addAll(new FrameworkComponentImpl(parentComp, this.storeTypes, this.componentService).inheritedKeySet());
            }
        }
        return keySet;
    }

    @Override
    public ValueMap getAnalyticsProperties() {
        return this.cqComp;
    }

}