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 Group group = userGroup.getGroup();
063
064 entityGroupId = group.getGroupId();
065 }
066 }
067 else if (entityName.equals("organization")) {
068 List<Organization> organizations =
069 OrganizationLocalServiceUtil.search(
070 companyId,
071 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
072 null, null, null, null, null, null, null, true, 0, 1);
073
074 if (organizations.size() > 0) {
075 Organization organization = organizations.get(0);
076
077 Group group = organization.getGroup();
078
079 entityGroupId = group.getGroupId();
080 }
081 }
082
083 entityGroupIdMap.put(entityName, entityGroupId);
084 }
085 else {
086 entityGroupId = entityGroupIdObj.longValue();
087 }
088
089 return entityGroupId;
090 }
091
092 protected Map<String, Long> getEntityMap(long companyId, String entityName)
093 throws PortalException, SystemException {
094
095 Map<String, Long> entityMap = entityMapMap.get(entityName);
096
097 if (entityMap != null) {
098 return entityMap;
099 }
100
101 entityMap = new HashMap<String, Long>();
102
103 if (entityName.equals("user-group")) {
104 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
105 companyId, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
106 (OrderByComparator)null);
107
108 for (int i = 0; i < userGroups.size(); i++) {
109 UserGroup userGroup = userGroups.get(i);
110
111 Group group = userGroup.getGroup();
112
113 entityMap.put(userGroup.getName(), group.getGroupId());
114 }
115 }
116 else if (entityName.equals("organization")) {
117 List<Organization> organizations =
118 OrganizationLocalServiceUtil.search(
119 companyId, OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
120 null, OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
121 null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
122
123 for (int i = 0; i < organizations.size(); i++) {
124 Organization organization = organizations.get(i);
125
126 Group group = organization.getGroup();
127
128 entityMap.put(organization.getName(), group.getGroupId());
129 }
130 }
131
132 entityMapMap.put(entityName, entityMap);
133
134 return entityMap;
135 }
136
137 protected List<Role> getGroupRoles(long groupId, String resourceName)
138 throws PortalException, SystemException {
139
140 List<Role> roles = groupRolesMap.get(groupId);
141
142 if (roles != null) {
143 return roles;
144 }
145
146 Group group = GroupLocalServiceUtil.getGroup(groupId);
147
148 roles = ListUtil.copy(
149 ResourceActionsUtil.getRoles(
150 group.getCompanyId(), group, resourceName, null));
151
152 Map<Team, Role> teamRoleMap = RoleLocalServiceUtil.getTeamRoleMap(
153 groupId);
154
155 for (Map.Entry<Team, Role> entry : teamRoleMap.entrySet()) {
156 Team team = entry.getKey();
157 Role teamRole = entry.getValue();
158
159 teamRole.setName(
160 PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
161 teamRole.setDescription(team.getDescription());
162
163 roles.add(teamRole);
164 }
165
166 groupRolesMap.put(groupId, roles);
167
168 return roles;
169 }
170
171 protected List<User> getGroupUsers(long groupId) throws SystemException {
172 List<User> users = groupUsersMap.get(groupId);
173
174 if (users == null) {
175 users = UserLocalServiceUtil.getGroupUsers(groupId);
176
177 groupUsersMap.put(groupId, users);
178 }
179
180 return users;
181 }
182
183 protected Role getRole(long companyId, String roleName)
184 throws PortalException, SystemException {
185
186 Role role = rolesMap.get(roleName);
187
188 if (role == null) {
189 try {
190 role = RoleLocalServiceUtil.getRole(companyId, roleName);
191
192 rolesMap.put(roleName, role);
193 }
194 catch (NoSuchRoleException nsre) {
195 }
196 }
197
198 return role;
199 }
200
201 protected List<Role> getUserRoles(long userId) throws SystemException {
202 List<Role> userRoles = userRolesMap.get(userId);
203
204 if (userRoles == null) {
205 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
206
207 userRolesMap.put(userId, userRoles);
208 }
209
210 return userRoles;
211 }
212
213 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
214 protected Map<String, Map<String, Long>> entityMapMap =
215 new HashMap<String, Map<String, Long>>();
216 protected Map<Long, List<Role>> groupRolesMap =
217 new HashMap<Long, List<Role>>();
218 protected Map<Long, List<User>> groupUsersMap =
219 new HashMap<Long, List<User>>();
220 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
221 protected Map<Long, List<Role>> userRolesMap =
222 new HashMap<Long, List<Role>>();
223
224 }