SitecatalystPageJsonPropWriter.java
5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* 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]+\\:.*");
}
}