1
22
23 package com.liferay.portlet.announcements.util;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.ListUtil;
27 import com.liferay.portal.model.Group;
28 import com.liferay.portal.model.Organization;
29 import com.liferay.portal.model.Role;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.model.UserGroup;
32 import com.liferay.portal.service.GroupLocalServiceUtil;
33 import com.liferay.portal.service.OrganizationLocalServiceUtil;
34 import com.liferay.portal.service.RoleLocalServiceUtil;
35 import com.liferay.portal.service.UserGroupLocalServiceUtil;
36 import com.liferay.portal.util.PortalUtil;
37
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41
42
48 public class AnnouncementsUtil {
49
50 public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
51 throws SystemException {
52
53 LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
54
55
57 scopes.put(new Long(0), new long[] {0});
58
59
61 scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
62
63
65 List<Group> groupsList = new ArrayList<Group>();
66
67 List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId);
68
69 if (groups.size() > 0) {
70 scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
71
72 groupsList.addAll(groups);
73 }
74
75
77 List<Organization> organizations =
78 OrganizationLocalServiceUtil.getUserOrganizations(userId);
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
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 }
103
104 if (roles.size() > 0) {
105 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
106 }
107 }
108
109
111 List<UserGroup> userGroups =
112 UserGroupLocalServiceUtil.getUserUserGroups(userId);
113
114 if (userGroups.size() > 0) {
115 scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
116 }
117
118 return scopes;
119 }
120
121 private static long[] _getGroupIds(List<Group> groups) {
122 long[] groupIds = new long[groups.size()];
123
124 int i = 0;
125
126 for (Group group : groups) {
127 groupIds[i++] = group.getGroupId();
128 }
129
130 return groupIds;
131 }
132
133 private static long[] _getOrganizationIds(
134 List<Organization> organizations) {
135
136 long[] organizationIds = new long[organizations.size()];
137
138 int i = 0;
139
140 for (Organization organization : organizations) {
141 organizationIds[i++] = organization.getOrganizationId();
142 }
143
144 return organizationIds;
145 }
146
147 private static long[] _getRoleIds(List<Role> roles) {
148 long[] roleIds = new long[roles.size()];
149
150 int i = 0;
151
152 for (Role role : roles) {
153 roleIds[i++] = role.getRoleId();
154 }
155
156 return roleIds;
157 }
158
159 private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
160 long[] userGroupIds = new long[userGroups.size()];
161
162 int i = 0;
163
164 for (UserGroup userGroup : userGroups) {
165 userGroupIds[i++] = userGroup.getUserGroupId();
166 }
167
168 return userGroupIds;
169 }
170
171 private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
172 Group.class.getName());
173
174 private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
175 Organization.class.getName());
176
177 private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
178 Role.class.getName());
179
180 private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
181 User.class.getName());
182
183 private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
184 UserGroup.class.getName());
185
186 }