1
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
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
56 scopes.put(new Long(0), new long[] {0});
57
58
60 scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
61
62
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
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
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
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 }