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