SitecatalystPageJsonPropWriter.java 5.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl.util;

import com.day.cq.commons.jcr.JcrUtil;
import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SitecatalystPageJsonPropWriter {
    private static final Logger LOG = LoggerFactory.getLogger(SitecatalystPageJsonPropWriter.class);
    private static final String CQ_PAGE_PROP = "cq:path";
    private String targetPageChildName;
    private Session session;

    public SitecatalystPageJsonPropWriter(Session session, String targetPageChildName) {
        this.targetPageChildName = targetPageChildName;
        this.session = session;
    }

    public void writeData(JSONObject scData) {
        try {
            this.session.refresh(true);
        }
        catch (RepositoryException e) {
            LOG.error("Could not refresh session.", (Throwable)e);
        }
        String pagePath = scData.optString("cq:path");
        Iterator keys = scData.keys();
        while (keys.hasNext()) {
            String key = (String)keys.next();
            String keyStrValue = scData.optString(key);
            if (StringUtils.isNotBlank((String)pagePath) && StringUtils.isNotBlank((String)keyStrValue) && this.isKeyAllowed(key)) {
                String propName = key;
                if (!this.isValidKey(key)) {
                    if (!key.startsWith("f:")) continue;
                    LOG.debug("Calculated metric key [{}] found, creating a valid property name.");
                    propName = JcrUtil.createValidName((String)key);
                }
                this.saveProperty(pagePath, this.targetPageChildName, propName, keyStrValue);
            }
            if (scData.optJSONObject(key) != null) {
                this.writeData(scData.optJSONObject(key));
                continue;
            }
            if (scData.optJSONArray(key) == null) continue;
            this.writeData(scData.optJSONArray(key));
        }
    }

    public void writeData(JSONArray array) {
        for (int idx = 0; idx < array.length(); ++idx) {
            if (array.optJSONObject(idx) != null) {
                this.writeData(array.optJSONObject(idx));
                continue;
            }
            if (array.optJSONArray(idx) == null) continue;
            this.writeData(array.optJSONArray(idx));
        }
    }

    private void saveProperty(String pagePath, String pageChild, String propName, String propValue) {
        try {
            if (!this.session.nodeExists(pagePath)) {
                return;
            }
            Node targetPage = this.session.getNode(pagePath);
            if (targetPage != null) {
                Node pageContentNode;
                Node targetNode = null;
                Node node = pageContentNode = targetPage.hasNode("jcr:content") ? targetPage.getNode("jcr:content") : null;
                if (pageContentNode == null) {
                    return;
                }
                Node analyticsDataRootNode = null;
                if (!pageContentNode.hasNode("cq:meta")) {
                    pageContentNode.addMixin("cq:metaMixin");
                    analyticsDataRootNode = pageContentNode.addNode("cq:meta", "cq:meta");
                } else {
                    analyticsDataRootNode = pageContentNode.getNode("cq:meta");
                }
                targetNode = analyticsDataRootNode.hasNode(pageChild) ? analyticsDataRootNode.getNode(pageChild) : analyticsDataRootNode.addNode(pageChild, "cq:meta");
                double dblPropValue = Double.parseDouble(propValue);
                long longPropValue = (long)dblPropValue;
                if (dblPropValue > (double)longPropValue) {
                    targetNode.setProperty("analytics_" + propName, dblPropValue);
                } else {
                    targetNode.setProperty("analytics_" + propName, longPropValue);
                }
                if (this.session.hasPendingChanges()) {
                    this.session.save();
                }
            } else {
                LOG.warn("Non-existent page path:" + pagePath);
            }
        }
        catch (Exception e) {
            LOG.error("Could not save SC property " + propName + " for page " + pagePath, (Throwable)e);
        }
    }

    private boolean isKeyAllowed(String key) {
        return !key.equalsIgnoreCase("name") && !key.equalsIgnoreCase("url");
    }

    private boolean isValidKey(String key) {
        return !key.matches("[a-z]+\\:.*");
    }
}