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.SystemException;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.model.Organization;
28  import com.liferay.portal.model.Role;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.UserGroup;
31  import com.liferay.portal.service.GroupLocalServiceUtil;
32  import com.liferay.portal.service.OrganizationLocalServiceUtil;
33  import com.liferay.portal.service.RoleLocalServiceUtil;
34  import com.liferay.portal.service.UserGroupLocalServiceUtil;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import java.util.ArrayList;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  
41  /**
42   * <a href="AnnouncementsUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Raymond Augé
45   *
46   */
47  public class AnnouncementsUtil {
48  
49      public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
50          throws SystemException {
51  
52          LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
53  
54          // General announcements
55  
56          scopes.put(new Long(0), new long[] {0});
57  
58          // Personal announcements
59  
60          scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
61  
62          // Community announcements
63  
64          List<Group> groupsList = new ArrayList<Group>();
65  
66          List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId);
67  
68          if (groups.size() > 0) {
69              scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
70  
71              groupsList.addAll(groups);
72          }
73  
74          // Organization announcements
75  
76          List<Organization> organizations =
77              OrganizationLocalServiceUtil.getUserOrganizations(userId);
78  
79          if (organizations.size() > 0) {
80              scopes.put(
81                  _ORGANIZATION_CLASS_NAME_ID,
82                  _getOrganizationIds(organizations));
83  
84              for (Organization organization : organizations) {
85                  groupsList.add(organization.getGroup());
86              }
87          }
88  
89          // Role announcements
90  
91          if (groupsList.size() > 0) {
92              List<Role> roles = RoleLocalServiceUtil.getUserRelatedRoles(
93                  userId, groupsList);
94  
95              for (Group group : groupsList) {
96                  roles.addAll(
97                      RoleLocalServiceUtil.getUserGroupRoles(
98                          userId, group.getGroupId()));
99              }
100 
101             if (roles.size() > 0) {
102                 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
103             }
104         }
105 
106         // User group announcements
107 
108         List<UserGroup> userGroups =
109             UserGroupLocalServiceUtil.getUserUserGroups(userId);
110 
111         if (userGroups.size() > 0) {
112             scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
113         }
114 
115         return scopes;
116     }
117 
118     private static long[] _getGroupIds(List<Group> groups) {
119         long[] groupIds = new long[groups.size()];
120 
121         int i = 0;
122 
123         for (Group group : groups) {
124             groupIds[i++] = group.getGroupId();
125         }
126 
127         return groupIds;
128     }
129 
130     private static long[] _getOrganizationIds(
131         List<Organization> organizations) {
132 
133         long[] organizationIds = new long[organizations.size()];
134 
135         int i = 0;
136 
137         for (Organization organization : organizations) {
138             organizationIds[i++] = organization.getOrganizationId();
139         }
140 
141         return organizationIds;
142     }
143 
144     private static long[] _getRoleIds(List<Role> roles) {
145         long[] roleIds = new long[roles.size()];
146 
147         int i = 0;
148 
149         for (Role role : roles) {
150             roleIds[i++] = role.getRoleId();
151         }
152 
153         return roleIds;
154     }
155 
156     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
157         long[] userGroupIds = new long[userGroups.size()];
158 
159         int i = 0;
160 
161         for (UserGroup userGroup : userGroups) {
162             userGroupIds[i++] = userGroup.getUserGroupId();
163         }
164 
165         return userGroupIds;
166     }
167 
168     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
169         Group.class.getName());
170 
171     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
172         Organization.class.getName());
173 
174     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
175         Role.class.getName());
176 
177     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
178         User.class.getName());
179 
180     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
181         UserGroup.class.getName());
182 
183 }