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.util.test;
016    
017    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
018    import com.liferay.portal.kernel.test.util.TestPropsValues;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.SetUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.xml.Attribute;
029    import com.liferay.portal.kernel.xml.Document;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.kernel.xml.Node;
032    import com.liferay.portal.kernel.xml.SAXReaderUtil;
033    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
034    import com.liferay.portal.kernel.xml.XPath;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
038    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
039    import com.liferay.portlet.dynamicdatamapping.model.DDMFormLayout;
040    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
042    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
043    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
045    
046    import java.util.Collections;
047    import java.util.HashMap;
048    import java.util.List;
049    import java.util.Locale;
050    import java.util.Map;
051    
052    /**
053     * @author Eudaldo Alonso
054     */
055    public class DDMStructureTestUtil {
056    
057            public static DDMStructure addStructure(long groupId, String className)
058                    throws Exception {
059    
060                    return addStructure(
061                            groupId, className, 0, getSampleDDMForm(),
062                            LocaleUtil.getSiteDefault(),
063                            ServiceContextTestUtil.getServiceContext());
064            }
065    
066            public static DDMStructure addStructure(
067                            long groupId, String className, DDMForm ddmForm)
068                    throws Exception {
069    
070                    return addStructure(
071                            groupId, className, 0, ddmForm, LocaleUtil.getSiteDefault(),
072                            ServiceContextTestUtil.getServiceContext());
073            }
074    
075            public static DDMStructure addStructure(
076                            long groupId, String className, DDMForm ddmForm,
077                            Locale defaultLocale)
078                    throws Exception {
079    
080                    return addStructure(
081                            groupId, className, 0, ddmForm, defaultLocale,
082                            ServiceContextTestUtil.getServiceContext());
083            }
084    
085            public static DDMStructure addStructure(
086                            long groupId, String className, Locale defaultLocale)
087                    throws Exception {
088    
089                    return addStructure(
090                            groupId, className, 0, getSampleDDMForm(), defaultLocale,
091                            ServiceContextTestUtil.getServiceContext());
092            }
093    
094            public static DDMStructure addStructure(
095                            long groupId, String className, long parentStructureId)
096                    throws Exception {
097    
098                    return addStructure(
099                            groupId, className, parentStructureId, getSampleDDMForm(),
100                            LocaleUtil.getSiteDefault(),
101                            ServiceContextTestUtil.getServiceContext());
102            }
103    
104            public static DDMStructure addStructure(
105                            long groupId, String className, long parentStructureId,
106                            DDMForm ddmForm, Locale defaultLocale,
107                            ServiceContext serviceContext)
108                    throws Exception {
109    
110                    Map<Locale, String> nameMap = new HashMap<>();
111    
112                    nameMap.put(defaultLocale, "Test Structure");
113    
114                    DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
115                    String ddlStorageType = GetterUtil.getString(
116                            PropsUtil.get(PropsKeys.DYNAMIC_DATA_LISTS_STORAGE_TYPE));
117    
118                    serviceContext.setAddGroupPermissions(true);
119                    serviceContext.setAddGuestPermissions(true);
120    
121                    return DDMStructureLocalServiceUtil.addStructure(
122                            TestPropsValues.getUserId(), groupId, parentStructureId,
123                            PortalUtil.getClassNameId(className), null, nameMap, null, ddmForm,
124                            ddmFormLayout, ddlStorageType, DDMStructureConstants.TYPE_DEFAULT,
125                            serviceContext);
126            }
127    
128            public static DDMStructure addStructure(String className) throws Exception {
129                    return addStructure(
130                            TestPropsValues.getGroupId(), className, 0, getSampleDDMForm(),
131                            LocaleUtil.getSiteDefault(),
132                            ServiceContextTestUtil.getServiceContext());
133            }
134    
135            public static DDMStructure addStructure(String className, DDMForm ddmForm)
136                    throws Exception {
137    
138                    return addStructure(
139                            TestPropsValues.getGroupId(), className, 0, ddmForm,
140                            LocaleUtil.getSiteDefault(),
141                            ServiceContextTestUtil.getServiceContext());
142            }
143    
144            public static DDMStructure addStructure(
145                            String className, DDMForm ddmForm, Locale defaultLocale)
146                    throws Exception {
147    
148                    return addStructure(
149                            TestPropsValues.getGroupId(), className, 0, ddmForm, defaultLocale,
150                            ServiceContextTestUtil.getServiceContext());
151            }
152    
153            public static DDMStructure addStructure(
154                            String className, Locale defaultLocale)
155                    throws Exception {
156    
157                    return addStructure(
158                            TestPropsValues.getGroupId(), className, 0,
159                            getSampleDDMForm(
160                                    "name", new Locale[] {LocaleUtil.US}, defaultLocale),
161                            defaultLocale, ServiceContextTestUtil.getServiceContext());
162            }
163    
164            public static DDMStructure addStructure(
165                            String className, Locale[] availableLocales, Locale defaultLocale)
166                    throws Exception {
167    
168                    return addStructure(
169                            TestPropsValues.getGroupId(), className, 0,
170                            getSampleDDMForm("name", availableLocales, defaultLocale),
171                            defaultLocale, ServiceContextTestUtil.getServiceContext());
172            }
173    
174            public static DDMForm getSampleDDMForm() {
175                    return getSampleDDMForm("name");
176            }
177    
178            public static DDMForm getSampleDDMForm(
179                    Locale[] availableLocales, Locale defaultLocale) {
180    
181                    return getSampleDDMForm("name", availableLocales, defaultLocale);
182            }
183    
184            public static DDMForm getSampleDDMForm(String name) {
185                    return getSampleDDMForm(
186                            name, new Locale[] {LocaleUtil.US}, LocaleUtil.US);
187            }
188    
189            public static DDMForm getSampleDDMForm(
190                    String name, Locale[] availableLocales, Locale defaultLocale) {
191    
192                    return getSampleDDMForm(
193                            name, "string", "text", true, "text", availableLocales,
194                            defaultLocale);
195            }
196    
197            public static DDMForm getSampleDDMForm(
198                    String name, String dataType, String indexType, boolean repeatable,
199                    String type, Locale[] availableLocales, Locale defaultLocale) {
200    
201                    DDMForm ddmForm = new DDMForm();
202    
203                    ddmForm.setAvailableLocales(SetUtil.fromArray(availableLocales));
204                    ddmForm.setDefaultLocale(defaultLocale);
205    
206                    DDMFormField ddmFormField = new DDMFormField(name, type);
207    
208                    ddmFormField.setDataType(dataType);
209                    ddmFormField.setIndexType(indexType);
210                    ddmFormField.setLocalizable(true);
211                    ddmFormField.setRepeatable(repeatable);
212    
213                    LocalizedValue label = new LocalizedValue(defaultLocale);
214    
215                    label.addString(defaultLocale, "Field");
216    
217                    ddmFormField.setLabel(label);
218    
219                    ddmForm.addDDMFormField(ddmFormField);
220    
221                    return ddmForm;
222            }
223    
224            public static String getSampleStructuredContent() {
225                    return getSampleStructuredContent("name", "title");
226            }
227    
228            public static String getSampleStructuredContent(
229                    Map<Locale, String> contents, String defaultLocale) {
230    
231                    return getSampleStructuredContent(
232                            "name", Collections.singletonList(contents), defaultLocale);
233            }
234    
235            public static String getSampleStructuredContent(String keywords) {
236                    return getSampleStructuredContent("name", keywords);
237            }
238    
239            public static String getSampleStructuredContent(
240                    String name, List<Map<Locale, String>> contents, String defaultLocale) {
241    
242                    StringBundler availableLocales = new StringBundler(2 * contents.size());
243    
244                    for (Map<Locale, String> map : contents) {
245                            StringBundler sb = new StringBundler(2 * map.size());
246    
247                            for (Locale locale : map.keySet()) {
248                                    sb.append(LocaleUtil.toLanguageId(locale));
249                                    sb.append(StringPool.COMMA);
250                            }
251    
252                            sb.setIndex(sb.index() - 1);
253    
254                            availableLocales.append(sb);
255                    }
256    
257                    Document document = createDocumentContent(
258                            availableLocales.toString(), defaultLocale);
259    
260                    Element rootElement = document.getRootElement();
261    
262                    for (Map<Locale, String> map : contents) {
263                            Element dynamicElementElement = rootElement.addElement(
264                                    "dynamic-element");
265    
266                            dynamicElementElement.addAttribute("index-type", "keyword");
267                            dynamicElementElement.addAttribute("name", name);
268                            dynamicElementElement.addAttribute("type", "text");
269    
270                            for (Map.Entry<Locale, String> entry : map.entrySet()) {
271                                    Element element = dynamicElementElement.addElement(
272                                            "dynamic-content");
273    
274                                    element.addAttribute(
275                                            "language-id", LocaleUtil.toLanguageId(entry.getKey()));
276                                    element.addCDATA(entry.getValue());
277                            }
278                    }
279    
280                    return document.asXML();
281            }
282    
283            public static String getSampleStructuredContent(
284                    String name, String keywords) {
285    
286                    Map<Locale, String> contents = new HashMap<>();
287    
288                    contents.put(Locale.US, keywords);
289    
290                    return getSampleStructuredContent(
291                            name, Collections.singletonList(contents), "en_US");
292            }
293    
294            public static Map<String, Map<String, String>> getXSDMap(String xsd)
295                    throws Exception {
296    
297                    Map<String, Map<String, String>> map = new HashMap<>();
298    
299                    Document document = UnsecureSAXReaderUtil.read(xsd);
300    
301                    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");
302    
303                    List<Node> nodes = xPathSelector.selectNodes(document);
304    
305                    for (Node node : nodes) {
306                            Element dynamicElementElement = (Element)node;
307    
308                            String elementName = getElementName(dynamicElementElement);
309    
310                            map.put(elementName, getElementMap(dynamicElementElement));
311                    }
312    
313                    return map;
314            }
315    
316            protected static Document createDocumentContent(
317                    String availableLocales, String defaultLocale) {
318    
319                    Document document = SAXReaderUtil.createDocument();
320    
321                    Element rootElement = document.addElement("root");
322    
323                    rootElement.addAttribute("available-locales", availableLocales);
324                    rootElement.addAttribute("default-locale", defaultLocale);
325                    rootElement.addElement("request");
326    
327                    return document;
328            }
329    
330            protected static Document createDocumentStructure(
331                    Locale[] availableLocales, Locale defaultLocale) {
332    
333                    Document document = SAXReaderUtil.createDocument();
334    
335                    Element rootElement = document.addElement("root");
336    
337                    rootElement.addAttribute(
338                            "available-locales",
339                            StringUtil.merge(LocaleUtil.toLanguageIds(availableLocales)));
340                    rootElement.addAttribute(
341                            "default-locale", LocaleUtil.toLanguageId(defaultLocale));
342    
343                    return document;
344            }
345    
346            protected static Map<String, String> getElementMap(Element element) {
347                    Map<String, String> elementMap = new HashMap<>();
348    
349                    // Attributes
350    
351                    for (Attribute attribute : element.attributes()) {
352                            elementMap.put(attribute.getName(), attribute.getValue());
353                    }
354    
355                    // Metadata
356    
357                    for (Element metadadataElement : element.elements("meta-data")) {
358                            String metadataLanguageId = metadadataElement.attributeValue(
359                                    "locale");
360    
361                            for (Element entryElement : metadadataElement.elements("entry")) {
362                                    String entryName = entryElement.attributeValue("name");
363    
364                                    elementMap.put(
365                                            entryName.concat(metadataLanguageId),
366                                            entryElement.getText());
367                            }
368                    }
369    
370                    return elementMap;
371            }
372    
373            protected static String getElementName(Element element) {
374                    StringBuilder sb = new StringBuilder();
375    
376                    sb.append(element.attributeValue("name"));
377    
378                    Element parentElement = element.getParent();
379    
380                    while (true) {
381                            if ((parentElement == null) ||
382                                    parentElement.getName().equals("root")) {
383    
384                                    break;
385                            }
386    
387                            sb.insert(
388                                    0, parentElement.attributeValue("name") + StringPool.SLASH);
389    
390                            parentElement = parentElement.getParent();
391                    }
392    
393                    String type = element.attributeValue("type");
394    
395                    if (Validator.equals(type, "option")) {
396                            sb.append(StringPool.SLASH);
397    
398                            sb.append(element.attributeValue("value"));
399                    }
400    
401                    return sb.toString();
402            }
403    
404    }