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