1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.DuplicateUserGroupException;
18  import com.liferay.portal.NoSuchUserGroupException;
19  import com.liferay.portal.PortalException;
20  import com.liferay.portal.RequiredUserGroupException;
21  import com.liferay.portal.SystemException;
22  import com.liferay.portal.UserGroupNameException;
23  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.lar.PortletDataHandlerKeys;
28  import com.liferay.portal.lar.UserIdStrategy;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.ResourceConstants;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.model.UserGroup;
33  import com.liferay.portal.model.impl.UserGroupImpl;
34  import com.liferay.portal.security.permission.PermissionCacheUtil;
35  import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
36  
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  /**
42   * <a href="UserGroupLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Charles May
45   */
46  public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
47  
48      public void addGroupUserGroups(long groupId, long[] userGroupIds)
49          throws SystemException {
50  
51          groupPersistence.addUserGroups(groupId, userGroupIds);
52  
53          PermissionCacheUtil.clearCache();
54      }
55  
56      public UserGroup addUserGroup(
57              long userId, long companyId, String name, String description)
58          throws PortalException, SystemException {
59  
60          // User Group
61  
62          validate(0, companyId, name);
63  
64          long userGroupId = counterLocalService.increment();
65  
66          UserGroup userGroup = userGroupPersistence.create(userGroupId);
67  
68          userGroup.setCompanyId(companyId);
69          userGroup.setParentUserGroupId(
70              UserGroupImpl.DEFAULT_PARENT_USER_GROUP_ID);
71          userGroup.setName(name);
72          userGroup.setDescription(description);
73  
74          userGroupPersistence.update(userGroup, false);
75  
76          // Group
77  
78          groupLocalService.addGroup(
79              userId, UserGroup.class.getName(), userGroup.getUserGroupId(), null,
80              null, 0, null, true);
81  
82          // Resources
83  
84          resourceLocalService.addResources(
85              companyId, 0, userId, UserGroup.class.getName(),
86              userGroup.getUserGroupId(), false, false, false);
87  
88          return userGroup;
89      }
90  
91      public void clearUserUserGroups(long userId) throws SystemException {
92          userPersistence.clearUserGroups(userId);
93  
94          PermissionCacheUtil.clearCache();
95      }
96  
97      public void copyUserGroupLayouts(long userGroupId, long userIds[])
98          throws PortalException, SystemException {
99  
100         for (long userId : userIds) {
101             if (!userGroupPersistence.containsUser(userGroupId, userId)) {
102                 copyUserGroupLayouts(userGroupId, userId);
103             }
104         }
105     }
106 
107     public void copyUserGroupLayouts(long userGroupIds[], long userId)
108         throws PortalException, SystemException {
109 
110         for (long userGroupId : userGroupIds) {
111             if (!userGroupPersistence.containsUser(userGroupId, userId)) {
112                 copyUserGroupLayouts(userGroupId, userId);
113             }
114         }
115     }
116 
117     public void copyUserGroupLayouts(long userGroupId, long userId)
118         throws PortalException, SystemException {
119 
120         UserGroup userGroup = userGroupLocalService.getUserGroup(userGroupId);
121         User user = userPersistence.findByPrimaryKey(userId);
122 
123         Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
124 
125         if (userGroup.hasPrivateLayouts()) {
126             long sourceGroupId = userGroup.getGroup().getGroupId();
127             long targetGroupId = user.getGroup().getGroupId();
128 
129             byte[] bytes = layoutLocalService.exportLayouts(
130                 sourceGroupId, true, parameterMap, null, null);
131 
132             UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
133                 bytes);
134 
135             layoutLocalService.importLayouts(
136                 userId, targetGroupId, true, parameterMap, ubais);
137         }
138 
139         if (userGroup.hasPublicLayouts()) {
140             long sourceGroupId = userGroup.getGroup().getGroupId();
141             long targetGroupId = user.getGroup().getGroupId();
142 
143             byte[] bytes = layoutLocalService.exportLayouts(
144                 sourceGroupId, false, parameterMap, null, null);
145 
146             UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
147                 bytes);
148 
149             layoutLocalService.importLayouts(
150                 userId, targetGroupId, false, parameterMap, ubais);
151         }
152     }
153 
154     public void deleteUserGroup(long userGroupId)
155         throws PortalException, SystemException {
156 
157         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
158             userGroupId);
159 
160         if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
161             throw new RequiredUserGroupException();
162         }
163 
164         // Users
165 
166         clearUserUserGroups(userGroupId);
167 
168         // Group
169 
170         Group group = userGroup.getGroup();
171 
172         groupLocalService.deleteGroup(group.getGroupId());
173 
174         // Resources
175 
176         resourceLocalService.deleteResource(
177             userGroup.getCompanyId(), UserGroup.class.getName(),
178             ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
179 
180         // User Group
181 
182         userGroupPersistence.remove(userGroupId);
183 
184         // Permission cache
185 
186         PermissionCacheUtil.clearCache();
187     }
188 
189     public UserGroup getUserGroup(long userGroupId)
190         throws PortalException, SystemException {
191 
192         return userGroupPersistence.findByPrimaryKey(userGroupId);
193     }
194 
195     public UserGroup getUserGroup(long companyId, String name)
196         throws PortalException, SystemException {
197 
198         return userGroupPersistence.findByC_N(companyId, name);
199     }
200 
201     public List<UserGroup> getUserGroups(long companyId)
202         throws SystemException {
203 
204         return userGroupPersistence.findByCompanyId(companyId);
205     }
206 
207     public List<UserGroup> getUserUserGroups(long userId)
208         throws SystemException {
209 
210         return userPersistence.getUserGroups(userId);
211     }
212 
213     public boolean hasGroupUserGroup(long groupId, long userGroupId)
214         throws SystemException {
215 
216         return groupPersistence.containsUserGroup(groupId, userGroupId);
217     }
218 
219     public List<UserGroup> search(
220             long companyId, String name, String description,
221             LinkedHashMap<String, Object> params, int start, int end,
222             OrderByComparator obc)
223         throws SystemException {
224 
225         return userGroupFinder.findByC_N_D(
226             companyId, name, description, params, start, end, obc);
227     }
228 
229     public int searchCount(
230             long companyId, String name, String description,
231             LinkedHashMap<String, Object> params)
232         throws SystemException {
233 
234         return userGroupFinder.countByC_N_D(
235             companyId, name, description, params);
236     }
237 
238     public void setUserUserGroups(long userId, long[] userGroupIds)
239         throws PortalException, SystemException {
240 
241         copyUserGroupLayouts(userGroupIds, userId);
242 
243         userPersistence.setUserGroups(userId, userGroupIds);
244 
245         PermissionCacheUtil.clearCache();
246     }
247 
248     public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
249         throws SystemException {
250 
251         groupPersistence.removeUserGroups(groupId, userGroupIds);
252 
253         PermissionCacheUtil.clearCache();
254     }
255 
256     public UserGroup updateUserGroup(
257             long companyId, long userGroupId, String name,
258             String description)
259         throws PortalException, SystemException {
260 
261         validate(userGroupId, companyId, name);
262 
263         UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
264             userGroupId);
265 
266         userGroup.setName(name);
267         userGroup.setDescription(description);
268 
269         userGroupPersistence.update(userGroup, false);
270 
271         return userGroup;
272     }
273 
274     protected Map<String, String[]> getLayoutTemplatesParameters() {
275         Map<String, String[]> parameterMap =
276             new LinkedHashMap<String, String[]>();
277 
278         parameterMap.put(
279             PortletDataHandlerKeys.DATA_STRATEGY,
280             new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
281         parameterMap.put(
282             PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
283             new String[] {Boolean.FALSE.toString()});
284         parameterMap.put(
285             PortletDataHandlerKeys.DELETE_PORTLET_DATA,
286             new String[] {Boolean.FALSE.toString()});
287         parameterMap.put(
288             PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
289             new String[] {PortletDataHandlerKeys.
290                 LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME});
291         parameterMap.put(
292             PortletDataHandlerKeys.PERMISSIONS,
293             new String[] {Boolean.TRUE.toString()});
294         parameterMap.put(
295             PortletDataHandlerKeys.PORTLET_DATA,
296             new String[] {Boolean.TRUE.toString()});
297         parameterMap.put(
298             PortletDataHandlerKeys.PORTLET_DATA_ALL,
299             new String[] {Boolean.TRUE.toString()});
300         parameterMap.put(
301             PortletDataHandlerKeys.PORTLET_SETUP,
302             new String[] {Boolean.TRUE.toString()});
303         parameterMap.put(
304             PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
305             new String[] {Boolean.TRUE.toString()});
306         parameterMap.put(
307             PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
308             new String[] {PortletDataHandlerKeys.
309                 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
310         parameterMap.put(
311             PortletDataHandlerKeys.THEME,
312             new String[] {Boolean.FALSE.toString()});
313         parameterMap.put(
314             PortletDataHandlerKeys.USER_ID_STRATEGY,
315             new String[] {UserIdStrategy.CURRENT_USER_ID});
316         parameterMap.put(
317             PortletDataHandlerKeys.USER_PERMISSIONS,
318             new String[] {Boolean.FALSE.toString()});
319 
320         return parameterMap;
321     }
322 
323     protected void validate(long userGroupId, long companyId, String name)
324         throws PortalException, SystemException {
325 
326         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
327             (name.indexOf(StringPool.COMMA) != -1) ||
328             (name.indexOf(StringPool.STAR) != -1)) {
329 
330             throw new UserGroupNameException();
331         }
332 
333         try {
334             UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
335 
336             if (userGroup.getUserGroupId() != userGroupId) {
337                 throw new DuplicateUserGroupException();
338             }
339         }
340         catch (NoSuchUserGroupException nsuge) {
341         }
342     }
343 
344 }