001
014
015 package com.liferay.portal.lar;
016
017 import com.liferay.portal.NoSuchRoleException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.util.ListUtil;
022 import com.liferay.portal.kernel.util.OrderByComparator;
023 import com.liferay.portal.model.Group;
024 import com.liferay.portal.model.Organization;
025 import com.liferay.portal.model.OrganizationConstants;
026 import com.liferay.portal.model.Role;
027 import com.liferay.portal.model.Team;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.model.UserGroup;
030 import com.liferay.portal.security.permission.ResourceActionsUtil;
031 import com.liferay.portal.service.GroupLocalServiceUtil;
032 import com.liferay.portal.service.OrganizationLocalServiceUtil;
033 import com.liferay.portal.service.RoleLocalServiceUtil;
034 import com.liferay.portal.service.UserGroupLocalServiceUtil;
035 import com.liferay.portal.service.UserLocalServiceUtil;
036
037 import java.util.HashMap;
038 import java.util.List;
039 import java.util.Map;
040
041
044 public class LayoutCache {
045
046 protected long getEntityGroupId(
047 long companyId, String entityName, String name)
048 throws PortalException, SystemException {
049
050 long entityGroupId = 0;
051
052 Long entityGroupIdObj = entityGroupIdMap.get(entityName);
053
054 if (entityGroupIdObj == null) {
055 if (entityName.equals("user-group")) {
056 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
057 companyId, null, null, 0, 1, (OrderByComparator)null);
058
059 if (userGroups.size() > 0) {
060 UserGroup userGroup = userGroups.get(0);
061
062 entityGroupId = userGroup.getGroupId();
063 }
064 }
065 else if (entityName.equals("organization")) {
066 List<Organization> organizations =
067 OrganizationLocalServiceUtil.search(
068 companyId,
069 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
070 null, null, null, null, null, null, null, true, 0, 1);
071
072 if (organizations.size() > 0) {
073 Organization organization = organizations.get(0);
074
075 entityGroupId = organization.getGroupId();
076 }
077 }
078
079 entityGroupIdMap.put(entityName, entityGroupId);
080 }
081 else {
082 entityGroupId = entityGroupIdObj.longValue();
083 }
084
085 return entityGroupId;
086 }
087
088 protected Map<String, Long> getEntityMap(long companyId, String entityName)
089 throws PortalException, SystemException {
090
091 Map<String, Long> entityMap = entityMapMap.get(entityName);
092
093 if (entityMap != null) {
094 return entityMap;
095 }
096
097 entityMap = new HashMap<String, Long>();
098
099 if (entityName.equals("user-group")) {
100 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
101 companyId, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
102 (OrderByComparator)null);
103
104 for (int i = 0; i < userGroups.size(); i++) {
105 UserGroup userGroup = userGroups.get(i);
106
107 Group group = userGroup.getGroup();
108
109 entityMap.put(userGroup.getName(), group.getGroupId());
110 }
111 }
112 else if (entityName.equals("organization")) {
113 List<Organization> organizations =
114 OrganizationLocalServiceUtil.search(
115 companyId, OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
116 null, OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
117 null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
118
119 for (int i = 0; i < organizations.size(); i++) {
120 Organization organization = organizations.get(i);
121
122 Group group = organization.getGroup();
123
124 entityMap.put(organization.getName(), group.getGroupId());
125 }
126 }
127
128 entityMapMap.put(entityName, entityMap);
129
130 return entityMap;
131 }
132
133 protected List<Role> getGroupRoles(long groupId, String resourceName)
134 throws PortalException, SystemException {
135
136 List<Role> roles = groupRolesMap.get(groupId);
137
138 if (roles != null) {
139 return roles;
140 }
141
142 Group group = GroupLocalServiceUtil.getGroup(groupId);
143
144 roles = ListUtil.copy(
145 ResourceActionsUtil.getRoles(
146 group.getCompanyId(), group, resourceName, null));
147
148 Map<Team, Role> teamRoleMap = RoleLocalServiceUtil.getTeamRoleMap(
149 groupId);
150
151 for (Map.Entry<Team, Role> entry : teamRoleMap.entrySet()) {
152 Team team = entry.getKey();
153 Role teamRole = entry.getValue();
154
155 teamRole.setName(
156 PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
157 teamRole.setDescription(team.getDescription());
158
159 roles.add(teamRole);
160 }
161
162 groupRolesMap.put(groupId, roles);
163
164 return roles;
165 }
166
167 protected List<User> getGroupUsers(long groupId) throws SystemException {
168 List<User> users = groupUsersMap.get(groupId);
169
170 if (users == null) {
171 users = UserLocalServiceUtil.getGroupUsers(groupId);
172
173 groupUsersMap.put(groupId, users);
174 }
175
176 return users;
177 }
178
179 protected Role getRole(long companyId, String roleName)
180 throws PortalException, SystemException {
181
182 Role role = rolesMap.get(roleName);
183
184 if (role == null) {
185 try {
186 role = RoleLocalServiceUtil.getRole(companyId, roleName);
187
188 rolesMap.put(roleName, role);
189 }
190 catch (NoSuchRoleException nsre) {
191 }
192 }
193
194 return role;
195 }
196
197 protected List<Role> getUserRoles(long userId) throws SystemException {
198 List<Role> userRoles = userRolesMap.get(userId);
199
200 if (userRoles == null) {
201 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
202
203 userRolesMap.put(userId, userRoles);
204 }
205
206 return userRoles;
207 }
208
209 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
210 protected Map<String, Map<String, Long>> entityMapMap =
211 new HashMap<String, Map<String, Long>>();
212 protected Map<Long, List<Role>> groupRolesMap =
213 new HashMap<Long, List<Role>>();
214 protected Map<Long, List<User>> groupUsersMap =
215 new HashMap<Long, List<User>>();
216 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
217 protected Map<Long, List<Role>> userRolesMap =
218 new HashMap<Long, List<Role>>();
219
220 }