AnalyticsComponent.java 5.63 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.components.ComponentManager
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.impl;

import com.day.cq.analytics.impl.AnalyticsComponentQueryCache;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentManager;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Session;
import javax.jcr.Value;
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.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AnalyticsComponent {
    public static final String PN_MAPPING_COMPONENT = "cq:mappingComponent";
    public static final String PN_DEFAULT_STORE = "cq:defaultStore";
    public static final String DEFAULT_STORE = "eventdata";
    public static final String PN_DEFAULT_EVENT_STORE = "cq:defaultEventStore";
    public static final String DEFAULT_EVENT_STORE = "eventdata.events";
    public static final String PN_TRACKVARS = "cq:trackvars";
    public static final String PN_TRACKEVENTS = "cq:trackevents";
    private final Logger logger;
    private String path;
    private String mappingComponent;
    private String defaultStore;
    private String defaultEventStore;
    private String[] trackedVars;
    private String[] trackedEvents;
    private ValueMap analyticsProps;

    public AnalyticsComponent(Resource component, AnalyticsComponentQueryCache analyticsComponentQueryCache) {
        Component comp;
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.mappingComponent = "";
        if (component == null) {
            throw new IllegalArgumentException("component may not be null");
        }
        Node node = (Node)component.adaptTo(Node.class);
        if (node == null) {
            throw new IllegalArgumentException("Resource at " + component.getPath() + " can't be adapted to a " + Node.class.getName());
        }
        this.analyticsProps = (ValueMap)component.adaptTo(ValueMap.class);
        this.path = ResourceUtil.getParent((String)component.getPath());
        ComponentManager componentManager = (ComponentManager)component.getResourceResolver().adaptTo(ComponentManager.class);
        if (componentManager != null && (comp = componentManager.getComponent(this.path)) != null) {
            this.mappingComponent = (String)comp.getProperties().get("cq:mappingComponent", (Object)"");
        }
        this.defaultStore = (String)this.analyticsProps.get("cq:defaultStore", (Object)"eventdata");
        this.defaultEventStore = (String)this.analyticsProps.get("cq:defaultEventStore", (Object)"eventdata.events");
        this.trackedVars = this.getListProperty(node, "cq:trackvars", analyticsComponentQueryCache);
        this.trackedEvents = this.getListProperty(node, "cq:trackevents", analyticsComponentQueryCache);
        this.analyticsProps = new ValueMapDecorator(new HashMap(this.analyticsProps));
    }

    public String getPath() {
        return this.path;
    }

    public String getDefaultStore() {
        return this.defaultStore;
    }

    public String getDefaultEventStore() {
        return this.defaultEventStore;
    }

    public String[] getTrackedVars() {
        return this.trackedVars;
    }

    public String[] getTrackedEvents() {
        return this.trackedEvents;
    }

    public ValueMap getAnalyticsProps() {
        return this.analyticsProps;
    }

    public String getMappingComponent() {
        return this.mappingComponent;
    }

    protected String[] getListProperty(Node node, String propertyName, AnalyticsComponentQueryCache analyticsComponentQueryCache) {
        String[] list = new String[]{};
        try {
            if (!node.hasProperty(propertyName)) {
                this.logger.warn(node.getPath() + "/" + propertyName + " does not exist");
            } else {
                Property property = node.getProperty(propertyName);
                while (property.getType() == 8) {
                    property = property.getProperty();
                }
                if (property.isMultiple()) {
                    Value[] values = property.getValues();
                    list = new String[values.length];
                    for (int i = 0; i < values.length; ++i) {
                        list[i] = values[i].getString();
                    }
                } else {
                    String value = property.getValue().getString().trim();
                    if (value != null && value.startsWith("SELECT ")) {
                        Session session = node.getSession();
                        list = analyticsComponentQueryCache.getQueryListProperty(value, session);
                    } else if (!StringUtils.isEmpty((String)value)) {
                        list = value.split("\\s*[,\\s]\\s*");
                    }
                }
            }
        }
        catch (Exception ignore) {
            this.logger.error(ignore.getMessage(), (Throwable)ignore);
        }
        return list;
    }
}