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.ListUtil;
020    import com.liferay.portal.kernel.util.UniqueList;
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.User;
026    import com.liferay.portal.model.UserGroup;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.OrganizationLocalServiceUtil;
029    import com.liferay.portal.service.RoleLocalServiceUtil;
030    import com.liferay.portal.service.UserGroupLocalServiceUtil;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.PropsValues;
034    
035    import java.util.ArrayList;
036    import java.util.LinkedHashMap;
037    import java.util.List;
038    
039    /**
040     * @author Raymond Augé
041     */
042    public class AnnouncementsUtil {
043    
044            public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
045                    throws PortalException, SystemException {
046    
047                    LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
048    
049                    // General announcements
050    
051                    scopes.put(new Long(0), new long[] {0});
052    
053                    // Personal announcements
054    
055                    scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
056    
057                    // Organization announcements
058    
059                    List<Group> groupsList = new ArrayList<Group>();
060    
061                    List<Organization> organizations =
062                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
063    
064                    if (!organizations.isEmpty()) {
065                            List<Organization> organizationsList =
066                                    new ArrayList<Organization>();
067    
068                            organizationsList.addAll(organizations);
069    
070                            for (Organization organization : organizations) {
071                                    groupsList.add(organization.getGroup());
072    
073                                    List<Organization> parentOrganizations =
074                                            OrganizationLocalServiceUtil.getParentOrganizations(
075                                                    organization.getOrganizationId());
076    
077                                    for (Organization parentOrganization : parentOrganizations) {
078                                            organizationsList.add(parentOrganization);
079                                            groupsList.add(parentOrganization.getGroup());
080                                    }
081                            }
082    
083                            scopes.put(
084                                    _ORGANIZATION_CLASS_NAME_ID,
085                                    _getOrganizationIds(organizationsList));
086                    }
087    
088                    // Site announcements
089    
090                    List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
091    
092                    if (!groups.isEmpty()) {
093                            scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
094    
095                            groupsList.addAll(groups);
096                    }
097    
098                    // User group announcements
099    
100                    List<UserGroup> userGroups =
101                            UserGroupLocalServiceUtil.getUserUserGroups(userId);
102    
103                    if (!userGroups.isEmpty()) {
104                            scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
105    
106                            for (UserGroup userGroup : userGroups) {
107                                    groupsList.add(userGroup.getGroup());
108                            }
109                    }
110    
111                    // Role announcements
112    
113                    List<Role> roles = new UniqueList<Role>();
114    
115                    if (!groupsList.isEmpty()) {
116                            roles = RoleLocalServiceUtil.getUserRelatedRoles(
117                                    userId, groupsList);
118    
119                            roles = ListUtil.copy(roles);
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 = RoleLocalServiceUtil.getUserRoles(userId);
132    
133                            roles = ListUtil.copy(roles);
134                    }
135    
136                    if (PropsValues.PERMISSIONS_CHECK_GUEST_ENABLED) {
137                            User user = UserLocalServiceUtil.getUserById(userId);
138    
139                            Role guestRole = RoleLocalServiceUtil.getRole(
140                                    user.getCompanyId(), RoleConstants.GUEST);
141    
142                            roles.add(guestRole);
143                    }
144    
145                    if (roles.size() > 0) {
146                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
147                    }
148    
149                    return scopes;
150            }
151    
152            private static long[] _getGroupIds(List<Group> groups) {
153                    long[] groupIds = new long[groups.size()];
154    
155                    int i = 0;
156    
157                    for (Group group : groups) {
158                            groupIds[i++] = group.getGroupId();
159                    }
160    
161                    return groupIds;
162            }
163    
164            private static long[] _getOrganizationIds(
165                    List<Organization> organizations) {
166    
167                    long[] organizationIds = new long[organizations.size()];
168    
169                    int i = 0;
170    
171                    for (Organization organization : organizations) {
172                            organizationIds[i++] = organization.getOrganizationId();
173                    }
174    
175                    return organizationIds;
176            }
177    
178            private static long[] _getRoleIds(List<Role> roles) {
179                    long[] roleIds = new long[roles.size()];
180    
181                    int i = 0;
182    
183                    for (Role role : roles) {
184                            roleIds[i++] = role.getRoleId();
185                    }
186    
187                    return roleIds;
188            }
189    
190            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
191                    long[] userGroupIds = new long[userGroups.size()];
192    
193                    int i = 0;
194    
195                    for (UserGroup userGroup : userGroups) {
196                            userGroupIds[i++] = userGroup.getUserGroupId();
197                    }
198    
199                    return userGroupIds;
200            }
201    
202            private static final long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
203                    Group.class.getName());
204    
205            private static final long _ORGANIZATION_CLASS_NAME_ID =
206                    PortalUtil.getClassNameId(Organization.class.getName());
207    
208            private static final long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
209                    Role.class.getName());
210    
211            private static final long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
212                    User.class.getName());
213    
214            private static final long _USER_GROUP_CLASS_NAME_ID =
215                    PortalUtil.getClassNameId(UserGroup.class.getName());
216    
217    }