1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.announcements.util;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.model.Role;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.UserGroup;
32  import com.liferay.portal.service.GroupLocalServiceUtil;
33  import com.liferay.portal.service.OrganizationLocalServiceUtil;
34  import com.liferay.portal.service.RoleLocalServiceUtil;
35  import com.liferay.portal.service.UserGroupLocalServiceUtil;
36  import com.liferay.portal.util.PortalUtil;
37  
38  import java.util.ArrayList;
39  import java.util.LinkedHashMap;
40  import java.util.List;
41  
42  /**
43   * <a href="AnnouncementsUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Raymond Augé
46   *
47   */
48  public class AnnouncementsUtil {
49  
50      public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
51          throws PortalException, SystemException {
52  
53          LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
54  
55          // General announcements
56  
57          scopes.put(new Long(0), new long[] {0});
58  
59          // Personal announcements
60  
61          scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
62  
63          // Community announcements
64  
65          List<Group> groupsList = new ArrayList<Group>();
66  
67          List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId);
68  
69          if (groups.size() > 0) {
70              scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
71  
72              groupsList.addAll(groups);
73          }
74  
75          // Organization announcements
76  
77          List<Organization> organizations =
78              OrganizationLocalServiceUtil.getUserOrganizations(userId);
79  
80          if (organizations.size() > 0) {
81              scopes.put(
82                  _ORGANIZATION_CLASS_NAME_ID,
83                  _getOrganizationIds(organizations));
84  
85              for (Organization organization : organizations) {
86                  groupsList.add(organization.getGroup());
87              }
88          }
89  
90          // Role announcements
91  
92          if (groupsList.size() > 0) {
93              List<Role> roles = RoleLocalServiceUtil.getUserRelatedRoles(
94                  userId, groupsList);
95  
96              for (Group group : groupsList) {
97                  roles.addAll(
98                      RoleLocalServiceUtil.getUserGroupRoles(
99                          userId, group.getGroupId()));
100             }
101 
102             if (roles.size() > 0) {
103                 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
104             }
105         }
106 
107         // User group announcements
108 
109         List<UserGroup> userGroups =
110             UserGroupLocalServiceUtil.getUserUserGroups(userId);
111 
112         if (userGroups.size() > 0) {
113             scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
114         }
115 
116         return scopes;
117     }
118 
119     private static long[] _getGroupIds(List<Group> groups) {
120         long[] groupIds = new long[groups.size()];
121 
122         int i = 0;
123 
124         for (Group group : groups) {
125             groupIds[i++] = group.getGroupId();
126         }
127 
128         return groupIds;
129     }
130 
131     private static long[] _getOrganizationIds(
132         List<Organization> organizations) {
133 
134         long[] organizationIds = new long[organizations.size()];
135 
136         int i = 0;
137 
138         for (Organization organization : organizations) {
139             organizationIds[i++] = organization.getOrganizationId();
140         }
141 
142         return organizationIds;
143     }
144 
145     private static long[] _getRoleIds(List<Role> roles) {
146         long[] roleIds = new long[roles.size()];
147 
148         int i = 0;
149 
150         for (Role role : roles) {
151             roleIds[i++] = role.getRoleId();
152         }
153 
154         return roleIds;
155     }
156 
157     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
158         long[] userGroupIds = new long[userGroups.size()];
159 
160         int i = 0;
161 
162         for (UserGroup userGroup : userGroups) {
163             userGroupIds[i++] = userGroup.getUserGroupId();
164         }
165 
166         return userGroupIds;
167     }
168 
169     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
170         Group.class.getName());
171 
172     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
173         Organization.class.getName());
174 
175     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
176         Role.class.getName());
177 
178     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
179         User.class.getName());
180 
181     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
182         UserGroup.class.getName());
183 
184 }