1
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
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
49 scopes.put(new Long(0), new long[] {0});
50
51
53 scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
54
55
57 List<Group> groupsList = new ArrayList<Group>();
58
59 List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
60
61 if (!groups.isEmpty()) {
62 scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
63
64 groupsList.addAll(groups);
65 }
66
67
69 List<Organization> organizations =
70 OrganizationLocalServiceUtil.getUserOrganizations(userId, true);
71
72 if (!organizations.isEmpty()) {
73 List<Organization> organizationsList =
74 new ArrayList<Organization>();
75
76 organizationsList.addAll(organizations);
77
78 for (Organization organization : organizations) {
79 groupsList.add(organization.getGroup());
80
81 List<Organization> parentOrganizations =
82 OrganizationLocalServiceUtil.getParentOrganizations(
83 organization.getOrganizationId());
84
85 for (Organization parentOrganization : parentOrganizations) {
86 organizationsList.add(parentOrganization);
87 groupsList.add(parentOrganization.getGroup());
88 }
89 }
90
91 scopes.put(
92 _ORGANIZATION_CLASS_NAME_ID,
93 _getOrganizationIds(organizationsList));
94 }
95
96
98 List<UserGroup> userGroups =
99 UserGroupLocalServiceUtil.getUserUserGroups(userId);
100
101 if (!userGroups.isEmpty()) {
102 scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
103
104 for (UserGroup userGroup : userGroups) {
105 groupsList.add(userGroup.getGroup());
106 }
107 }
108
109
111 List<Role> roles = new ArrayList<Role>();
112
113 if (!groupsList.isEmpty()) {
114 roles = RoleLocalServiceUtil.getUserRelatedRoles(
115 userId, groupsList);
116
117 roles = ListUtil.copy(roles);
118
119 for (Group group : groupsList) {
120 roles.addAll(
121 RoleLocalServiceUtil.getUserGroupRoles(
122 userId, group.getGroupId()));
123 roles.addAll(
124 RoleLocalServiceUtil.getUserGroupGroupRoles(
125 userId, group.getGroupId()));
126 }
127 }
128 else {
129 roles = RoleLocalServiceUtil.getUserRoles(userId);
130 }
131
132 if (roles.size() > 0) {
133 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
134 }
135
136 return scopes;
137 }
138
139 private static long[] _getGroupIds(List<Group> groups) {
140 long[] groupIds = new long[groups.size()];
141
142 int i = 0;
143
144 for (Group group : groups) {
145 groupIds[i++] = group.getGroupId();
146 }
147
148 return groupIds;
149 }
150
151 private static long[] _getOrganizationIds(
152 List<Organization> organizations) {
153
154 long[] organizationIds = new long[organizations.size()];
155
156 int i = 0;
157
158 for (Organization organization : organizations) {
159 organizationIds[i++] = organization.getOrganizationId();
160 }
161
162 return organizationIds;
163 }
164
165 private static long[] _getRoleIds(List<Role> roles) {
166 long[] roleIds = new long[roles.size()];
167
168 int i = 0;
169
170 for (Role role : roles) {
171 roleIds[i++] = role.getRoleId();
172 }
173
174 return roleIds;
175 }
176
177 private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
178 long[] userGroupIds = new long[userGroups.size()];
179
180 int i = 0;
181
182 for (UserGroup userGroup : userGroups) {
183 userGroupIds[i++] = userGroup.getUserGroupId();
184 }
185
186 return userGroupIds;
187 }
188
189 private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
190 Group.class.getName());
191
192 private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
193 Organization.class.getName());
194
195 private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
196 Role.class.getName());
197
198 private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
199 User.class.getName());
200
201 private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
202 UserGroup.class.getName());
203
204 }