001
014
015 package com.liferay.portlet.exportimport.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
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 = new HashMap<>();
115 protected Map<Long, List<User>> groupUsersMap = new HashMap<>();
116 protected Map<String, Role> rolesMap = new HashMap<>();
117 protected Map<Long, List<Role>> userRolesMap = new HashMap<>();
118
119 }