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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Organization;
30  import com.liferay.portal.model.Role;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.model.UserGroup;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.service.OrganizationLocalServiceUtil;
35  import com.liferay.portal.service.RoleLocalServiceUtil;
36  import com.liferay.portal.service.UserGroupLocalServiceUtil;
37  import com.liferay.portal.util.PortalUtil;
38  
39  import java.util.ArrayList;
40  import java.util.LinkedHashMap;
41  import java.util.List;
42  
43  /**
44   * <a href="AnnouncementsUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Raymond Augé
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, true);
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, true);
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                 roles.addAll(
103                     RoleLocalServiceUtil.getUserGroupGroupRoles(
104                         userId, group.getGroupId()));
105             }
106 
107             if (roles.size() > 0) {
108                 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
109             }
110         }
111 
112         // User group announcements
113 
114         List<UserGroup> userGroups =
115             UserGroupLocalServiceUtil.getUserUserGroups(userId);
116 
117         if (userGroups.size() > 0) {
118             scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
119         }
120 
121         return scopes;
122     }
123 
124     private static long[] _getGroupIds(List<Group> groups) {
125         long[] groupIds = new long[groups.size()];
126 
127         int i = 0;
128 
129         for (Group group : groups) {
130             groupIds[i++] = group.getGroupId();
131         }
132 
133         return groupIds;
134     }
135 
136     private static long[] _getOrganizationIds(
137         List<Organization> organizations) {
138 
139         long[] organizationIds = new long[organizations.size()];
140 
141         int i = 0;
142 
143         for (Organization organization : organizations) {
144             organizationIds[i++] = organization.getOrganizationId();
145         }
146 
147         return organizationIds;
148     }
149 
150     private static long[] _getRoleIds(List<Role> roles) {
151         long[] roleIds = new long[roles.size()];
152 
153         int i = 0;
154 
155         for (Role role : roles) {
156             roleIds[i++] = role.getRoleId();
157         }
158 
159         return roleIds;
160     }
161 
162     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
163         long[] userGroupIds = new long[userGroups.size()];
164 
165         int i = 0;
166 
167         for (UserGroup userGroup : userGroups) {
168             userGroupIds[i++] = userGroup.getUserGroupId();
169         }
170 
171         return userGroupIds;
172     }
173 
174     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
175         Group.class.getName());
176 
177     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
178         Organization.class.getName());
179 
180     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
181         Role.class.getName());
182 
183     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
184         User.class.getName());
185 
186     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
187         UserGroup.class.getName());
188 
189 }