1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.announcements.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.ListUtil;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.Organization;
22  import com.liferay.portal.model.Role;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.model.UserGroup;
25  import com.liferay.portal.service.GroupLocalServiceUtil;
26  import com.liferay.portal.service.OrganizationLocalServiceUtil;
27  import com.liferay.portal.service.RoleLocalServiceUtil;
28  import com.liferay.portal.service.UserGroupLocalServiceUtil;
29  import com.liferay.portal.util.PortalUtil;
30  
31  import java.util.ArrayList;
32  import java.util.LinkedHashMap;
33  import java.util.List;
34  
35  /**
36   * <a href="AnnouncementsUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Raymond Augé
39   */
40  public class AnnouncementsUtil {
41  
42      public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
43          throws PortalException, SystemException {
44  
45          LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
46  
47          // General announcements
48  
49          scopes.put(new Long(0), new long[] {0});
50  
51          // Personal announcements
52  
53          scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
54  
55          // Community announcements
56  
57          List<Group> groupsList = new ArrayList<Group>();
58  
59          List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
60  
61          if (!groups.isEmpty()) {
62              scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
63  
64              groupsList.addAll(groups);
65          }
66  
67          // Organization announcements
68  
69          List<Organization> organizations =
70              OrganizationLocalServiceUtil.getUserOrganizations(userId, true);
71  
72          if (!organizations.isEmpty()) {
73              scopes.put(
74                  _ORGANIZATION_CLASS_NAME_ID,
75                  _getOrganizationIds(organizations));
76  
77              for (Organization organization : organizations) {
78                  groupsList.add(organization.getGroup());
79              }
80          }
81  
82          // User group announcements
83  
84          List<UserGroup> userGroups =
85              UserGroupLocalServiceUtil.getUserUserGroups(userId);
86  
87          if (!userGroups.isEmpty()) {
88              scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
89  
90              for (UserGroup userGroup : userGroups) {
91                  groupsList.add(userGroup.getGroup());
92              }
93          }
94  
95          // Role announcements
96  
97          List<Role> roles = new ArrayList<Role>();
98  
99          if (!groupsList.isEmpty()) {
100             roles = RoleLocalServiceUtil.getUserRelatedRoles(
101                 userId, groupsList);
102 
103             roles = ListUtil.copy(roles);
104 
105             for (Group group : groupsList) {
106                 roles.addAll(
107                     RoleLocalServiceUtil.getUserGroupRoles(
108                         userId, group.getGroupId()));
109                 roles.addAll(
110                     RoleLocalServiceUtil.getUserGroupGroupRoles(
111                         userId, group.getGroupId()));
112             }
113         }
114         else {
115             roles = RoleLocalServiceUtil.getUserRoles(userId);
116         }
117 
118         if (roles.size() > 0) {
119             scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
120         }
121 
122         return scopes;
123     }
124 
125     private static long[] _getGroupIds(List<Group> groups) {
126         long[] groupIds = new long[groups.size()];
127 
128         int i = 0;
129 
130         for (Group group : groups) {
131             groupIds[i++] = group.getGroupId();
132         }
133 
134         return groupIds;
135     }
136 
137     private static long[] _getOrganizationIds(
138         List<Organization> organizations) {
139 
140         long[] organizationIds = new long[organizations.size()];
141 
142         int i = 0;
143 
144         for (Organization organization : organizations) {
145             organizationIds[i++] = organization.getOrganizationId();
146         }
147 
148         return organizationIds;
149     }
150 
151     private static long[] _getRoleIds(List<Role> roles) {
152         long[] roleIds = new long[roles.size()];
153 
154         int i = 0;
155 
156         for (Role role : roles) {
157             roleIds[i++] = role.getRoleId();
158         }
159 
160         return roleIds;
161     }
162 
163     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
164         long[] userGroupIds = new long[userGroups.size()];
165 
166         int i = 0;
167 
168         for (UserGroup userGroup : userGroups) {
169             userGroupIds[i++] = userGroup.getUserGroupId();
170         }
171 
172         return userGroupIds;
173     }
174 
175     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
176         Group.class.getName());
177 
178     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
179         Organization.class.getName());
180 
181     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
182         Role.class.getName());
183 
184     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
185         User.class.getName());
186 
187     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
188         UserGroup.class.getName());
189 
190 }