-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
DDMFormValuesJSONSerializer.java
227 lines (177 loc) · 6.66 KB
/
DDMFormValuesJSONSerializer.java
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
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.dynamic.data.mapping.internal.io;
import com.liferay.dynamic.data.mapping.form.field.type.DDMFormFieldValueJSONSerializer;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesSerializer;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesSerializerSerializeRequest;
import com.liferay.dynamic.data.mapping.io.DDMFormValuesSerializerSerializeResponse;
import com.liferay.dynamic.data.mapping.model.DDMForm;
import com.liferay.dynamic.data.mapping.model.DDMFormField;
import com.liferay.dynamic.data.mapping.model.Value;
import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
import com.liferay.dynamic.data.mapping.storage.DDMFormValues;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMap;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMapFactory;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactory;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.LocaleUtil;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
/**
* @author Marcellus Tavares
*/
@Component(
immediate = true, property = "ddm.form.values.serializer.type=json",
service = DDMFormValuesSerializer.class
)
public class DDMFormValuesJSONSerializer implements DDMFormValuesSerializer {
@Override
public DDMFormValuesSerializerSerializeResponse serialize(
DDMFormValuesSerializerSerializeRequest
ddmFormValuesSerializerSerializeRequest) {
DDMFormValues ddmFormValues =
ddmFormValuesSerializerSerializeRequest.getDDMFormValues();
JSONObject jsonObject = _jsonFactory.createJSONObject();
addAvailableLanguageIds(
jsonObject, ddmFormValues.getAvailableLocales());
addDefaultLanguageId(jsonObject, ddmFormValues.getDefaultLocale());
DDMForm ddmForm = ddmFormValues.getDDMForm();
_addFieldValues(
jsonObject, ddmForm.getDDMFormFieldsMap(true),
ddmFormValues.getDDMFormFieldValues());
DDMFormValuesSerializerSerializeResponse.Builder builder =
DDMFormValuesSerializerSerializeResponse.Builder.newBuilder(
jsonObject.toString());
return builder.build();
}
@Activate
protected void activate(BundleContext bundleContext) {
_serviceTrackerMap = ServiceTrackerMapFactory.openSingleValueMap(
bundleContext, DDMFormFieldValueJSONSerializer.class,
"ddm.form.field.type.name");
}
protected void addAvailableLanguageIds(
JSONObject jsonObject, Set<Locale> availableLocales) {
JSONArray jsonArray = _jsonFactory.createJSONArray();
for (Locale availableLocale : availableLocales) {
jsonArray.put(LocaleUtil.toLanguageId(availableLocale));
}
jsonObject.put("availableLanguageIds", jsonArray);
}
protected void addDefaultLanguageId(
JSONObject jsonObject, Locale defaultLocale) {
jsonObject.put(
"defaultLanguageId", LocaleUtil.toLanguageId(defaultLocale));
}
@Deactivate
protected void deactivate() {
_serviceTrackerMap.close();
}
@Reference(unbind = "-")
protected void setJSONFactory(JSONFactory jsonFactory) {
_jsonFactory = jsonFactory;
}
protected JSONObject toJSONObject(
Map<String, DDMFormField> ddmFormFieldsMap,
DDMFormFieldValue ddmFormFieldValue) {
JSONObject jsonObject = _jsonFactory.createJSONObject();
jsonObject.put(
"fieldReference", ddmFormFieldValue.getFieldReference()
).put(
"instanceId", ddmFormFieldValue.getInstanceId()
).put(
"name", ddmFormFieldValue.getName()
);
_addNestedFieldValues(
jsonObject, ddmFormFieldsMap,
ddmFormFieldValue.getNestedDDMFormFieldValues());
_addValue(
jsonObject, ddmFormFieldsMap.get(ddmFormFieldValue.getName()),
ddmFormFieldValue);
return jsonObject;
}
protected JSONObject toJSONObject(Value value) {
JSONObject jsonObject = _jsonFactory.createJSONObject();
for (Locale availableLocale : value.getAvailableLocales()) {
jsonObject.put(
LocaleUtil.toLanguageId(availableLocale),
value.getString(availableLocale));
}
return jsonObject;
}
private void _addFieldValues(
JSONObject jsonObject, Map<String, DDMFormField> ddmFormFieldsMap,
List<DDMFormFieldValue> ddmFormFieldValues) {
jsonObject.put(
"fieldValues", _toJSONArray(ddmFormFieldsMap, ddmFormFieldValues));
}
private void _addNestedFieldValues(
JSONObject jsonObject, Map<String, DDMFormField> ddmFormFieldsMap,
List<DDMFormFieldValue> nestedDDMFormFieldValues) {
if (nestedDDMFormFieldValues.isEmpty()) {
return;
}
jsonObject.put(
"nestedFieldValues",
_toJSONArray(ddmFormFieldsMap, nestedDDMFormFieldValues));
}
private void _addValue(
JSONObject jsonObject, DDMFormField ddmFormField,
DDMFormFieldValue ddmFormFieldValue) {
Value value = ddmFormFieldValue.getValue();
if (value == null) {
return;
}
DDMFormFieldValueJSONSerializer ddmFormFieldValueJSONSerializer =
_getDDMFormFieldValueJSONSerializer(ddmFormField);
if (ddmFormFieldValueJSONSerializer != null) {
jsonObject.put(
"value",
ddmFormFieldValueJSONSerializer.serialize(ddmFormField, value));
}
else if (value.isLocalized()) {
jsonObject.put("value", toJSONObject(value));
}
else {
jsonObject.put("value", value.getString(LocaleUtil.ROOT));
}
}
private DDMFormFieldValueJSONSerializer _getDDMFormFieldValueJSONSerializer(
DDMFormField ddmFormField) {
if (ddmFormField == null) {
return null;
}
return _serviceTrackerMap.getService(ddmFormField.getType());
}
private JSONArray _toJSONArray(
Map<String, DDMFormField> ddmFormFieldsMap,
List<DDMFormFieldValue> ddmFormFieldValues) {
JSONArray jsonArray = _jsonFactory.createJSONArray();
for (DDMFormFieldValue ddmFormFieldValue : ddmFormFieldValues) {
jsonArray.put(toJSONObject(ddmFormFieldsMap, ddmFormFieldValue));
}
return jsonArray;
}
private JSONFactory _jsonFactory;
private ServiceTrackerMap<String, DDMFormFieldValueJSONSerializer>
_serviceTrackerMap;
}