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.LocaleUtil;
020    import com.liferay.portal.kernel.util.SetUtil;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.dynamicdatamapping.DDMForm;
024    import com.liferay.portlet.dynamicdatamapping.DDMFormField;
025    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.DDMStructureManager;
027    import com.liferay.portlet.dynamicdatamapping.DDMStructureManagerUtil;
028    import com.liferay.portlet.dynamicdatamapping.LocalizedValue;
029    import com.liferay.portlet.dynamicdatamapping.StorageEngineManager;
030    
031    import java.util.HashMap;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    /**
036     * @author Eudaldo Alonso
037     * @author Rafael Praxedes
038     */
039    public class DDMStructureTestUtil {
040    
041            public static DDMStructure addStructure(long groupId, String className)
042                    throws Exception {
043    
044                    return addStructure(
045                            groupId, className, null, getSampleDDMForm(),
046                            LocaleUtil.getSiteDefault(),
047                            ServiceContextTestUtil.getServiceContext());
048            }
049    
050            public static DDMStructure addStructure(
051                            long groupId, String className, String parentStructureId,
052                            DDMForm ddmForm, Locale defaultLocale,
053                            ServiceContext serviceContext)
054                    throws Exception {
055    
056                    Map<Locale, String> nameMap = new HashMap<>();
057    
058                    nameMap.put(defaultLocale, "Test Structure");
059    
060                    serviceContext.setAddGroupPermissions(true);
061                    serviceContext.setAddGuestPermissions(true);
062    
063                    return DDMStructureManagerUtil.addStructure(
064                            TestPropsValues.getUserId(), groupId, parentStructureId,
065                            PortalUtil.getClassNameId(className), null, nameMap, null, ddmForm,
066                            StorageEngineManager.STORAGE_TYPE_DEFAULT,
067                            DDMStructureManager.STRUCTURE_TYPE_DEFAULT, serviceContext);
068            }
069    
070            public static DDMStructure addStructure(String className) throws Exception {
071                    return addStructure(
072                            TestPropsValues.getGroupId(), className, null, getSampleDDMForm(),
073                            LocaleUtil.getSiteDefault(),
074                            ServiceContextTestUtil.getServiceContext());
075            }
076    
077            public static DDMForm getSampleDDMForm() {
078                    return getSampleDDMForm("name");
079            }
080    
081            public static DDMForm getSampleDDMForm(
082                    Locale[] availableLocales, Locale defaultLocale) {
083    
084                    return getSampleDDMForm("name", availableLocales, defaultLocale);
085            }
086    
087            public static DDMForm getSampleDDMForm(String name) {
088                    return getSampleDDMForm(
089                            name, new Locale[] {LocaleUtil.US}, LocaleUtil.US);
090            }
091    
092            public static DDMForm getSampleDDMForm(
093                    String name, Locale[] availableLocales, Locale defaultLocale) {
094    
095                    return getSampleDDMForm(
096                            name, "string", "text", true, "text", availableLocales,
097                            defaultLocale);
098            }
099    
100            public static DDMForm getSampleDDMForm(
101                    String name, String dataType, String indexType, boolean repeatable,
102                    String type, Locale[] availableLocales, Locale defaultLocale) {
103    
104                    DDMForm ddmForm = new DDMForm();
105    
106                    ddmForm.setAvailableLocales(SetUtil.fromArray(availableLocales));
107                    ddmForm.setDefaultLocale(defaultLocale);
108    
109                    DDMFormField ddmFormField = new DDMFormField(name, type);
110    
111                    ddmFormField.setDataType(dataType);
112                    ddmFormField.setIndexType(indexType);
113                    ddmFormField.setLocalizable(true);
114                    ddmFormField.setRepeatable(repeatable);
115    
116                    LocalizedValue label = new LocalizedValue(defaultLocale);
117    
118                    label.addString(defaultLocale, "Field");
119    
120                    ddmFormField.setLabel(label);
121    
122                    ddmForm.addDDMFormField(ddmFormField);
123    
124                    return ddmForm;
125            }
126    
127    }