001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.io;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldOptions;
024    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
025    
026    import java.util.List;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.Set;
030    
031    /**
032     * @author Marcellus Tavares
033     */
034    public class DDMFormJSONSerializerImpl implements DDMFormJSONSerializer {
035    
036            @Override
037            public String serialize(DDMForm ddmForm) {
038                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
039    
040                    addAvailableLanguageIds(jsonObject, ddmForm.getAvailableLocales());
041                    addDefaultLanguageId(jsonObject, ddmForm.getDefaultLocale());
042                    addFields(jsonObject, ddmForm.getDDMFormFields());
043    
044                    return jsonObject.toString();
045            }
046    
047            protected void addAvailableLanguageIds(
048                    JSONObject jsonObject, Set<Locale> availableLocales) {
049    
050                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
051    
052                    for (Locale availableLocale : availableLocales) {
053                            jsonArray.put(LocaleUtil.toLanguageId(availableLocale));
054                    }
055    
056                    jsonObject.put("availableLanguageIds", jsonArray);
057            }
058    
059            protected void addDefaultLanguageId(
060                    JSONObject jsonObject, Locale defaultLocale) {
061    
062                    jsonObject.put(
063                            "defaultLanguageId", LocaleUtil.toLanguageId(defaultLocale));
064            }
065    
066            protected void addFields(
067                    JSONObject jsonObject, List<DDMFormField> ddmFormFields) {
068    
069                    jsonObject.put("fields", toJSONArray(ddmFormFields));
070            }
071    
072            protected void addLocalizedProperty(
073                    JSONObject jsonObject, String propertyName,
074                    LocalizedValue localizedValue) {
075    
076                    Map<Locale, String> values = localizedValue.getValues();
077    
078                    if (values.isEmpty()) {
079                            return;
080                    }
081    
082                    jsonObject.put(propertyName, toJSONObject(localizedValue));
083            }
084    
085            protected void addNestedFields(
086                    JSONObject jsonObject, List<DDMFormField> nestedDDMFormFields) {
087    
088                    if (nestedDDMFormFields.isEmpty()) {
089                            return;
090                    }
091    
092                    jsonObject.put("nestedFields", toJSONArray(nestedDDMFormFields));
093            }
094    
095            protected void addOptions(
096                    JSONObject jsonObject, DDMFormFieldOptions ddmFormFieldOptions) {
097    
098                    Set<String> optionValues = ddmFormFieldOptions.getOptionsValues();
099    
100                    if (optionValues.isEmpty()) {
101                            return;
102                    }
103    
104                    jsonObject.put("options", toJSONArray(ddmFormFieldOptions));
105            }
106    
107            protected void addSimpleProperties(
108                    JSONObject jsonObject, DDMFormField ddmFormField) {
109    
110                    jsonObject.put("dataType", ddmFormField.getDataType());
111                    jsonObject.put("fieldNamespace", ddmFormField.getNamespace());
112                    jsonObject.put("indexType", ddmFormField.getIndexType());
113                    jsonObject.put("localizable", ddmFormField.isLocalizable());
114                    jsonObject.put("multiple", ddmFormField.isMultiple());
115                    jsonObject.put("name", ddmFormField.getName());
116                    jsonObject.put("readOnly", ddmFormField.isReadOnly());
117                    jsonObject.put("repeatable", ddmFormField.isRepeatable());
118                    jsonObject.put("required", ddmFormField.isRequired());
119                    jsonObject.put("showLabel", ddmFormField.isShowLabel());
120                    jsonObject.put("type", ddmFormField.getType());
121            }
122    
123            protected JSONArray toJSONArray(DDMFormFieldOptions ddmFormFieldOptions) {
124                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
125    
126                    for (String optionValue : ddmFormFieldOptions.getOptionsValues()) {
127                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
128    
129                            jsonObject.put("value", optionValue);
130                            jsonObject.put(
131                                    "label",
132                                    toJSONObject(ddmFormFieldOptions.getOptionLabels(optionValue)));
133    
134                            jsonArray.put(jsonObject);
135                    }
136    
137                    return jsonArray;
138            }
139    
140            protected JSONArray toJSONArray(List<DDMFormField> ddmFormFields) {
141                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
142    
143                    for (DDMFormField ddmFormField : ddmFormFields) {
144                            jsonArray.put(toJSONObject(ddmFormField));
145                    }
146    
147                    return jsonArray;
148            }
149    
150            protected JSONObject toJSONObject(DDMFormField ddmFormField) {
151                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
152    
153                    addLocalizedProperty(jsonObject, "label", ddmFormField.getLabel());
154                    addLocalizedProperty(
155                            jsonObject, "predefinedValue", ddmFormField.getPredefinedValue());
156                    addLocalizedProperty(jsonObject, "style", ddmFormField.getStyle());
157                    addLocalizedProperty(jsonObject, "tip", ddmFormField.getTip());
158                    addNestedFields(jsonObject, ddmFormField.getNestedDDMFormFields());
159                    addOptions(jsonObject, ddmFormField.getDDMFormFieldOptions());
160                    addSimpleProperties(jsonObject, ddmFormField);
161    
162                    return jsonObject;
163            }
164    
165            protected JSONObject toJSONObject(LocalizedValue localizedValue) {
166                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
167    
168                    for (Locale availableLocale : localizedValue.getAvailableLocales()) {
169                            jsonObject.put(
170                                    LocaleUtil.toLanguageId(availableLocale),
171                                    localizedValue.getString(availableLocale));
172                    }
173    
174                    return jsonObject;
175            }
176    
177    }