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.portal.kernel.test.util;
016    
017    import com.liferay.portal.kernel.cache.thread.local.Lifecycle;
018    import com.liferay.portal.kernel.cache.thread.local.ThreadLocalCacheManager;
019    import com.liferay.portal.kernel.test.randomizerbumpers.NumericStringRandomizerBumper;
020    import com.liferay.portal.kernel.test.randomizerbumpers.UniqueStringRandomizerBumper;
021    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.GroupConstants;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.GroupServiceUtil;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationParameterMapFactory;
034    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
035    import com.liferay.portlet.exportimport.service.StagingLocalServiceUtil;
036    
037    import java.util.Collection;
038    import java.util.HashMap;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    /**
043     * @author Manuel de la Pe??a
044     */
045    public class GroupTestUtil {
046    
047            public static Group addGroup() throws Exception {
048                    return addGroup(GroupConstants.DEFAULT_PARENT_GROUP_ID);
049            }
050    
051            public static Group addGroup(long parentGroupId) throws Exception {
052                    return addGroup(
053                            TestPropsValues.getCompanyId(), TestPropsValues.getUserId(),
054                            parentGroupId);
055            }
056    
057            public static Group addGroup(long userId, Layout layout) throws Exception {
058                    return addGroup(userId, GroupConstants.DEFAULT_PARENT_GROUP_ID, layout);
059            }
060    
061            public static Group addGroup(long userId, long parentGroupId, Layout layout)
062                    throws Exception {
063    
064                    Group scopeGroup = layout.getScopeGroup();
065    
066                    if (scopeGroup != null) {
067                            return scopeGroup;
068                    }
069    
070                    Map<Locale, String> nameMap = new HashMap<>();
071    
072                    nameMap.put(LocaleUtil.getDefault(), String.valueOf(layout.getPlid()));
073    
074                    return GroupLocalServiceUtil.addGroup(
075                            userId, parentGroupId, Layout.class.getName(), layout.getPlid(),
076                            GroupConstants.DEFAULT_LIVE_GROUP_ID, nameMap, null, 0, true,
077                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, false, true,
078                            null);
079            }
080    
081            public static Group addGroup(
082                            long companyId, long userId, long parentGroupId)
083                    throws Exception {
084    
085                    String name = RandomTestUtil.randomString(
086                            NumericStringRandomizerBumper.INSTANCE,
087                            UniqueStringRandomizerBumper.INSTANCE);
088    
089                    Group group = GroupLocalServiceUtil.fetchGroup(companyId, name);
090    
091                    if (group != null) {
092                            return group;
093                    }
094    
095                    Map<Locale, String> nameMap = new HashMap<>();
096    
097                    nameMap.put(LocaleUtil.getDefault(), name);
098    
099                    Map<Locale, String> descriptionMap = new HashMap<>();
100    
101                    descriptionMap.put(
102                            LocaleUtil.getDefault(), RandomTestUtil.randomString());
103    
104                    int type = GroupConstants.TYPE_SITE_OPEN;
105                    String friendlyURL =
106                            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
107                    boolean site = true;
108                    boolean active = true;
109                    boolean manualMembership = true;
110                    int membershipRestriction =
111                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION;
112    
113                    return GroupLocalServiceUtil.addGroup(
114                            userId, parentGroupId, null, 0,
115                            GroupConstants.DEFAULT_LIVE_GROUP_ID, nameMap, descriptionMap, type,
116                            manualMembership, membershipRestriction, friendlyURL, site, active,
117                            ServiceContextTestUtil.getServiceContext());
118            }
119    
120            public static Group addGroup(
121                            long parentGroupId, ServiceContext serviceContext)
122                    throws Exception {
123    
124                    String name = RandomTestUtil.randomString(
125                            NumericStringRandomizerBumper.INSTANCE,
126                            UniqueStringRandomizerBumper.INSTANCE);
127    
128                    Group group = GroupLocalServiceUtil.fetchGroup(
129                            TestPropsValues.getCompanyId(), name);
130    
131                    if (group != null) {
132                            return group;
133                    }
134    
135                    Map<Locale, String> nameMap = new HashMap<>();
136    
137                    nameMap.put(LocaleUtil.getDefault(), name);
138    
139                    Map<Locale, String> descriptionMap = new HashMap<>();
140    
141                    descriptionMap.put(
142                            LocaleUtil.getDefault(), RandomTestUtil.randomString());
143    
144                    int type = GroupConstants.TYPE_SITE_OPEN;
145                    String friendlyURL =
146                            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
147                    boolean site = true;
148                    boolean active = true;
149                    boolean manualMembership = true;
150                    int membershipRestriction =
151                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION;
152    
153                    if (serviceContext == null) {
154                            serviceContext = ServiceContextTestUtil.getServiceContext();
155                    }
156    
157                    return GroupServiceUtil.addGroup(
158                            parentGroupId, GroupConstants.DEFAULT_LIVE_GROUP_ID, nameMap,
159                            descriptionMap, type, manualMembership, membershipRestriction,
160                            friendlyURL, site, active, serviceContext);
161            }
162    
163            public static void enableLocalStaging(Group group) throws Exception {
164                    ServiceContext serviceContext =
165                            ServiceContextTestUtil.getServiceContext();
166    
167                    serviceContext.setAddGroupPermissions(true);
168                    serviceContext.setAddGuestPermissions(true);
169                    serviceContext.setScopeGroupId(group.getGroupId());
170    
171                    Map<String, String[]> parameters =
172                            ExportImportConfigurationParameterMapFactory.buildParameterMap();
173    
174                    parameters.put(
175                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
176                            new String[] {Boolean.FALSE.toString()});
177                    parameters.put(
178                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
179                            new String[] {Boolean.FALSE.toString()});
180    
181                    for (String parameterName : parameters.keySet()) {
182                            serviceContext.setAttribute(
183                                    parameterName, parameters.get(parameterName)[0]);
184                    }
185    
186                    StagingLocalServiceUtil.enableLocalStaging(
187                            TestPropsValues.getUserId(), group, false, false, serviceContext);
188            }
189    
190            public static Group updateDisplaySettings(
191                            long groupId, Collection<Locale> availableLocales,
192                            Locale defaultLocale)
193                    throws Exception {
194    
195                    UnicodeProperties typeSettingsProperties = new UnicodeProperties();
196    
197                    boolean inheritLocales = false;
198    
199                    if ((availableLocales == null) && (defaultLocale == null)) {
200                            inheritLocales = true;
201                    }
202    
203                    typeSettingsProperties.put(
204                            GroupConstants.TYPE_SETTINGS_KEY_INHERIT_LOCALES,
205                            String.valueOf(inheritLocales));
206    
207                    if (availableLocales != null) {
208                            typeSettingsProperties.put(
209                                    PropsKeys.LOCALES,
210                                    StringUtil.merge(LocaleUtil.toLanguageIds(availableLocales)));
211                    }
212    
213                    if (defaultLocale != null) {
214                            typeSettingsProperties.put(
215                                    "languageId", LocaleUtil.toLanguageId(defaultLocale));
216                    }
217    
218                    Group group = GroupLocalServiceUtil.updateGroup(
219                            groupId, typeSettingsProperties.toString());
220    
221                    ThreadLocalCacheManager.clearAll(Lifecycle.REQUEST);
222    
223                    return group;
224            }
225    
226    }