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.LocaleUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
020    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
021    import com.liferay.portlet.dynamicdatamapping.model.UnlocalizedValue;
022    import com.liferay.portlet.dynamicdatamapping.model.Value;
023    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
024    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
025    
026    import java.util.LinkedHashSet;
027    import java.util.Locale;
028    import java.util.Set;
029    
030    /**
031     * @author Marcellus Tavares
032     */
033    public class DDMFormValuesTestUtil {
034    
035            public static Set<Locale> createAvailableLocales(Locale... locales) {
036                    Set<Locale> availableLocales = new LinkedHashSet<>();
037    
038                    for (Locale locale : locales) {
039                            availableLocales.add(locale);
040                    }
041    
042                    return availableLocales;
043            }
044    
045            public static DDMFormFieldValue createDDMFormFieldValue(
046                    String instanceId, String name, Value value) {
047    
048                    DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();
049    
050                    ddmFormFieldValue.setInstanceId(instanceId);
051                    ddmFormFieldValue.setName(name);
052                    ddmFormFieldValue.setValue(value);
053    
054                    return ddmFormFieldValue;
055            }
056    
057            public static DDMFormFieldValue createDDMFormFieldValue(
058                    String name, Value value) {
059    
060                    return createDDMFormFieldValue(StringUtil.randomString(), name, value);
061            }
062    
063            public static DDMFormValues createDDMFormValues(DDMForm ddmForm) {
064                    DDMFormValues ddmFormValues = new DDMFormValues(ddmForm);
065    
066                    ddmFormValues.setAvailableLocales(ddmForm.getAvailableLocales());
067                    ddmFormValues.setDefaultLocale(ddmForm.getDefaultLocale());
068    
069                    return ddmFormValues;
070            }
071    
072            public static DDMFormValues createDDMFormValues(
073                    DDMForm ddmForm, Set<Locale> availableLocales, Locale defaultLocale) {
074    
075                    DDMFormValues ddmFormValues = new DDMFormValues(ddmForm);
076    
077                    ddmFormValues.setAvailableLocales(availableLocales);
078                    ddmFormValues.setDefaultLocale(defaultLocale);
079    
080                    return ddmFormValues;
081            }
082    
083            public static DDMFormFieldValue createLocalizedDDMFormFieldValue(
084                    String name, String enValue) {
085    
086                    Value localizedValue = new LocalizedValue(LocaleUtil.US);
087    
088                    localizedValue.addString(LocaleUtil.US, enValue);
089    
090                    return createDDMFormFieldValue(name, localizedValue);
091            }
092    
093            public static LocalizedValue createLocalizedValue(
094                    String enValue, Locale defaultLocale) {
095    
096                    LocalizedValue localizedValue = new LocalizedValue(defaultLocale);
097    
098                    localizedValue.addString(LocaleUtil.US, enValue);
099    
100                    return localizedValue;
101            }
102    
103            public static LocalizedValue createLocalizedValue(
104                    String enValue, String ptValue, Locale defaultLocale) {
105    
106                    LocalizedValue localizedValue = new LocalizedValue(defaultLocale);
107    
108                    localizedValue.addString(LocaleUtil.BRAZIL, ptValue);
109                    localizedValue.addString(LocaleUtil.US, enValue);
110    
111                    return localizedValue;
112            }
113    
114            public static DDMFormFieldValue createUnlocalizedDDMFormFieldValue(
115                    String name, String value) {
116    
117                    return createDDMFormFieldValue(name, new UnlocalizedValue(value));
118            }
119    
120    }