AbstractFDMPrefillService.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aem.dermis.api.IFormDataModelManager
* com.adobe.aem.dermis.api.bridge.DataContext
* com.adobe.aem.dermis.api.bridge.IDermisBridge
* com.adobe.aem.dermis.api.bridge.OperationOptions
* com.adobe.aem.dermis.api.bridge.Query
* com.adobe.aem.dermis.slingmodel.Entity
* com.adobe.aem.dermis.slingmodel.FormDataModel
* com.adobe.aem.dermis.util.DermisUtils
* com.adobe.aem.dermis.util.SlingJSONWriter
* com.adobe.aemds.guide.service.GuideModelTransformer
* com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer
* com.adobe.forms.common.service.DataOptions
* com.adobe.forms.common.service.PrefillData
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* 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.aem.dermis.api.IFormDataModelManager;
import com.adobe.aem.dermis.api.bridge.DataContext;
import com.adobe.aem.dermis.api.bridge.IDermisBridge;
import com.adobe.aem.dermis.api.bridge.OperationOptions;
import com.adobe.aem.dermis.api.bridge.Query;
import com.adobe.aem.dermis.slingmodel.Entity;
import com.adobe.aem.dermis.slingmodel.FormDataModel;
import com.adobe.aem.dermis.util.DermisUtils;
import com.adobe.aem.dermis.util.SlingJSONWriter;
import com.adobe.aemds.guide.addon.fdm.prefill.FDMResourcePropertyTransformer;
import com.adobe.aemds.guide.addon.fdm.prefill.IFDMPrefill;
import com.adobe.aemds.guide.service.GuideModelTransformer;
import com.adobe.aemds.guide.transformer.api.ResourcePropertyTransformer;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.PrefillData;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=0)
public abstract class AbstractFDMPrefillService
implements IFDMPrefill {
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
private IDermisBridge dermisBridge;
@Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
protected IFormDataModelManager formDataModelManager;
@Reference
private GuideModelTransformer guideModelTransformer;
private final Logger logger;
private Map<String, Object> extraParamMap;
public AbstractFDMPrefillService() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
public abstract PrefillData getPrefillData(DataOptions var1);
protected String getFormDataModelDamAssetPath(Resource formResource) {
if (formResource != null) {
ValueMap formResourceValueMap = null;
formResourceValueMap = (ValueMap)formResource.adaptTo(ValueMap.class);
if (formResourceValueMap != null) {
return (String)formResourceValueMap.get("schemaRef", (Object)"");
}
}
return "";
}
protected InputStream getData(DataOptions options) throws Exception {
String dataRef = options.getDataRef();
Resource formResource = options.getFormResource();
this.extraParamMap = options.getExtras();
String formDataModelDamAssetPath = this.getFormDataModelDamAssetPath(formResource);
FormDataModel formDataModel = null;
if (StringUtils.isBlank((CharSequence)formDataModelDamAssetPath)) {
this.logger.warn("No form data model associated with adaptive form");
return null;
}
try {
formDataModel = this.formDataModelManager.getFormDataModel(formDataModelDamAssetPath);
}
catch (Exception ex) {
this.logger.warn("Incorrect form data model path set " + formDataModelDamAssetPath, (Throwable)ex);
return null;
}
ByteArrayInputStream result = null;
if (StringUtils.isEmpty((CharSequence)dataRef) || StringUtils.startsWith((CharSequence)dataRef, (CharSequence)"service")) {
if (StringUtils.isNotBlank((CharSequence)formDataModelDamAssetPath)) {
String selectionPoints = this.getSelectionPoints(formResource);
if (selectionPoints == null) {
this.logger.warn("No selection points configured for the adaptive form: " + formResource.getPath());
return null;
}
OperationOptions operationOptions = new OperationOptions(selectionPoints, this.getEntityPrefillOperationMap(formResource));
Query query = new Query(formDataModelDamAssetPath, this.getDataContext(), operationOptions);
if (this.dermisBridge != null) {
Object data = this.dermisBridge.executeQuery(query);
if (data != null) {
data = this.marshallDataWithAFWrapper(data);
}
if (data instanceof String) {
result = new ByteArrayInputStream(((String)data).getBytes(StandardCharsets.UTF_8));
}
}
} else {
throw new Exception("No form data model associated with adaptive form " + formResource.getPath());
}
}
return result;
}
protected Object marshallDataWithAFWrapper(Object data) {
HashMap<String, Map> jsonData;
if (data instanceof Map && (jsonData = (HashMap<String, Map>)data).get("afData") == null) {
HashMap<K, V> afBoundData = new HashMap<K, V>();
HashMap<K, V> afUnboundData = new HashMap<K, V>();
HashMap<String, Map> boundDataObject = new HashMap<String, Map>();
HashMap<String, HashMap<K, V>> unboundDataObject = new HashMap<String, HashMap<K, V>>();
for (Map.Entry<K, V> thisEntry : jsonData.entrySet()) {
String entityName = (String)thisEntry.getKey();
if (!(jsonData.get(entityName) instanceof List)) continue;
List jsonList = (List)jsonData.get(entityName);
Map arrayData = (Map)jsonList.get(0);
jsonData = new HashMap<String, Map>();
jsonData.put(entityName, arrayData);
}
boundDataObject.put("data", jsonData);
unboundDataObject.put("data", new HashMap<K, V>());
HashMap<String, HashMap<K, V>> afData = new HashMap<String, HashMap<K, V>>();
HashMap<String, HashMap> afDataChildren = new HashMap<String, HashMap>();
afDataChildren.put("afBoundData", boundDataObject);
afDataChildren.put("afUnboundData", unboundDataObject);
afData.put("afData", afDataChildren);
StringWriter writer = new StringWriter();
SlingJSONWriter slingJSONWriter = new SlingJSONWriter((Writer)writer);
slingJSONWriter.write(afData);
data = writer.toString();
}
return data;
}
@Override
public String getSelectionPoints(Resource formResource) throws JSONException {
String selectionPoints = null;
FDMResourcePropertyTransformer transformer = new FDMResourcePropertyTransformer();
this.guideModelTransformer.getAdaptiveFormTreeJSON(formResource, (ResourcePropertyTransformer)transformer);
JSONObject result = transformer.getResult();
if (result != null) {
selectionPoints = result.toString();
}
return selectionPoints;
}
@Override
public Map<String, String> getEntityPrefillOperationMap(Resource formResource) throws Exception {
if (formResource == null) {
throw new Exception("Form resource cannot be null");
}
HashMap<String, String> entityPrefillOperationMap = new HashMap<String, String>();
String formDataModelDamAssetPath = this.getFormDataModelDamAssetPath(formResource);
if (StringUtils.isNotEmpty((CharSequence)formDataModelDamAssetPath)) {
FormDataModel formDataModel = this.formDataModelManager.getFormDataModel(formDataModelDamAssetPath);
List entityList = formDataModel.getDataModels();
for (Entity entity : entityList) {
entityPrefillOperationMap.put(entity.getId(), entity.getDefaultReadOperation());
}
}
return entityPrefillOperationMap;
}
@Override
public DataContext getDataContext() {
Map wrapperMap = DermisUtils.createRequestDataContext(this.extraParamMap);
return new DataContext(wrapperMap);
}
public abstract String getServiceName();
public abstract String getServiceDescription();
protected void bindDermisBridge(IDermisBridge iDermisBridge) {
this.dermisBridge = iDermisBridge;
}
protected void unbindDermisBridge(IDermisBridge iDermisBridge) {
if (this.dermisBridge == iDermisBridge) {
this.dermisBridge = null;
}
}
protected void bindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
this.formDataModelManager = iFormDataModelManager;
}
protected void unbindFormDataModelManager(IFormDataModelManager iFormDataModelManager) {
if (this.formDataModelManager == iFormDataModelManager) {
this.formDataModelManager = null;
}
}
protected void bindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
this.guideModelTransformer = guideModelTransformer;
}
protected void unbindGuideModelTransformer(GuideModelTransformer guideModelTransformer) {
if (this.guideModelTransformer == guideModelTransformer) {
this.guideModelTransformer = null;
}
}
}