001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.announcements.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Organization;
023    import com.liferay.portal.model.Role;
024    import com.liferay.portal.model.RoleConstants;
025    import com.liferay.portal.model.Team;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.model.UserGroup;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.service.OrganizationLocalServiceUtil;
030    import com.liferay.portal.service.RoleLocalServiceUtil;
031    import com.liferay.portal.service.TeamLocalServiceUtil;
032    import com.liferay.portal.service.UserGroupLocalServiceUtil;
033    import com.liferay.portal.service.UserLocalServiceUtil;
034    import com.liferay.portal.util.PortalUtil;
035    
036    import java.util.ArrayList;
037    import java.util.LinkedHashMap;
038    import java.util.LinkedHashSet;
039    import java.util.List;
040    import java.util.Set;
041    
042    /**
043     * @author Raymond Aug??
044     */
045    public class AnnouncementsUtil {
046    
047            public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
048                    throws PortalException {
049    
050                    LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
051    
052                    // General announcements
053    
054                    scopes.put(new Long(0), new long[] {0});
055    
056                    // Personal announcements
057    
058                    scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
059    
060                    // Organization announcements
061    
062                    List<Group> groupsList = new ArrayList<Group>();
063    
064                    List<Organization> organizations =
065                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
066    
067                    if (!organizations.isEmpty()) {
068                            List<Organization> organizationsList =
069                                    new ArrayList<Organization>();
070    
071                            organizationsList.addAll(organizations);
072    
073                            for (Organization organization : organizations) {
074                                    groupsList.add(organization.getGroup());
075    
076                                    List<Organization> parentOrganizations =
077                                            OrganizationLocalServiceUtil.getParentOrganizations(
078                                                    organization.getOrganizationId());
079    
080                                    for (Organization parentOrganization : parentOrganizations) {
081                                            organizationsList.add(parentOrganization);
082                                            groupsList.add(parentOrganization.getGroup());
083                                    }
084                            }
085    
086                            scopes.put(
087                                    _ORGANIZATION_CLASS_NAME_ID,
088                                    _getOrganizationIds(organizationsList));
089                    }
090    
091                    // Site announcements
092    
093                    List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
094    
095                    if (!groups.isEmpty()) {
096                            scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
097    
098                            groupsList.addAll(groups);
099                    }
100    
101                    // User group announcements
102    
103                    List<UserGroup> userGroups =
104                            UserGroupLocalServiceUtil.getUserUserGroups(userId);
105    
106                    if (!userGroups.isEmpty()) {
107                            scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
108    
109                            for (UserGroup userGroup : userGroups) {
110                                    groupsList.add(userGroup.getGroup());
111                            }
112                    }
113    
114                    // Role announcements
115    
116                    Set<Role> roles = new LinkedHashSet<Role>();
117    
118                    if (!groupsList.isEmpty()) {
119                            roles.addAll(
120                                    RoleLocalServiceUtil.getUserRelatedRoles(userId, groupsList));
121    
122                            for (Group group : groupsList) {
123                                    roles.addAll(
124                                            RoleLocalServiceUtil.getUserGroupRoles(
125                                                    userId, group.getGroupId()));
126                                    roles.addAll(
127                                            RoleLocalServiceUtil.getUserGroupGroupRoles(
128                                                    userId, group.getGroupId()));
129                            }
130                    }
131                    else {
132                            roles.addAll(RoleLocalServiceUtil.getUserRoles(userId));
133                    }
134    
135                    List<Team> teams = TeamLocalServiceUtil.getUserTeams(userId);
136    
137                    for (Team team : teams) {
138                            roles.add(team.getRole());
139                    }
140    
141                    if (_PERMISSIONS_CHECK_GUEST_ENABLED) {
142                            User user = UserLocalServiceUtil.getUserById(userId);
143    
144                            Role guestRole = RoleLocalServiceUtil.getRole(
145                                    user.getCompanyId(), RoleConstants.GUEST);
146    
147                            roles.add(guestRole);
148                    }
149    
150                    if (!roles.isEmpty()) {
151                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
152                    }
153    
154                    return scopes;
155            }
156    
157            private static long[] _getGroupIds(List<Group> groups) {
158                    long[] groupIds = new long[groups.size()];
159    
160                    int i = 0;
161    
162                    for (Group group : groups) {
163                            groupIds[i++] = group.getGroupId();
164                    }
165    
166                    return groupIds;
167            }
168    
169            private static long[] _getOrganizationIds(
170                    List<Organization> organizations) {
171    
172                    long[] organizationIds = new long[organizations.size()];
173    
174                    int i = 0;
175    
176                    for (Organization organization : organizations) {
177                            organizationIds[i++] = organization.getOrganizationId();
178                    }
179    
180                    return organizationIds;
181            }
182    
183            private static long[] _getRoleIds(Set<Role> roles) {
184                    long[] roleIds = new long[roles.size()];
185    
186                    int i = 0;
187    
188                    for (Role role : roles) {
189                            roleIds[i++] = role.getRoleId();
190                    }
191    
192                    return roleIds;
193            }
194    
195            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
196                    long[] userGroupIds = new long[userGroups.size()];
197    
198                    int i = 0;
199    
200                    for (UserGroup userGroup : userGroups) {
201                            userGroupIds[i++] = userGroup.getUserGroupId();
202                    }
203    
204                    return userGroupIds;
205            }
206    
207            private static final long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
208                    Group.class.getName());
209    
210            private static final long _ORGANIZATION_CLASS_NAME_ID =
211                    PortalUtil.getClassNameId(Organization.class.getName());
212    
213            private static final boolean _PERMISSIONS_CHECK_GUEST_ENABLED =
214                    GetterUtil.getBoolean(
215                            PropsUtil.get(PropsKeys.PERMISSIONS_CHECK_GUEST_ENABLED));
216    
217            private static final long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
218                    Role.class.getName());
219    
220            private static final long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
221                    User.class.getName());
222    
223            private static final long _USER_GROUP_CLASS_NAME_ID =
224                    PortalUtil.getClassNameId(UserGroup.class.getName());
225    
226    }