001
014
015 package com.liferay.portlet.announcements.util;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.ListUtil;
020 import com.liferay.portal.model.Group;
021 import com.liferay.portal.model.Organization;
022 import com.liferay.portal.model.Role;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.model.UserGroup;
025 import com.liferay.portal.service.GroupLocalServiceUtil;
026 import com.liferay.portal.service.OrganizationLocalServiceUtil;
027 import com.liferay.portal.service.RoleLocalServiceUtil;
028 import com.liferay.portal.service.UserGroupLocalServiceUtil;
029 import com.liferay.portal.util.PortalUtil;
030
031 import java.util.ArrayList;
032 import java.util.LinkedHashMap;
033 import java.util.List;
034
035
038 public class AnnouncementsUtil {
039
040 public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
041 throws PortalException, SystemException {
042
043 LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
044
045
046
047 scopes.put(new Long(0), new long[] {0});
048
049
050
051 scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
052
053
054
055 List<Group> groupsList = new ArrayList<Group>();
056
057 List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
058
059 if (!groups.isEmpty()) {
060 scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
061
062 groupsList.addAll(groups);
063 }
064
065
066
067 List<Organization> organizations =
068 OrganizationLocalServiceUtil.getUserOrganizations(userId, true);
069
070 if (!organizations.isEmpty()) {
071 List<Organization> organizationsList =
072 new ArrayList<Organization>();
073
074 organizationsList.addAll(organizations);
075
076 for (Organization organization : organizations) {
077 groupsList.add(organization.getGroup());
078
079 List<Organization> parentOrganizations =
080 OrganizationLocalServiceUtil.getParentOrganizations(
081 organization.getOrganizationId());
082
083 for (Organization parentOrganization : parentOrganizations) {
084 organizationsList.add(parentOrganization);
085 groupsList.add(parentOrganization.getGroup());
086 }
087 }
088
089 scopes.put(
090 _ORGANIZATION_CLASS_NAME_ID,
091 _getOrganizationIds(organizationsList));
092 }
093
094
095
096 List<UserGroup> userGroups =
097 UserGroupLocalServiceUtil.getUserUserGroups(userId);
098
099 if (!userGroups.isEmpty()) {
100 scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
101
102 for (UserGroup userGroup : userGroups) {
103 groupsList.add(userGroup.getGroup());
104 }
105 }
106
107
108
109 List<Role> roles = new ArrayList<Role>();
110
111 if (!groupsList.isEmpty()) {
112 roles = RoleLocalServiceUtil.getUserRelatedRoles(
113 userId, groupsList);
114
115 roles = ListUtil.copy(roles);
116
117 for (Group group : groupsList) {
118 roles.addAll(
119 RoleLocalServiceUtil.getUserGroupRoles(
120 userId, group.getGroupId()));
121 roles.addAll(
122 RoleLocalServiceUtil.getUserGroupGroupRoles(
123 userId, group.getGroupId()));
124 }
125 }
126 else {
127 roles = RoleLocalServiceUtil.getUserRoles(userId);
128 }
129
130 if (roles.size() > 0) {
131 scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
132 }
133
134 return scopes;
135 }
136
137 private static long[] _getGroupIds(List<Group> groups) {
138 long[] groupIds = new long[groups.size()];
139
140 int i = 0;
141
142 for (Group group : groups) {
143 groupIds[i++] = group.getGroupId();
144 }
145
146 return groupIds;
147 }
148
149 private static long[] _getOrganizationIds(
150 List<Organization> organizations) {
151
152 long[] organizationIds = new long[organizations.size()];
153
154 int i = 0;
155
156 for (Organization organization : organizations) {
157 organizationIds[i++] = organization.getOrganizationId();
158 }
159
160 return organizationIds;
161 }
162
163 private static long[] _getRoleIds(List<Role> roles) {
164 long[] roleIds = new long[roles.size()];
165
166 int i = 0;
167
168 for (Role role : roles) {
169 roleIds[i++] = role.getRoleId();
170 }
171
172 return roleIds;
173 }
174
175 private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
176 long[] userGroupIds = new long[userGroups.size()];
177
178 int i = 0;
179
180 for (UserGroup userGroup : userGroups) {
181 userGroupIds[i++] = userGroup.getUserGroupId();
182 }
183
184 return userGroupIds;
185 }
186
187 private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
188 Group.class.getName());
189
190 private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
191 Organization.class.getName());
192
193 private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
194 Role.class.getName());
195
196 private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
197 User.class.getName());
198
199 private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
200 UserGroup.class.getName());
201
202 }