DefaultProgressiveOrderingGenerator.java
10.5 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
218
219
220
221
222
223
224
225
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.utils;
import com.adobe.aemds.guide.common.FormsGuideException;
import com.adobe.aemds.guide.utils.CustomJSONWriter;
import com.adobe.aemds.guide.utils.GuideProgressiveUtils;
import com.adobe.aemds.guide.utils.NodeStructureUtils;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class DefaultProgressiveOrderingGenerator {
private static Logger logger = LoggerFactory.getLogger(DefaultProgressiveOrderingGenerator.class);
private ResourceResolver resourceResolver;
private CustomJSONWriter jsonWriter = null;
int sectionId = 0;
int fieldCounter = 0;
public DefaultProgressiveOrderingGenerator(ResourceResolver resResolver) {
this.resourceResolver = resResolver;
}
public String generateProgressiveOrdering(JSONObject guideContainerJson) throws FormsGuideException {
try {
JSONObject rootPanel = guideContainerJson.getJSONObject("rootPanel");
StringWriter stringWriter = new StringWriter();
this.jsonWriter = new CustomJSONWriter(stringWriter);
this.jsonWriter.object();
String fragPrefixString = null;
String fragRef = this.getJsonValue(rootPanel, "fragRef");
if (fragRef != null) {
String path = this.getJsonValue(rootPanel, "jcr:path");
Resource panel = this.resourceResolver.getResource(path);
fragPrefixString = NodeStructureUtils.getFragPrefixString(panel, null);
}
this.generateSectionForPanel(rootPanel, fragPrefixString);
this.jsonWriter.key("completion").object();
this.jsonWriter.key("completionTitle").value("Submit the form");
this.jsonWriter.key("completionAfterMessage").value("Thank you for submitting the form.");
this.jsonWriter.key("completionBeforeMessage").value("Do you want to submit the form?");
this.jsonWriter.key("completionButtonText").value("Submit");
this.jsonWriter.key("guideNodeClass").value("guideProgressiveCompletionSection");
this.jsonWriter.key("completionScript").value("window.guideBridge.submit(options)");
this.jsonWriter.key("completionSuccessScript").value("");
this.jsonWriter.key("completionFailureScript").value("");
this.jsonWriter.endObject();
this.jsonWriter.endObject();
return stringWriter.toString();
}
catch (Exception e) {
logger.error(e.getMessage(), (Throwable)e);
throw new FormsGuideException(e);
}
}
private void generateSectionForPanel(JSONObject panel, String fragPrefixString) throws JSONException {
int maxOccur = Integer.valueOf(this.getOrElse(panel, "maxOccur", "1"));
if (maxOccur < 0 || maxOccur > 1) {
this.writeProgressiveSectionJson(panel, true, this.getFields(panel, false, fragPrefixString), fragPrefixString);
return;
}
List<FieldFragRefMapping> directFields = this.getFields(panel, true, fragPrefixString);
if (directFields.size() > 0) {
this.writeProgressiveSectionJson(panel, false, directFields, fragPrefixString);
}
JSONObject panelItems = null;
if (panel.has("items") && panel.get("items") instanceof JSONObject) {
panelItems = panel.getJSONObject("items");
Iterator itemIterator = panelItems.keys();
while (itemIterator.hasNext()) {
String guideNodeClass;
String itemKey = (String)itemIterator.next();
Object itemValue = panelItems.get(itemKey);
if (!(itemValue instanceof JSONObject) || !"guidePanel".equals(guideNodeClass = this.getJsonValue((JSONObject)itemValue, "guideNodeClass"))) continue;
String fragRef = this.getJsonValue((JSONObject)itemValue, "fragRef");
String fragPrefixStringForChildPanel = null;
if (fragRef != null) {
String path = this.getJsonValue((JSONObject)itemValue, "jcr:path");
Resource res = this.resourceResolver.getResource(path);
fragPrefixStringForChildPanel = NodeStructureUtils.getFragPrefixString(res, fragPrefixString);
}
if (fragPrefixStringForChildPanel == null) {
fragPrefixStringForChildPanel = fragPrefixString;
}
this.generateSectionForPanel((JSONObject)itemValue, fragPrefixStringForChildPanel);
}
}
}
private void writeProgressiveSectionJson(JSONObject panel, boolean isRepeatable, List<FieldFragRefMapping> pdcFields, String fragPrefixString) throws JSONException {
String id = "section_" + this.sectionId++;
this.jsonWriter.key(id).object();
this.jsonWriter.key("guideNodeClass").value("guideProgressiveSection");
this.jsonWriter.key("jcr:title").value(this.getSectionTitle(panel));
this.jsonWriter.key("name").value(this.getSectionName(panel, id));
if (isRepeatable) {
String panelPath = this.getJsonValue(panel, "jcr:path");
Resource pdcPanelResource = this.resourceResolver.getResource(panelPath);
String repeatablePanelId = NodeStructureUtils.getGuideNodeHtmlId(pdcPanelResource);
this.jsonWriter.key("repeatablePanelPath").value(panelPath);
this.jsonWriter.key("repeatablePanelId").value(repeatablePanelId);
}
this.jsonWriter.key("items").object();
for (FieldFragRefMapping pdcField : pdcFields) {
int index = pdcField.getPath().lastIndexOf(47);
String name = pdcField.getPath().substring(index + 1) + "_" + this.fieldCounter;
++this.fieldCounter;
this.jsonWriter.key(name).object();
this.jsonWriter.key("jcr:primaryType").value("nt:unstructured");
Resource pdcFieldResource = this.resourceResolver.getResource(pdcField.getPath());
String pdcFieldId = NodeStructureUtils.getGuideNodeHtmlId(pdcFieldResource, pdcField.getFragPrefix());
this.jsonWriter.key("id").value(pdcFieldId);
this.jsonWriter.key("path").value(pdcField.getPath());
if (pdcField.getFragPrefix() != null) {
this.jsonWriter.key("prefixId").value(pdcField.getFragPrefix());
}
this.jsonWriter.endObject();
}
this.jsonWriter.endObject();
this.jsonWriter.endObject();
}
private String getSectionTitle(JSONObject panel) throws JSONException {
String title = this.getJsonValue(panel, "jcr:title");
if (StringUtils.isEmpty((String)title)) {
title = this.getJsonValue(panel, "name");
}
return title;
}
private String getSectionName(JSONObject panel, String id) throws JSONException {
String name = this.getJsonValue(panel, "name");
if (StringUtils.isEmpty((String)name)) {
name = id;
}
return name;
}
private List<FieldFragRefMapping> getFields(JSONObject panel, boolean onlyDirectFields, String fragPrefixString) throws JSONException {
ArrayList<FieldFragRefMapping> fields = new ArrayList<FieldFragRefMapping>();
JSONObject panelItems = null;
if (panel.has("items") && panel.get("items") instanceof JSONObject) {
panelItems = panel.getJSONObject("items");
Iterator itemIterator = panelItems.keys();
while (itemIterator.hasNext()) {
String itemKey = (String)itemIterator.next();
Object itemValue = panelItems.get(itemKey);
if (!(itemValue instanceof JSONObject)) continue;
String guideNodeClass = this.getJsonValue((JSONObject)itemValue, "guideNodeClass");
if (GuideProgressiveUtils.isGuideProgressiveFieldModel(guideNodeClass)) {
fields.add(new FieldFragRefMapping(this.getJsonValue((JSONObject)itemValue, "jcr:path"), fragPrefixString));
continue;
}
if (!"guidePanel".equals(guideNodeClass) || onlyDirectFields) continue;
String fragref = this.getJsonValue((JSONObject)itemValue, "fragRef");
String fragPrefixStringForChildPanel = null;
if (fragref != null) {
String path = this.getJsonValue((JSONObject)itemValue, "jcr:path");
Resource res = this.resourceResolver.getResource(path);
fragPrefixStringForChildPanel = NodeStructureUtils.getFragPrefixString(res, fragPrefixString);
} else {
fragPrefixStringForChildPanel = fragPrefixString;
}
fields.addAll(this.getFields((JSONObject)itemValue, onlyDirectFields, fragPrefixStringForChildPanel));
}
}
return fields;
}
private String getJsonValue(JSONObject jsonObject, String keyName) throws JSONException {
if (jsonObject.has(keyName)) {
return jsonObject.getString(keyName);
}
return null;
}
private String getOrElse(JSONObject jsonObject, String keyName, String fallbackValue) throws JSONException {
String value = this.getJsonValue(jsonObject, keyName);
return value != null ? value : fallbackValue;
}
public class FieldFragRefMapping {
private String path;
private String fragPrefix;
public FieldFragRefMapping(String field, String fragRef) {
this.path = field;
this.fragPrefix = fragRef;
}
public String getPath() {
return this.path;
}
public String getFragPrefix() {
return this.fragPrefix;
}
}
}