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