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