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.size() > 0) {
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.size() > 0) {
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.size() > 0) {
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          if (groupsList.size() > 0) {
98              List<Role> roles = RoleLocalServiceUtil.getUserRelatedRoles(
99                  userId, groupsList);
100 
101             roles = ListUtil.copy(roles);
102 
103             for (Group group : groupsList) {
104                 roles.addAll(
105                     RoleLocalServiceUtil.getUserGroupRoles(
106                         userId, group.getGroupId()));
107                 roles.addAll(
108                     RoleLocalServiceUtil.getUserGroupGroupRoles(
109                         userId, group.getGroupId()));
110             }
111 
112             if (roles.size() > 0) {
113                 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
114             }
115         }
116 
117         return scopes;
118     }
119 
120     private static long[] _getGroupIds(List<Group> groups) {
121         long[] groupIds = new long[groups.size()];
122 
123         int i = 0;
124 
125         for (Group group : groups) {
126             groupIds[i++] = group.getGroupId();
127         }
128 
129         return groupIds;
130     }
131 
132     private static long[] _getOrganizationIds(
133         List<Organization> organizations) {
134 
135         long[] organizationIds = new long[organizations.size()];
136 
137         int i = 0;
138 
139         for (Organization organization : organizations) {
140             organizationIds[i++] = organization.getOrganizationId();
141         }
142 
143         return organizationIds;
144     }
145 
146     private static long[] _getRoleIds(List<Role> roles) {
147         long[] roleIds = new long[roles.size()];
148 
149         int i = 0;
150 
151         for (Role role : roles) {
152             roleIds[i++] = role.getRoleId();
153         }
154 
155         return roleIds;
156     }
157 
158     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
159         long[] userGroupIds = new long[userGroups.size()];
160 
161         int i = 0;
162 
163         for (UserGroup userGroup : userGroups) {
164             userGroupIds[i++] = userGroup.getUserGroupId();
165         }
166 
167         return userGroupIds;
168     }
169 
170     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
171         Group.class.getName());
172 
173     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
174         Organization.class.getName());
175 
176     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
177         Role.class.getName());
178 
179     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
180         User.class.getName());
181 
182     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
183         UserGroup.class.getName());
184 
185 }