LetterDataMerger.java
6.61 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.utils.DocumentDataMerger
* com.adobe.icc.ddg.api.HTML5ModuleResolverService
* org.apache.commons.lang3.StringUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
*/
package com.adobe.fd.adaddon.internal.utils;
import com.adobe.aemds.guide.utils.DocumentDataMerger;
import com.adobe.icc.ddg.api.HTML5ModuleResolverService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.w3c.dom.Document;
public class LetterDataMerger
extends DocumentDataMerger {
private static String ROW_ID_PREFIX = "ROW_ID:";
private final HTML5ModuleResolverService html5ModuleResolverService;
private Map<String, Object> fieldsValueMap;
private Map<String, Object> currentContext;
public LetterDataMerger(JSONObject guideJson, Document mergeReferenceDocument, Map<String, Object> params, HTML5ModuleResolverService html5ModuleResolverService) {
super(guideJson, mergeReferenceDocument, params);
if (guideJson.has("letterRef")) {
try {
params.put("letterRef", guideJson.getString("letterRef"));
}
catch (Exception e) {
this.logger.error("Failed to retrieve letter ref from guide json: " + (Object)guideJson, (Throwable)e);
}
}
this.fieldsValueMap = (Map)params.get("FIELD_VALUE_MAP");
this.currentContext = this.fieldsValueMap;
this.html5ModuleResolverService = html5ModuleResolverService;
}
protected String getComputedBindProperty(JSONObject jsonObject) throws JSONException {
String bindProp;
if (jsonObject.has("bindRef") && StringUtils.isNotBlank((CharSequence)(bindProp = jsonObject.getString("bindRef"))) && bindProp.startsWith("FA_ID:")) {
return bindProp;
}
if (jsonObject.has("sling:resourceType") && "fd/af/components/tableRow".equals(jsonObject.getString("sling:resourceType")) && StringUtils.isNotBlank((CharSequence)(bindProp = this.handleDynamicTableCase(jsonObject)))) {
return bindProp;
}
return super.getComputedBindProperty(jsonObject);
}
private String handleDynamicTableCase(JSONObject jsonObject) throws JSONException {
ArrayList<String> faIds = new ArrayList<String>();
ArrayList<Object> fieldValues = new ArrayList<Object>();
if (jsonObject.has("maxOccur") && "-1".equals(jsonObject.getString("maxOccur")) && jsonObject.has("items")) {
JSONObject itemObject = jsonObject.getJSONObject("items");
Iterator keys = itemObject.keys();
while (keys.hasNext()) {
String bindProp;
JSONObject childJSONObject;
String key = (String)keys.next();
Object obj = itemObject.get(key);
if (!(obj instanceof JSONObject) || !(childJSONObject = (JSONObject)obj).has("bindRef") || !StringUtils.isNotBlank((CharSequence)(bindProp = childJSONObject.getString("bindRef"))) || !bindProp.startsWith("FA_ID:")) continue;
String faId = StringUtils.substringAfter((String)bindProp, (String)"FA_ID:");
faIds.add(faId);
fieldValues.add(this.fieldsValueMap.get(faId));
}
}
if (faIds.size() > 0) {
String dynamicBindProp = ROW_ID_PREFIX + StringUtils.join(faIds, (String)":");
if (this.fieldsValueMap.get(dynamicBindProp) == null) {
List flattenedFieldValueMap = this.html5ModuleResolverService.prepareTableData(fieldValues);
ArrayList contextValueMap = new ArrayList();
for (List row : flattenedFieldValueMap) {
Iterator faIdIter = faIds.iterator();
HashMap<String, String> cellMap = new HashMap<String, String>();
for (String col : row) {
String faId = (String)faIdIter.next();
cellMap.put(faId, col);
}
contextValueMap.add(cellMap);
}
this.fieldsValueMap.put(dynamicBindProp, contextValueMap);
}
return dynamicBindProp;
}
return null;
}
protected String matchPrimitive(String relativePath, boolean isRepeatChild, boolean isRichText) throws Exception {
return this.matchPrimitive(relativePath, isRepeatChild);
}
protected String matchPrimitive(String relativePath, boolean isRepeat) throws Exception {
if (StringUtils.isNotBlank((CharSequence)relativePath) && relativePath.startsWith("FA_ID:") && this.currentContext != null) {
String faId = StringUtils.substringAfter((String)relativePath, (String)"FA_ID:");
Object matchedValue = this.currentContext.get(faId);
return matchedValue != null ? String.valueOf(this.currentContext.get(faId)) : "";
}
return super.matchPrimitive(relativePath, isRepeat);
}
protected Object matchComposite(String relativePath, String fullPath) throws Exception {
if (this.currentContext.get(relativePath) != null) {
return this.currentContext.get(relativePath);
}
return super.matchComposite(relativePath, fullPath);
}
protected void setCurrentContext(Object ctx) {
if (ctx instanceof Map) {
this.currentContext = (Map)ctx;
return;
}
super.setCurrentContext(ctx);
}
protected int getMatchCount(Object match) {
if (match != null && match instanceof List) {
List nodeList = (List)match;
return nodeList.size();
}
return super.getMatchCount(match);
}
protected Object getMatchItem(Object match, int ith) {
if (match != null && match instanceof List) {
List nodeList = (List)match;
return nodeList.get(ith);
}
return super.getMatchItem(match, ith);
}
protected void resetContext() {
this.currentContext = this.fieldsValueMap;
super.resetContext();
}
protected String getRelativePath(String path) {
if (StringUtils.isNotBlank((CharSequence)path) && path.startsWith(ROW_ID_PREFIX)) {
return path;
}
return super.getRelativePath(path);
}
protected Object getCurrentContext() {
return this.currentContext;
}
}