001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.UniqueList;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Organization;
026    import com.liferay.portal.model.Role;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.UserGroup;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.OrganizationLocalServiceUtil;
032    import com.liferay.portal.service.RoleLocalServiceUtil;
033    import com.liferay.portal.service.UserGroupLocalServiceUtil;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.util.PortalUtil;
036    
037    import java.util.ArrayList;
038    import java.util.LinkedHashMap;
039    import java.util.List;
040    
041    /**
042     * @author Raymond Aug??
043     */
044    public class AnnouncementsUtil {
045    
046            public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
047                    throws PortalException, SystemException {
048    
049                    LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
050    
051                    // General announcements
052    
053                    scopes.put(new Long(0), new long[] {0});
054    
055                    // Personal announcements
056    
057                    scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
058    
059                    // Organization announcements
060    
061                    List<Group> groupsList = new ArrayList<Group>();
062    
063                    List<Organization> organizations =
064                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
065    
066                    if (!organizations.isEmpty()) {
067                            List<Organization> organizationsList =
068                                    new ArrayList<Organization>();
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                    List<Role> roles = new UniqueList<Role>();
116    
117                    if (!groupsList.isEmpty()) {
118                            roles = RoleLocalServiceUtil.getUserRelatedRoles(
119                                    userId, groupsList);
120    
121                            roles = ListUtil.copy(roles);
122    
123                            for (Group group : groupsList) {
124                                    roles.addAll(
125                                            RoleLocalServiceUtil.getUserGroupRoles(
126                                                    userId, group.getGroupId()));
127                                    roles.addAll(
128                                            RoleLocalServiceUtil.getUserGroupGroupRoles(
129                                                    userId, group.getGroupId()));
130                            }
131                    }
132                    else {
133                            roles = RoleLocalServiceUtil.getUserRoles(userId);
134    
135                            roles = ListUtil.copy(roles);
136                    }
137    
138                    if (_PERMISSIONS_CHECK_GUEST_ENABLED) {
139                            User user = UserLocalServiceUtil.getUserById(userId);
140    
141                            Role guestRole = RoleLocalServiceUtil.getRole(
142                                    user.getCompanyId(), RoleConstants.GUEST);
143    
144                            roles.add(guestRole);
145                    }
146    
147                    if (roles.size() > 0) {
148                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
149                    }
150    
151                    return scopes;
152            }
153    
154            private static long[] _getGroupIds(List<Group> groups) {
155                    long[] groupIds = new long[groups.size()];
156    
157                    int i = 0;
158    
159                    for (Group group : groups) {
160                            groupIds[i++] = group.getGroupId();
161                    }
162    
163                    return groupIds;
164            }
165    
166            private static long[] _getOrganizationIds(
167                    List<Organization> organizations) {
168    
169                    long[] organizationIds = new long[organizations.size()];
170    
171                    int i = 0;
172    
173                    for (Organization organization : organizations) {
174                            organizationIds[i++] = organization.getOrganizationId();
175                    }
176    
177                    return organizationIds;
178            }
179    
180            private static long[] _getRoleIds(List<Role> roles) {
181                    long[] roleIds = new long[roles.size()];
182    
183                    int i = 0;
184    
185                    for (Role role : roles) {
186                            roleIds[i++] = role.getRoleId();
187                    }
188    
189                    return roleIds;
190            }
191    
192            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
193                    long[] userGroupIds = new long[userGroups.size()];
194    
195                    int i = 0;
196    
197                    for (UserGroup userGroup : userGroups) {
198                            userGroupIds[i++] = userGroup.getUserGroupId();
199                    }
200    
201                    return userGroupIds;
202            }
203    
204            private static final long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
205                    Group.class.getName());
206    
207            private static final long _ORGANIZATION_CLASS_NAME_ID =
208                    PortalUtil.getClassNameId(Organization.class.getName());
209    
210            private static final boolean _PERMISSIONS_CHECK_GUEST_ENABLED =
211                    GetterUtil.getBoolean(
212                            PropsUtil.get(PropsKeys.PERMISSIONS_CHECK_GUEST_ENABLED));
213    
214            private static final long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
215                    Role.class.getName());
216    
217            private static final long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
218                    User.class.getName());
219    
220            private static final long _USER_GROUP_CLASS_NAME_ID =
221                    PortalUtil.getClassNameId(UserGroup.class.getName());
222    
223    }