1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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   */
49  public class AnnouncementsUtil {
50  
51      public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
52          throws PortalException, SystemException {
53  
54          LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
55  
56          // General announcements
57  
58          scopes.put(new Long(0), new long[] {0});
59  
60          // Personal announcements
61  
62          scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
63  
64          // Community announcements
65  
66          List<Group> groupsList = new ArrayList<Group>();
67  
68          List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId);
69  
70          if (groups.size() > 0) {
71              scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
72  
73              groupsList.addAll(groups);
74          }
75  
76          // Organization announcements
77  
78          List<Organization> organizations =
79              OrganizationLocalServiceUtil.getUserOrganizations(userId);
80  
81          if (organizations.size() > 0) {
82              scopes.put(
83                  _ORGANIZATION_CLASS_NAME_ID,
84                  _getOrganizationIds(organizations));
85  
86              for (Organization organization : organizations) {
87                  groupsList.add(organization.getGroup());
88              }
89          }
90  
91          // Role announcements
92  
93          if (groupsList.size() > 0) {
94              List<Role> roles = RoleLocalServiceUtil.getUserRelatedRoles(
95                  userId, groupsList);
96  
97              roles = ListUtil.copy(roles);
98  
99              for (Group group : groupsList) {
100                 roles.addAll(
101                     RoleLocalServiceUtil.getUserGroupRoles(
102                         userId, group.getGroupId()));
103             }
104 
105             if (roles.size() > 0) {
106                 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
107             }
108         }
109 
110         // User group announcements
111 
112         List<UserGroup> userGroups =
113             UserGroupLocalServiceUtil.getUserUserGroups(userId);
114 
115         if (userGroups.size() > 0) {
116             scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
117         }
118 
119         return scopes;
120     }
121 
122     private static long[] _getGroupIds(List<Group> groups) {
123         long[] groupIds = new long[groups.size()];
124 
125         int i = 0;
126 
127         for (Group group : groups) {
128             groupIds[i++] = group.getGroupId();
129         }
130 
131         return groupIds;
132     }
133 
134     private static long[] _getOrganizationIds(
135         List<Organization> organizations) {
136 
137         long[] organizationIds = new long[organizations.size()];
138 
139         int i = 0;
140 
141         for (Organization organization : organizations) {
142             organizationIds[i++] = organization.getOrganizationId();
143         }
144 
145         return organizationIds;
146     }
147 
148     private static long[] _getRoleIds(List<Role> roles) {
149         long[] roleIds = new long[roles.size()];
150 
151         int i = 0;
152 
153         for (Role role : roles) {
154             roleIds[i++] = role.getRoleId();
155         }
156 
157         return roleIds;
158     }
159 
160     private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
161         long[] userGroupIds = new long[userGroups.size()];
162 
163         int i = 0;
164 
165         for (UserGroup userGroup : userGroups) {
166             userGroupIds[i++] = userGroup.getUserGroupId();
167         }
168 
169         return userGroupIds;
170     }
171 
172     private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
173         Group.class.getName());
174 
175     private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
176         Organization.class.getName());
177 
178     private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
179         Role.class.getName());
180 
181     private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
182         User.class.getName());
183 
184     private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
185         UserGroup.class.getName());
186 
187 }