ReportDescription.java
8.7 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* 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;
}
}