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<>();
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<>();
063    
064                    List<Organization> organizations =
065                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
066    
067                    if (!organizations.isEmpty()) {
068                            List<Organization> organizationsList = new ArrayList<>();
069    
070                            organizationsList.addAll(organizations);
071    
072                            for (Organization organization : organizations) {
073                                    groupsList.add(organization.getGroup());
074    
075                                    List<Organization> parentOrganizations =
076                                            OrganizationLocalServiceUtil.getParentOrganizations(
077                                                    organization.getOrganizationId());
078    
079                                    for (Organization parentOrganization : parentOrganizations) {
080                                            organizationsList.add(parentOrganization);
081                                            groupsList.add(parentOrganization.getGroup());
082                                    }
083                            }
084    
085                            scopes.put(
086                                    _ORGANIZATION_CLASS_NAME_ID,
087                                    _getOrganizationIds(organizationsList));
088                    }
089    
090                    // Site announcements
091    
092                    List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
093    
094                    if (!groups.isEmpty()) {
095                            scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
096    
097                            groupsList.addAll(groups);
098                    }
099    
100                    // User group announcements
101    
102                    List<UserGroup> userGroups =
103                            UserGroupLocalServiceUtil.getUserUserGroups(userId);
104    
105                    if (!userGroups.isEmpty()) {
106                            scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
107    
108                            for (UserGroup userGroup : userGroups) {
109                                    groupsList.add(userGroup.getGroup());
110                            }
111                    }
112    
113                    // Role announcements
114    
115                    Set<Role> roles = new LinkedHashSet<>();
116    
117                    if (!groupsList.isEmpty()) {
118                            roles.addAll(
119                                    RoleLocalServiceUtil.getUserRelatedRoles(userId, groupsList));
120    
121                            for (Group group : groupsList) {
122                                    roles.addAll(
123                                            RoleLocalServiceUtil.getUserGroupRoles(
124                                                    userId, group.getGroupId()));
125                                    roles.addAll(
126                                            RoleLocalServiceUtil.getUserGroupGroupRoles(
127                                                    userId, group.getGroupId()));
128                            }
129                    }
130                    else {
131                            roles.addAll(RoleLocalServiceUtil.getUserRoles(userId));
132                    }
133    
134                    List<Team> teams = TeamLocalServiceUtil.getUserTeams(userId);
135    
136                    for (Team team : teams) {
137                            roles.add(team.getRole());
138                    }
139    
140                    if (_PERMISSIONS_CHECK_GUEST_ENABLED) {
141                            User user = UserLocalServiceUtil.getUserById(userId);
142    
143                            Role guestRole = RoleLocalServiceUtil.getRole(
144                                    user.getCompanyId(), RoleConstants.GUEST);
145    
146                            roles.add(guestRole);
147                    }
148    
149                    if (!roles.isEmpty()) {
150                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
151                    }
152    
153                    return scopes;
154            }
155    
156            private static long[] _getGroupIds(List<Group> groups) {
157                    long[] groupIds = new long[groups.size()];
158    
159                    int i = 0;
160    
161                    for (Group group : groups) {
162                            groupIds[i++] = group.getGroupId();
163                    }
164    
165                    return groupIds;
166            }
167    
168            private static long[] _getOrganizationIds(
169                    List<Organization> organizations) {
170    
171                    long[] organizationIds = new long[organizations.size()];
172    
173                    int i = 0;
174    
175                    for (Organization organization : organizations) {
176                            organizationIds[i++] = organization.getOrganizationId();
177                    }
178    
179                    return organizationIds;
180            }
181    
182            private static long[] _getRoleIds(Set<Role> roles) {
183                    long[] roleIds = new long[roles.size()];
184    
185                    int i = 0;
186    
187                    for (Role role : roles) {
188                            roleIds[i++] = role.getRoleId();
189                    }
190    
191                    return roleIds;
192            }
193    
194            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
195                    long[] userGroupIds = new long[userGroups.size()];
196    
197                    int i = 0;
198    
199                    for (UserGroup userGroup : userGroups) {
200                            userGroupIds[i++] = userGroup.getUserGroupId();
201                    }
202    
203                    return userGroupIds;
204            }
205    
206            private static final long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
207                    Group.class.getName());
208    
209            private static final long _ORGANIZATION_CLASS_NAME_ID =
210                    PortalUtil.getClassNameId(Organization.class.getName());
211    
212            private static final boolean _PERMISSIONS_CHECK_GUEST_ENABLED =
213                    GetterUtil.getBoolean(
214                            PropsUtil.get(PropsKeys.PERMISSIONS_CHECK_GUEST_ENABLED));
215    
216            private static final long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
217                    Role.class.getName());
218    
219            private static final long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
220                    User.class.getName());
221    
222            private static final long _USER_GROUP_CLASS_NAME_ID =
223                    PortalUtil.getClassNameId(UserGroup.class.getName());
224    
225    }