ReportDescription.java 8.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.settings.SlingSettingsService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl.importer;

import com.day.cq.analytics.sitecatalyst.SitecatalystUtil;
import com.day.cq.analytics.sitecatalyst.impl.util.DateAdjuster;
import com.day.cq.analytics.sitecatalyst.util.SitecatalystJsonItemWriter;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.CharArrayWriter;
import java.io.Writer;
import java.text.ParseException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReportDescription {
    private static final int DEFAULT_TOP = 100;
    private static final int MAX_TOP_VALUE = 50000;
    private static final String PROP_TOP = "top";
    private static final String PROP_STARTING_WITH = "startingWith";
    private final Logger logger;
    private SlingSettingsService settingsService;
    private JSONObject reportDescription;

    public ReportDescription(Resource targetResource, Configuration configuration, SlingSettingsService settingsService) throws JSONException, ParseException, RepositoryException {
        this.logger = LoggerFactory.getLogger(this.getClass());
        this.settingsService = settingsService;
        this.parse(targetResource);
        this.adjustDates();
        this.applyConfiguration(targetResource, configuration);
    }

    public void addSegment(String segmentIdentifier) throws JSONException {
        this.reportDescription.append("segments", (Object)new JSONObject().put("id", (Object)segmentIdentifier));
    }

    public void setFirstElementTop(int top) throws IllegalArgumentException {
        if (top > 50000) {
            throw new IllegalArgumentException("Largest value can be 50000");
        }
        this.setFirstElementValue("top", top, false);
    }

    public int getFirstElementTop() {
        JSONObject firstElement = this.getFirstElement();
        if (firstElement != null) {
            return firstElement.optInt("top", 100);
        }
        return 100;
    }

    public void setFirstElementStartWith(int index) {
        this.setFirstElementValue("startingWith", index, true);
    }

    public JSONObject getJSONObject() {
        return this.reportDescription;
    }

    public boolean isOvertimeReport() {
        if (this.reportDescription != null) {
            return this.reportDescription.optJSONArray("elements").length() == 0 && this.reportDescription.opt("dateGranularity") != null;
        }
        return false;
    }

    public boolean isRankedReport() {
        if (this.reportDescription != null) {
            return this.reportDescription.optJSONArray("elements").length() >= 1 && this.reportDescription.opt("dateGranularity") == null;
        }
        return false;
    }

    public boolean isTrendedReport() {
        if (this.reportDescription != null) {
            return this.reportDescription.optJSONArray("elements").length() >= 1 && this.reportDescription.opt("dateGranularity") != null;
        }
        return false;
    }

    public boolean isSummaryReport() {
        if (this.reportDescription != null) {
            boolean hasReportSuiteElement;
            block4 : {
                hasReportSuiteElement = false;
                try {
                    JSONArray elements = this.reportDescription.optJSONArray("elements");
                    if (elements == null) break block4;
                    for (int i = 0; i < elements.length(); ++i) {
                        JSONObject element = elements.getJSONObject(i);
                        if (!"reportsuite".equalsIgnoreCase(element.optString("id"))) continue;
                        hasReportSuiteElement = true;
                        break;
                    }
                }
                catch (JSONException e) {
                    this.logger.error("Error while determining summary report type.", (Throwable)e);
                }
            }
            return this.reportDescription.opt("reportSuiteID") == null && hasReportSuiteElement;
        }
        return false;
    }

    public boolean isPathingReport() {
        if (this.reportDescription != null) {
            boolean hasPattern = false;
            boolean hasCheckpoints = false;
            try {
                JSONArray elements = this.reportDescription.getJSONArray("elements");
                if (elements != null) {
                    for (int i = 0; i < elements.length(); ++i) {
                        JSONObject element = elements.getJSONObject(i);
                        if (element.has("pattern")) {
                            hasPattern = true;
                        }
                        if (!element.has("checkpoints")) continue;
                        hasCheckpoints = true;
                    }
                }
            }
            catch (JSONException e) {
                this.logger.error("Error while determining summary report type.", (Throwable)e);
            }
            return hasPattern || hasCheckpoints;
        }
        return false;
    }

    private void parse(Resource resource) throws JSONException, RepositoryException {
        Resource description = resource.getChild("reportDescription");
        if (description == null) {
            throw new JSONException("Unable to parse non-existing reportDescription resource.");
        }
        Node node = (Node)description.adaptTo(Node.class);
        if (node == null) {
            throw new RepositoryException("Unable to adapt description to Node.");
        }
        CharArrayWriter charArray = new CharArrayWriter();
        SitecatalystJsonItemWriter itemWriter = new SitecatalystJsonItemWriter();
        itemWriter.dump(node, (Writer)charArray, -1);
        this.reportDescription = new JSONObject(charArray.toString());
    }

    private JSONObject getFirstElement() {
        JSONArray elementsConfig;
        if (this.reportDescription != null && this.reportDescription.optJSONArray("elements") != null && (elementsConfig = this.reportDescription.optJSONArray("elements")) != null && elementsConfig.length() > 0) {
            return elementsConfig.optJSONObject(0);
        }
        return null;
    }

    private void setFirstElementValue(String key, Object value, boolean overwrite) {
        try {
            JSONObject firstElement = this.getFirstElement();
            if (firstElement != null) {
                if (overwrite) {
                    firstElement.put(key, value);
                } else if (!firstElement.has(key) || firstElement.opt(key) == null) {
                    firstElement.put(key, value);
                }
            }
        }
        catch (JSONException e) {
            this.logger.error("Unable to set value on first element.", (Throwable)e);
        }
    }

    private void adjustDates() throws JSONException, ParseException {
        this.reportDescription = DateAdjuster.adjustDates(this.reportDescription);
    }

    private void applyConfiguration(Resource target, Configuration configuration) throws JSONException {
        String segment;
        String reportSuiteID = this.getReportSuiteId(target, configuration);
        if (reportSuiteID == null) {
            throw new IllegalArgumentException("No reportSuiteID specified in the configuration.");
        }
        if (!this.isSummaryReport()) {
            this.reportDescription.put("reportSuiteID", (Object)reportSuiteID);
        }
        if (!StringUtils.isBlank((String)(segment = (String)configuration.getInherited("segment", (Object)null)))) {
            this.addSegment(segment);
        }
    }

    private String getReportSuiteId(Resource target, Configuration configuration) {
        String reportSuiteID = null;
        boolean preferPublishReports = (Boolean)target.getValueMap().get("preferPublishReports", (Object)false);
        reportSuiteID = preferPublishReports ? SitecatalystUtil.getPublishPreferredReportSuite(configuration) : SitecatalystUtil.getReportSuites(this.settingsService, configuration);
        return reportSuiteID;
    }
}