001    /**
002     * Copyright (c) 2000-2010 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.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    /**
036     * @author Raymond Augé
037     */
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                    // General announcements
046    
047                    scopes.put(new Long(0), new long[] {0});
048    
049                    // Personal announcements
050    
051                    scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
052    
053                    // Community announcements
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                    // Organization announcements
066    
067                    List<Organization> organizations =
068                            OrganizationLocalServiceUtil.getUserOrganizations(userId, true);
069    
070                    if (!organizations.isEmpty()) {
071                            scopes.put(
072                                    _ORGANIZATION_CLASS_NAME_ID,
073                                    _getOrganizationIds(organizations));
074    
075                            for (Organization organization : organizations) {
076                                    groupsList.add(organization.getGroup());
077                            }
078                    }
079    
080                    // User group announcements
081    
082                    List<UserGroup> userGroups =
083                            UserGroupLocalServiceUtil.getUserUserGroups(userId);
084    
085                    if (!userGroups.isEmpty()) {
086                            scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
087    
088                            for (UserGroup userGroup : userGroups) {
089                                    groupsList.add(userGroup.getGroup());
090                            }
091                    }
092    
093                    // Role announcements
094    
095                    List<Role> roles = new ArrayList<Role>();
096    
097                    if (!groupsList.isEmpty()) {
098                            roles = RoleLocalServiceUtil.getUserRelatedRoles(
099                                    userId, groupsList);
100    
101                            roles = ListUtil.copy(roles);
102    
103                            for (Group group : groupsList) {
104                                    roles.addAll(
105                                            RoleLocalServiceUtil.getUserGroupRoles(
106                                                    userId, group.getGroupId()));
107                                    roles.addAll(
108                                            RoleLocalServiceUtil.getUserGroupGroupRoles(
109                                                    userId, group.getGroupId()));
110                            }
111                    }
112                    else {
113                            roles = RoleLocalServiceUtil.getUserRoles(userId);
114                    }
115    
116                    if (roles.size() > 0) {
117                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
118                    }
119    
120                    return scopes;
121            }
122    
123            private static long[] _getGroupIds(List<Group> groups) {
124                    long[] groupIds = new long[groups.size()];
125    
126                    int i = 0;
127    
128                    for (Group group : groups) {
129                            groupIds[i++] = group.getGroupId();
130                    }
131    
132                    return groupIds;
133            }
134    
135            private static long[] _getOrganizationIds(
136                    List<Organization> organizations) {
137    
138                    long[] organizationIds = new long[organizations.size()];
139    
140                    int i = 0;
141    
142                    for (Organization organization : organizations) {
143                            organizationIds[i++] = organization.getOrganizationId();
144                    }
145    
146                    return organizationIds;
147            }
148    
149            private static long[] _getRoleIds(List<Role> roles) {
150                    long[] roleIds = new long[roles.size()];
151    
152                    int i = 0;
153    
154                    for (Role role : roles) {
155                            roleIds[i++] = role.getRoleId();
156                    }
157    
158                    return roleIds;
159            }
160    
161            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
162                    long[] userGroupIds = new long[userGroups.size()];
163    
164                    int i = 0;
165    
166                    for (UserGroup userGroup : userGroups) {
167                            userGroupIds[i++] = userGroup.getUserGroupId();
168                    }
169    
170                    return userGroupIds;
171            }
172    
173            private static long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
174                    Group.class.getName());
175    
176            private static long _ORGANIZATION_CLASS_NAME_ID = PortalUtil.getClassNameId(
177                    Organization.class.getName());
178    
179            private static long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
180                    Role.class.getName());
181    
182            private static long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
183                    User.class.getName());
184    
185            private static long _USER_GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
186                    UserGroup.class.getName());
187    
188    }