001
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.DDMFormLayout;
022 import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutColumn;
023 import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutPage;
024 import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutRow;
025 import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
026
027 import java.util.List;
028 import java.util.Locale;
029 import java.util.Map;
030
031
034 public class DDMFormLayoutJSONSerializerImpl
035 implements DDMFormLayoutJSONSerializer {
036
037 @Override
038 public String serialize(DDMFormLayout ddmFormLayout) {
039 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
040
041 addDefaultLanguageId(jsonObject, ddmFormLayout.getDefaultLocale());
042 addPages(jsonObject, ddmFormLayout.getDDMFormLayoutPages());
043
044 return jsonObject.toString();
045 }
046
047 protected void addColumns(
048 JSONObject jsonObject, List<DDMFormLayoutColumn> ddmFormLayoutColumns) {
049
050 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
051
052 for (DDMFormLayoutColumn ddmFormLayoutColumn : ddmFormLayoutColumns) {
053 jsonArray.put(toJSONObject(ddmFormLayoutColumn));
054 }
055
056 jsonObject.put("columns", 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 addDescription(
067 JSONObject pageJSONObject, LocalizedValue description) {
068
069 Map<Locale, String> values = description.getValues();
070
071 if (values.isEmpty()) {
072 return;
073 }
074
075 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
076
077 for (Locale availableLocale : description.getAvailableLocales()) {
078 jsonObject.put(
079 LocaleUtil.toLanguageId(availableLocale),
080 description.getString(availableLocale));
081 }
082
083 pageJSONObject.put("description", jsonObject);
084 }
085
086 protected void addFieldNames(
087 JSONObject jsonObject, List<String> ddmFormFieldNames) {
088
089 if (ddmFormFieldNames.isEmpty()) {
090 return;
091 }
092
093 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
094
095 for (String ddmFormFieldName : ddmFormFieldNames) {
096 jsonArray.put(ddmFormFieldName);
097 }
098
099 jsonObject.put("fieldNames", jsonArray);
100 }
101
102 protected void addPages(
103 JSONObject jsonObject, List<DDMFormLayoutPage> ddmFormLayoutPages) {
104
105 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
106
107 for (DDMFormLayoutPage ddmFormLayoutPage : ddmFormLayoutPages) {
108 jsonArray.put(toJSONObject(ddmFormLayoutPage));
109 }
110
111 jsonObject.put("pages", jsonArray);
112 }
113
114 protected void addRows(
115 JSONObject jsonObject, List<DDMFormLayoutRow> ddmFormLayoutRows) {
116
117 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
118
119 for (DDMFormLayoutRow ddmFormLayoutRow : ddmFormLayoutRows) {
120 jsonArray.put(toJSONObject(ddmFormLayoutRow));
121 }
122
123 jsonObject.put("rows", jsonArray);
124 }
125
126 protected void addTitle(JSONObject pageJSONObject, LocalizedValue title) {
127 Map<Locale, String> values = title.getValues();
128
129 if (values.isEmpty()) {
130 return;
131 }
132
133 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
134
135 for (Locale availableLocale : title.getAvailableLocales()) {
136 jsonObject.put(
137 LocaleUtil.toLanguageId(availableLocale),
138 title.getString(availableLocale));
139 }
140
141 pageJSONObject.put("title", jsonObject);
142 }
143
144 protected JSONObject toJSONObject(DDMFormLayoutColumn ddmFormLayoutColumn) {
145 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
146
147 jsonObject.put("size", ddmFormLayoutColumn.getSize());
148
149 addFieldNames(jsonObject, ddmFormLayoutColumn.getDDMFormFieldNames());
150
151 return jsonObject;
152 }
153
154 protected JSONObject toJSONObject(DDMFormLayoutPage ddmFormLayoutPage) {
155 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
156
157 addDescription(jsonObject, ddmFormLayoutPage.getDescription());
158 addRows(jsonObject, ddmFormLayoutPage.getDDMFormLayoutRows());
159 addTitle(jsonObject, ddmFormLayoutPage.getTitle());
160
161 return jsonObject;
162 }
163
164 protected JSONObject toJSONObject(DDMFormLayoutRow ddmFormLayoutRow) {
165 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
166
167 addColumns(jsonObject, ddmFormLayoutRow.getDDMFormLayoutColumns());
168
169 return jsonObject;
170 }
171
172 }