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.portal.lar;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.Team;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.security.permission.ResourceActionsUtil;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portal.service.RoleLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    import java.util.HashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Charles May
035     */
036    public class LayoutCache {
037    
038            protected List<Role> getGroupRoles(long groupId, String resourceName)
039                    throws PortalException {
040    
041                    List<Role> roles = groupRolesMap.get(groupId);
042    
043                    if (roles != null) {
044                            return roles;
045                    }
046    
047                    Group group = GroupLocalServiceUtil.getGroup(groupId);
048    
049                    roles = ListUtil.copy(
050                            ResourceActionsUtil.getRoles(
051                                    group.getCompanyId(), group, resourceName, null));
052    
053                    Map<Team, Role> teamRoleMap = RoleLocalServiceUtil.getTeamRoleMap(
054                            groupId);
055    
056                    for (Map.Entry<Team, Role> entry : teamRoleMap.entrySet()) {
057                            Team team = entry.getKey();
058                            Role teamRole = entry.getValue();
059    
060                            teamRole.setName(
061                                    PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
062                            teamRole.setDescription(team.getDescription());
063    
064                            roles.add(teamRole);
065                    }
066    
067                    groupRolesMap.put(groupId, roles);
068    
069                    return roles;
070            }
071    
072            protected List<User> getGroupUsers(long groupId) {
073                    List<User> users = groupUsersMap.get(groupId);
074    
075                    if (users == null) {
076                            users = UserLocalServiceUtil.getGroupUsers(groupId);
077    
078                            groupUsersMap.put(groupId, users);
079                    }
080    
081                    return users;
082            }
083    
084            protected Role getRole(long companyId, String roleName)
085                    throws PortalException {
086    
087                    Role role = rolesMap.get(roleName);
088    
089                    if (role == null) {
090                            try {
091                                    role = RoleLocalServiceUtil.getRole(companyId, roleName);
092    
093                                    rolesMap.put(roleName, role);
094                            }
095                            catch (NoSuchRoleException nsre) {
096                            }
097                    }
098    
099                    return role;
100            }
101    
102            protected List<Role> getUserRoles(long userId) {
103                    List<Role> userRoles = userRolesMap.get(userId);
104    
105                    if (userRoles == null) {
106                            userRoles = RoleLocalServiceUtil.getUserRoles(userId);
107    
108                            userRolesMap.put(userId, userRoles);
109                    }
110    
111                    return userRoles;
112            }
113    
114            protected Map<Long, List<Role>> groupRolesMap =
115                    new HashMap<Long, List<Role>>();
116            protected Map<Long, List<User>> groupUsersMap =
117                    new HashMap<Long, List<User>>();
118            protected Map<String, Role> rolesMap = new HashMap<String, Role>();
119            protected Map<Long, List<Role>> userRolesMap =
120                    new HashMap<Long, List<Role>>();
121    
122    }