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