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.template.TemplateConstants;
018    import com.liferay.portal.kernel.test.util.TestPropsValues;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
028    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
029    
030    import java.util.HashMap;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * @author Eudaldo Alonso
036     */
037    public class DDMTemplateTestUtil {
038    
039            public static void addDynamicContentElement(
040                    Element dynamicElementElement, String languageId, String value) {
041    
042                    Element dynamicContentElement = dynamicElementElement.addElement(
043                            "dynamic-content");
044    
045                    dynamicContentElement.addAttribute("language-id", languageId);
046                    dynamicContentElement.setText(value);
047            }
048    
049            public static Element addDynamicElementElement(
050                    Element element, String type, String name) {
051    
052                    Element dynamicElementElement = element.addElement("dynamic-element");
053    
054                    dynamicElementElement.addAttribute("name", name);
055                    dynamicElementElement.addAttribute("type", type);
056    
057                    return dynamicElementElement;
058            }
059    
060            public static DDMTemplate addTemplate(long structureId) throws Exception {
061                    return addTemplate(
062                            structureId, TemplateConstants.LANG_TYPE_VM, getSampleTemplateXSL(),
063                            LocaleUtil.getSiteDefault());
064            }
065    
066            public static DDMTemplate addTemplate(
067                            long structureId, Locale defaultLocale)
068                    throws Exception {
069    
070                    return addTemplate(
071                            structureId, TemplateConstants.LANG_TYPE_VM, getSampleTemplateXSL(),
072                            defaultLocale);
073            }
074    
075            public static DDMTemplate addTemplate(long groupId, long structureId)
076                    throws Exception {
077    
078                    return addTemplate(
079                            groupId, structureId, TemplateConstants.LANG_TYPE_VM,
080                            getSampleTemplateXSL(), LocaleUtil.getSiteDefault());
081            }
082    
083            public static DDMTemplate addTemplate(
084                            long groupId, long structureId, Locale defaultLocale)
085                    throws Exception {
086    
087                    return addTemplate(
088                            groupId, structureId, TemplateConstants.LANG_TYPE_VM,
089                            getSampleTemplateXSL(), defaultLocale);
090            }
091    
092            public static DDMTemplate addTemplate(
093                            long groupId, long classNameId, long classPK)
094                    throws Exception {
095    
096                    return addTemplate(
097                            groupId, classNameId, classPK, TemplateConstants.LANG_TYPE_VM,
098                            getSampleTemplateXSL(), LocaleUtil.getSiteDefault());
099            }
100    
101            public static DDMTemplate addTemplate(
102                            long groupId, long classNameId, long classPK, String language,
103                            String script, Locale defaultLocale)
104                    throws Exception {
105    
106                    Map<Locale, String> nameMap = new HashMap<>();
107    
108                    nameMap.put(defaultLocale, "Test Template");
109    
110                    ServiceContext serviceContext = new ServiceContext();
111    
112                    serviceContext.setAddGroupPermissions(true);
113                    serviceContext.setAddGuestPermissions(true);
114    
115                    return DDMTemplateLocalServiceUtil.addTemplate(
116                            TestPropsValues.getUserId(), groupId, classNameId, classPK, 0,
117                            nameMap, null, DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, null,
118                            language, script, serviceContext);
119            }
120    
121            public static DDMTemplate addTemplate(
122                            long groupId, long structureId, String language, String script,
123                            Locale defaultLocale)
124                    throws Exception {
125    
126                    return addTemplate(
127                            groupId, PortalUtil.getClassNameId(DDMStructure.class), structureId,
128                            language, script, defaultLocale);
129            }
130    
131            public static DDMTemplate addTemplate(
132                            long structureId, String language, String script)
133                    throws Exception {
134    
135                    return addTemplate(
136                            TestPropsValues.getGroupId(), structureId, language, script,
137                            LocaleUtil.getSiteDefault());
138            }
139    
140            public static DDMTemplate addTemplate(
141                            long structureId, String language, String script,
142                            Locale defaultLocale)
143                    throws Exception {
144    
145                    return addTemplate(
146                            TestPropsValues.getGroupId(), structureId, language, script,
147                            defaultLocale);
148            }
149    
150            public static Document createDocument(
151                    String availableLocales, String defaultLocale) {
152    
153                    Document document = SAXReaderUtil.createDocument();
154    
155                    Element rootElement = document.addElement("root");
156    
157                    rootElement.addAttribute("available-locales", availableLocales);
158                    rootElement.addAttribute("default-locale", defaultLocale);
159                    rootElement.addElement("request");
160    
161                    return document;
162            }
163    
164            public static String getSampleTemplateXSL() {
165                    return "$name.getData()";
166            }
167    
168    }