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.util.LocaleUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.SAXReaderUtil;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldOptions;
025    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
026    
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.Set;
032    
033    /**
034     * @author Pablo Carvalho
035     */
036    public class DDMFormXSDSerializerImpl implements DDMFormXSDSerializer {
037    
038            @Override
039            public String serialize(DDMForm ddmForm) {
040                    Document document = SAXReaderUtil.createDocument();
041    
042                    Element rootElement = document.addElement("root");
043    
044                    rootElement.addAttribute(
045                            "available-locales", getAvailableLanguagesIds(ddmForm));
046                    rootElement.addAttribute(
047                            "default-locale",
048                            LocaleUtil.toLanguageId(ddmForm.getDefaultLocale()));
049    
050                    addDynamicElementElements(ddmForm.getDDMFormFields(), rootElement);
051    
052                    return document.asXML();
053            }
054    
055            protected void addDynamicElementAttributes(
056                    DDMFormField ddmFormField, Element dynamicElementElement) {
057    
058                    dynamicElementElement.addAttribute(
059                            "dataType", ddmFormField.getDataType());
060                    dynamicElementElement.addAttribute(
061                            "fieldNamespace", ddmFormField.getNamespace());
062                    dynamicElementElement.addAttribute(
063                            "indexType", ddmFormField.getIndexType());
064                    dynamicElementElement.addAttribute(
065                            "localizable", Boolean.toString(ddmFormField.isLocalizable()));
066                    dynamicElementElement.addAttribute(
067                            "multiple", Boolean.toString(ddmFormField.isMultiple()));
068                    dynamicElementElement.addAttribute("name", ddmFormField.getName());
069                    dynamicElementElement.addAttribute(
070                            "readOnly", Boolean.toString(ddmFormField.isReadOnly()));
071                    dynamicElementElement.addAttribute(
072                            "repeatable", Boolean.toString(ddmFormField.isRepeatable()));
073                    dynamicElementElement.addAttribute(
074                            "required", Boolean.toString(ddmFormField.isRequired()));
075                    dynamicElementElement.addAttribute(
076                            "showLabel", Boolean.toString(ddmFormField.isShowLabel()));
077                    dynamicElementElement.addAttribute("type", ddmFormField.getType());
078            }
079    
080            protected void addDynamicElementElement(
081                    DDMFormField ddmFormField, Element element) {
082    
083                    Element dynamicElementElement = element.addElement("dynamic-element");
084    
085                    addDynamicElementAttributes(ddmFormField, dynamicElementElement);
086    
087                    addDynamicElementElements(
088                            ddmFormField.getNestedDDMFormFields(), dynamicElementElement);
089    
090                    String ddmFormFieldType = ddmFormField.getType();
091    
092                    if (ddmFormFieldType.equals("radio") ||
093                            ddmFormFieldType.equals("select")) {
094    
095                            addOptionsDynamicElements(
096                                    ddmFormField.getDDMFormFieldOptions(), dynamicElementElement);
097                    }
098    
099                    Map<Locale, Map<String, String>> metadataMap =
100                            getDDMFormFieldMetadataMap(ddmFormField);
101    
102                    addMetadataElements(metadataMap, dynamicElementElement);
103            }
104    
105            protected void addDynamicElementElements(
106                    List<DDMFormField> ddmFormFields, Element element) {
107    
108                    for (DDMFormField ddmFormField : ddmFormFields) {
109                            addDynamicElementElement(ddmFormField, element);
110                    }
111            }
112    
113            protected void addMetadataElements(
114                    Map<Locale, Map<String, String>> metadataMap,
115                    Element dynamicElementElement) {
116    
117                    for (Locale locale : metadataMap.keySet()) {
118                            Element metadataElement = dynamicElementElement.addElement(
119                                    "meta-data");
120    
121                            metadataElement.addAttribute(
122                                    "locale", LocaleUtil.toLanguageId(locale));
123    
124                            addMetadataEntry(metadataMap.get(locale), metadataElement);
125                    }
126            }
127    
128            protected void addMetadataEntry(
129                    Map<String, String> entryMap, Element metadataElement) {
130    
131                    for (Map.Entry<String, String> entry : entryMap.entrySet()) {
132                            Element entryElement = metadataElement.addElement("entry");
133    
134                            entryElement.addAttribute("name", entry.getKey());
135    
136                            entryElement.addText(entry.getValue());
137                    }
138            }
139    
140            protected void addMetadataEntryValues(
141                    Map<Locale, Map<String, String>> ddmFormFieldMetadataMap,
142                    String entryName, LocalizedValue localizedValue) {
143    
144                    for (Locale availableLocale : localizedValue.getAvailableLocales()) {
145                            Map<String, String> metadataMap = ddmFormFieldMetadataMap.get(
146                                    availableLocale);
147    
148                            if (metadataMap == null) {
149                                    metadataMap = new HashMap<String, String>();
150    
151                                    ddmFormFieldMetadataMap.put(availableLocale, metadataMap);
152                            }
153    
154                            metadataMap.put(
155                                    entryName, localizedValue.getString(availableLocale));
156                    }
157            }
158    
159            protected Element addOptionDynamicElement(
160                    String optionValue, Element dynamicElement) {
161    
162                    Element optionElement = dynamicElement.addElement("dynamic-element");
163    
164                    optionElement.addAttribute("name", "option_" + StringUtil.randomId());
165                    optionElement.addAttribute("type", "option");
166                    optionElement.addAttribute("value", optionValue);
167    
168                    return optionElement;
169            }
170    
171            protected void addOptionsDynamicElements(
172                    DDMFormFieldOptions ddmFormFieldOptions,
173                    Element dynamicElementElement) {
174    
175                    for (String optionValue : ddmFormFieldOptions.getOptionsValues()) {
176                            Element optionElement = addOptionDynamicElement(
177                                    optionValue, dynamicElementElement);
178    
179                            Map<Locale, Map<String, String>> optionLabelsMap =
180                                    getOptionLabelsMap(
181                                            ddmFormFieldOptions.getOptionLabels(optionValue));
182    
183                            addMetadataElements(optionLabelsMap, optionElement);
184                    }
185            }
186    
187            protected String getAvailableLanguagesIds(DDMForm ddmForm) {
188                    Set<Locale> availableLocales = ddmForm.getAvailableLocales();
189    
190                    String[] availableLanguageIds = LocaleUtil.toLanguageIds(
191                            availableLocales.toArray(new Locale[availableLocales.size()]));
192    
193                    return StringUtil.merge(availableLanguageIds);
194            }
195    
196            protected Map<Locale, Map<String, String>> getDDMFormFieldMetadataMap(
197                    DDMFormField ddmFormField) {
198    
199                    Map<Locale, Map<String, String>> ddmFormFieldMetadataMap =
200                            new HashMap<Locale, Map<String, String>>();
201    
202                    addMetadataEntryValues(
203                            ddmFormFieldMetadataMap, "label", ddmFormField.getLabel());
204    
205                    addMetadataEntryValues(
206                            ddmFormFieldMetadataMap, "predefinedValue",
207                            ddmFormField.getPredefinedValue());
208    
209                    addMetadataEntryValues(
210                            ddmFormFieldMetadataMap, "tip", ddmFormField.getTip());
211    
212                    return ddmFormFieldMetadataMap;
213            }
214    
215            protected Map<Locale, Map<String, String>> getOptionLabelsMap(
216                    LocalizedValue optionLabels) {
217    
218                    Map<Locale, Map<String, String>> optionLabelsMap =
219                            new HashMap<Locale, Map<String, String>>();
220    
221                    for (Locale availableLocale : optionLabels.getAvailableLocales()) {
222                            Map<String, String> optionMetadataEntries =
223                                    new HashMap<String, String>();
224    
225                            optionMetadataEntries.put(
226                                    "label", optionLabels.getString(availableLocale));
227    
228                            optionLabelsMap.put(availableLocale, optionMetadataEntries);
229                    }
230    
231                    return optionLabelsMap;
232            }
233    
234    }