FDMResourcePropertyTransformer.java
6.38 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.transformer.api.ResourcePropertyTransformer
* com.adobe.aemds.guide.utils.GuideContainerThreadLocal
* com.adobe.aemds.guide.utils.GuideFragmentHolder
* com.adobe.aemds.guide.utils.GuideUtils
* org.apache.commons.lang3.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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.addon.fdm.prefill;
import com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer;
import com.adobe.aemds.guide.utils.GuideContainerThreadLocal;
import com.adobe.aemds.guide.utils.GuideFragmentHolder;
import com.adobe.aemds.guide.utils.GuideUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FDMResourcePropertyTransformer
implements ResourcePropertyTransformer {
private JSONObject result = null;
private Map<String, JSONArray> map = new HashMap<String, JSONArray>();
private final Logger logger;
public FDMResourcePropertyTransformer() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
private String getFormDataModelName(String bindRef) {
if (StringUtils.isEmpty((CharSequence)bindRef)) {
return null;
}
return StringUtils.substringAfterLast((String)bindRef, (String)"/");
}
private List<String> getAllPrefixes(String bindRef) {
ArrayList<String> prefixes = new ArrayList<String>();
if (!StringUtils.isEmpty((CharSequence)bindRef)) {
prefixes.add(bindRef);
String subStringBeforeSlash = StringUtils.substringBeforeLast((String)bindRef, (String)"/");
while (StringUtils.isNotEmpty((CharSequence)subStringBeforeSlash)) {
prefixes.add(subStringBeforeSlash);
subStringBeforeSlash = StringUtils.substringBeforeLast((String)subStringBeforeSlash, (String)"/");
}
}
return prefixes;
}
public JSONObject getResult() {
return this.result;
}
public JSONObject transform(Resource resource, String parentSOM) throws JSONException {
JSONObject obj = new JSONObject();
ValueMap props = (ValueMap)resource.adaptTo(ValueMap.class);
if ("layout".equals(resource.getName())) {
return null;
}
String nodeClass = (String)props.get((Object)"guideNodeClass");
if (StringUtils.isEmpty((CharSequence)nodeClass)) {
return obj;
}
String bindRef = (String)props.get((Object)"bindRef");
if (StringUtils.isEmpty((CharSequence)bindRef)) {
return obj;
}
obj.put("bindRef", (Object)bindRef);
GuideFragmentHolder guideFragmentHolder = null;
try {
guideFragmentHolder = GuideContainerThreadLocal.getGuideFragmentHolder();
}
catch (Exception e) {
this.logger.error("Unable to get fragment holder", (Throwable)e);
}
if (guideFragmentHolder != null) {
String bindRefPrefixForFragment = guideFragmentHolder.getBindRefPrefixForFragment();
String fragmentRoot = guideFragmentHolder.getFragmentModelRoot();
bindRef = GuideUtils.manipulateBindRefForFragments((String)bindRefPrefixForFragment, (String)bindRef, (String)fragmentRoot);
}
if (GuideUtils.isGuideFieldModel((String)nodeClass) || GuideUtils.isGuidePanelModel((String)nodeClass)) {
String stringBeforeSlash = StringUtils.substringBeforeLast((String)bindRef, (String)"/");
if (StringUtils.isNotEmpty((CharSequence)stringBeforeSlash)) {
int i;
String parentModelNameKey = stringBeforeSlash;
JSONArray arr = this.map.get(parentModelNameKey);
if (arr == null) {
JSONArray modelArr = null;
List<String> models = this.getAllPrefixes(stringBeforeSlash);
for (i = models.size() - 1; i >= 0; --i) {
String modelKey = models.get(i);
if (!this.map.containsKey(modelKey)) {
this.addPanelToResult(modelKey, modelArr);
}
modelArr = this.map.get(modelKey);
}
arr = modelArr;
}
if (GuideUtils.isGuidePanelModel((String)nodeClass)) {
this.addPanelToResult(bindRef, arr);
} else {
String name = this.getFormDataModelName(bindRef);
boolean isKeyPresent = false;
for (i = 0; i < arr.length(); ++i) {
if (!arr.getString(i).equals(name)) continue;
isKeyPresent = true;
break;
}
if (!isKeyPresent) {
arr.put((Object)name);
}
}
} else {
this.addPanelToResult(bindRef, null);
}
}
return obj;
}
private void addPanelToResult(String bindRef, JSONArray arr) throws JSONException {
if (!this.map.containsKey(bindRef)) {
String dataModelName = this.getFormDataModelName(bindRef);
if (StringUtils.isEmpty((CharSequence)dataModelName)) {
return;
}
JSONObject current = new JSONObject();
JSONArray child = new JSONArray();
current.put(dataModelName, (Object)child);
if (this.result == null) {
this.result = current;
} else if (arr != null) {
arr.put((Object)current);
} else {
child = new JSONArray();
this.result.put(dataModelName, (Object)child);
}
this.map.put(bindRef, child);
}
}
}