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.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayout;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutColumn;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutPage;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayoutRow;
026    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
027    
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.List;
031    import java.util.Locale;
032    
033    /**
034     * @author Marcellus Tavares
035     */
036    public class DDMFormLayoutJSONDeserializerImpl
037            implements DDMFormLayoutJSONDeserializer {
038    
039            @Override
040            public DDMFormLayout deserialize(String serializedDDMFormLayout)
041                    throws PortalException {
042    
043                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
044                            serializedDDMFormLayout);
045    
046                    DDMFormLayout ddmFormLayout = new DDMFormLayout();
047    
048                    setDDMFormLayoutDefaultLocale(
049                            jsonObject.getString("defaultLanguageId"), ddmFormLayout);
050                    setDDMFormLayoutPages(jsonObject.getJSONArray("pages"), ddmFormLayout);
051                    setDDMFormLayoutPageTitlesDefaultLocale(ddmFormLayout);
052    
053                    return ddmFormLayout;
054            }
055    
056            protected DDMFormLayoutColumn getDDMFormLayoutColumn(
057                    JSONObject jsonObject) {
058    
059                    DDMFormLayoutColumn ddmFormLayoutColumn = new DDMFormLayoutColumn(
060                            jsonObject.getInt("size"));
061    
062                    setDDMFormLayouColumnFieldNames(
063                            jsonObject.getJSONArray("fieldNames"), ddmFormLayoutColumn);
064    
065                    return ddmFormLayoutColumn;
066            }
067    
068            protected List<String> getDDMFormLayoutColumnFieldNames(
069                    JSONArray jsonArray) {
070    
071                    List<String> ddmFormFieldNames = new ArrayList<>();
072    
073                    for (int i = 0; i < jsonArray.length(); i++) {
074                            ddmFormFieldNames.add(jsonArray.getString(i));
075                    }
076    
077                    return ddmFormFieldNames;
078            }
079    
080            protected List<DDMFormLayoutColumn> getDDMFormLayoutColumns(
081                    JSONArray jsonArray) {
082    
083                    List<DDMFormLayoutColumn> ddmFormLayoutColumns = new ArrayList<>();
084    
085                    for (int i = 0; i < jsonArray.length(); i++) {
086                            DDMFormLayoutColumn ddmFormLayoutColumn = getDDMFormLayoutColumn(
087                                    jsonArray.getJSONObject(i));
088    
089                            ddmFormLayoutColumns.add(ddmFormLayoutColumn);
090                    }
091    
092                    return ddmFormLayoutColumns;
093            }
094    
095            protected DDMFormLayoutPage getDDMFormLayoutPage(JSONObject jsonObject) {
096                    DDMFormLayoutPage ddmFormLayoutPage = new DDMFormLayoutPage();
097    
098                    setDDMFormLayoutPageDescription(
099                            jsonObject.getJSONObject("description"), ddmFormLayoutPage);
100                    setDDMFormLayoutPageRows(
101                            jsonObject.getJSONArray("rows"), ddmFormLayoutPage);
102                    setDDMFormLayoutPageTitle(
103                            jsonObject.getJSONObject("title"), ddmFormLayoutPage);
104    
105                    return ddmFormLayoutPage;
106            }
107    
108            protected List<DDMFormLayoutPage> getDDMFormLayoutPages(
109                    JSONArray jsonArray) {
110    
111                    List<DDMFormLayoutPage> ddmFormLayoutPages = new ArrayList<>();
112    
113                    for (int i = 0; i < jsonArray.length(); i++) {
114                            DDMFormLayoutPage ddmFormLayoutPage = getDDMFormLayoutPage(
115                                    jsonArray.getJSONObject(i));
116    
117                            ddmFormLayoutPages.add(ddmFormLayoutPage);
118                    }
119    
120                    return ddmFormLayoutPages;
121            }
122    
123            protected DDMFormLayoutRow getDDMFormLayoutRow(JSONObject jsonObject) {
124                    DDMFormLayoutRow ddmFormLayoutRow = new DDMFormLayoutRow();
125    
126                    setDDMFormLayoutRowColumns(
127                            jsonObject.getJSONArray("columns"), ddmFormLayoutRow);
128    
129                    return ddmFormLayoutRow;
130            }
131    
132            protected List<DDMFormLayoutRow> getDDMFormLayoutRows(JSONArray jsonArray) {
133                    List<DDMFormLayoutRow> ddmFormLayoutRows = new ArrayList<>();
134    
135                    for (int i = 0; i < jsonArray.length(); i++) {
136                            DDMFormLayoutRow ddmFormLayoutRow = getDDMFormLayoutRow(
137                                    jsonArray.getJSONObject(i));
138    
139                            ddmFormLayoutRows.add(ddmFormLayoutRow);
140                    }
141    
142                    return ddmFormLayoutRows;
143            }
144    
145            protected LocalizedValue getTitle(JSONObject jsonObject) {
146                    if (jsonObject == null) {
147                            return null;
148                    }
149    
150                    LocalizedValue title = new LocalizedValue();
151    
152                    Iterator<String> itr = jsonObject.keys();
153    
154                    while (itr.hasNext()) {
155                            String languageId = itr.next();
156    
157                            title.addString(
158                                    LocaleUtil.fromLanguageId(languageId),
159                                    jsonObject.getString(languageId));
160                    }
161    
162                    return title;
163            }
164    
165            protected void setDDMFormLayouColumnFieldNames(
166                    JSONArray jsonArray, DDMFormLayoutColumn ddmFormLayoutColumn) {
167    
168                    List<String> ddmFormLayoutColumnNames =
169                            getDDMFormLayoutColumnFieldNames(jsonArray);
170    
171                    ddmFormLayoutColumn.setDDMFormFieldNames(ddmFormLayoutColumnNames);
172            }
173    
174            protected void setDDMFormLayoutDefaultLocale(
175                    String defaultLanguageId, DDMFormLayout ddmFormLayout) {
176    
177                    Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId);
178    
179                    ddmFormLayout.setDefaultLocale(defaultLocale);
180            }
181    
182            protected void setDDMFormLayoutPageDescription(
183                    JSONObject jsonObject, DDMFormLayoutPage ddmFormLayoutPage) {
184    
185                    LocalizedValue description = getDescription(jsonObject);
186    
187                    if (description == null) {
188                            return;
189                    }
190    
191                    ddmFormLayoutPage.setDescription(description);
192            }
193    
194            protected void setDDMFormLayoutPageRows(
195                    JSONArray jsonArray, DDMFormLayoutPage ddmFormLayoutPage) {
196    
197                    List<DDMFormLayoutRow> ddmFormLayoutRows = getDDMFormLayoutRows(
198                            jsonArray);
199    
200                    ddmFormLayoutPage.setDDMFormLayoutRows(ddmFormLayoutRows);
201            }
202    
203            protected void setDDMFormLayoutPages(
204                    JSONArray jsonArray, DDMFormLayout ddmFormLayout) {
205    
206                    List<DDMFormLayoutPage> ddmFormLayoutPages = getDDMFormLayoutPages(
207                            jsonArray);
208    
209                    ddmFormLayout.setDDMFormLayoutPages(ddmFormLayoutPages);
210            }
211    
212            protected void setDDMFormLayoutPageTitle(
213                    JSONObject jsonObject, DDMFormLayoutPage ddmFormLayoutPage) {
214    
215                    LocalizedValue title = getTitle(jsonObject);
216    
217                    if (title == null) {
218                            return;
219                    }
220    
221                    ddmFormLayoutPage.setTitle(title);
222            }
223    
224            protected void setDDMFormLayoutPageTitlesDefaultLocale(
225                    DDMFormLayout ddmFormLayout) {
226    
227                    for (DDMFormLayoutPage ddmFormLayoutPage :
228                                    ddmFormLayout.getDDMFormLayoutPages()) {
229    
230                            LocalizedValue title = ddmFormLayoutPage.getTitle();
231    
232                            title.setDefaultLocale(ddmFormLayout.getDefaultLocale());
233                    }
234            }
235    
236            protected void setDDMFormLayoutRowColumns(
237                    JSONArray jsonArray, DDMFormLayoutRow ddmFormLayoutRow) {
238    
239                    List<DDMFormLayoutColumn> ddmFormLayoutColumns =
240                            getDDMFormLayoutColumns(jsonArray);
241    
242                    ddmFormLayoutRow.setDDMFormLayoutColumns(ddmFormLayoutColumns);
243            }
244    
245            private LocalizedValue getDescription(JSONObject jsonObject) {
246                    if (jsonObject == null) {
247                            return null;
248                    }
249    
250                    LocalizedValue description = new LocalizedValue();
251    
252                    Iterator<String> itr = jsonObject.keys();
253    
254                    while (itr.hasNext()) {
255                            String languageId = itr.next();
256    
257                            description.addString(
258                                    LocaleUtil.fromLanguageId(languageId),
259                                    jsonObject.getString(languageId));
260                    }
261    
262                    return description;
263            }
264    
265    }