001
014
015 package com.liferay.portal.lar;
016
017 import com.liferay.portal.NoSuchResourceException;
018 import com.liferay.portal.NoSuchRoleException;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.util.OrderByComparator;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.workflow.WorkflowConstants;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.Organization;
028 import com.liferay.portal.model.OrganizationConstants;
029 import com.liferay.portal.model.Resource;
030 import com.liferay.portal.model.Role;
031 import com.liferay.portal.model.Team;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.model.UserGroup;
034 import com.liferay.portal.security.permission.ResourceActionsUtil;
035 import com.liferay.portal.service.GroupLocalServiceUtil;
036 import com.liferay.portal.service.OrganizationLocalServiceUtil;
037 import com.liferay.portal.service.ResourceLocalServiceUtil;
038 import com.liferay.portal.service.RoleLocalServiceUtil;
039 import com.liferay.portal.service.TeamLocalServiceUtil;
040 import com.liferay.portal.service.UserGroupLocalServiceUtil;
041 import com.liferay.portal.service.UserLocalServiceUtil;
042
043 import java.util.HashMap;
044 import java.util.LinkedHashMap;
045 import java.util.List;
046 import java.util.Map;
047
048
051 public class LayoutCache {
052
053 protected long getEntityGroupId(
054 long companyId, String entityName, String name)
055 throws PortalException, SystemException {
056
057 long entityGroupId = 0;
058
059 Long entityGroupIdObj = entityGroupIdMap.get(entityName);
060
061 if (entityGroupIdObj == null) {
062 if (entityName.equals("user-group")) {
063 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
064 companyId, null, null, 0, 1, null);
065
066 if (userGroups.size() > 0) {
067 UserGroup userGroup = userGroups.get(0);
068
069 Group group = userGroup.getGroup();
070
071 entityGroupId = group.getGroupId();
072 }
073 }
074 else if (entityName.equals("organization")) {
075 List<Organization> organizations =
076 OrganizationLocalServiceUtil.search(
077 companyId,
078 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
079 null, null, null, null, null, null, null, true, 0, 1);
080
081 if (organizations.size() > 0) {
082 Organization organization = organizations.get(0);
083
084 Group group = organization.getGroup();
085
086 entityGroupId = group.getGroupId();
087 }
088 }
089
090 entityGroupIdMap.put(entityName, entityGroupId);
091 }
092 else {
093 entityGroupId = entityGroupIdObj.longValue();
094 }
095
096 return entityGroupId;
097 }
098
099 protected Map<String, Long> getEntityMap(long companyId, String entityName)
100 throws PortalException, SystemException {
101
102 Map<String, Long> entityMap = entityMapMap.get(entityName);
103
104 if (entityMap == null) {
105 entityMap = new HashMap<String, Long>();
106
107 if (entityName.equals("user-group")) {
108 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
109 companyId, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
110 null);
111
112 for (int i = 0; i < userGroups.size(); i++) {
113 UserGroup userGroup = userGroups.get(i);
114
115 Group group = userGroup.getGroup();
116
117 entityMap.put(userGroup.getName(), group.getGroupId());
118 }
119 }
120 else if (entityName.equals("organization")) {
121 List<Organization> organizations =
122 OrganizationLocalServiceUtil.search(
123 companyId,
124 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
125 OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
126 null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
127
128 for (int i = 0; i < organizations.size(); i++) {
129 Organization organization = organizations.get(i);
130
131 Group group = organization.getGroup();
132
133 entityMap.put(organization.getName(), group.getGroupId());
134 }
135 }
136
137 entityMapMap.put(entityName, entityMap);
138 }
139
140 return entityMap;
141 }
142
143 protected List<Role> getGroupRoles_1to4(long groupId)
144 throws SystemException {
145
146 List<Role> roles = groupRolesMap.get(groupId);
147
148 if (roles == null) {
149 roles = RoleLocalServiceUtil.getGroupRoles(groupId);
150
151 groupRolesMap.put(groupId, roles);
152 }
153
154 return roles;
155 }
156
157 protected List<Role> getGroupRoles_5(long groupId, String resourceName)
158 throws PortalException, SystemException {
159
160 List<Role> roles = groupRolesMap.get(groupId);
161
162 if (roles != null) {
163 return roles;
164 }
165
166 Group group = GroupLocalServiceUtil.getGroup(groupId);
167
168 roles = ResourceActionsUtil.getRoles(
169 group.getCompanyId(), group, resourceName, null);
170
171 List<Team> teams = TeamLocalServiceUtil.getGroupTeams(groupId);
172
173 for (Team team : teams) {
174 Role teamRole = RoleLocalServiceUtil.getTeamRole(
175 group.getCompanyId(), team.getTeamId());
176
177 teamRole.setName(
178 PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
179 teamRole.setDescription(team.getDescription());
180
181 roles.add(teamRole);
182 }
183
184 groupRolesMap.put(groupId, roles);
185
186 return roles;
187 }
188
189 protected List<User> getGroupUsers(long groupId) throws SystemException {
190 List<User> users = groupUsersMap.get(groupId);
191
192 if (users == null) {
193 users = UserLocalServiceUtil.getGroupUsers(groupId);
194
195 groupUsersMap.put(groupId, users);
196 }
197
198 return users;
199 }
200
201 protected Resource getResource(
202 long companyId, long groupId, String resourceName, int scope,
203 String resourcePrimKey, boolean portletActions)
204 throws PortalException, SystemException {
205
206 StringBundler sb = new StringBundler(5);
207
208 sb.append(resourceName);
209 sb.append(StringPool.PIPE);
210 sb.append(scope);
211 sb.append(StringPool.PIPE);
212 sb.append(resourcePrimKey);
213
214 String key = sb.toString();
215
216 Resource resource = resourcesMap.get(key);
217
218 if (resource == null) {
219 try {
220 resource = ResourceLocalServiceUtil.getResource(
221 companyId, resourceName, scope, resourcePrimKey);
222 }
223 catch (NoSuchResourceException nsre) {
224 ResourceLocalServiceUtil.addResources(
225 companyId, groupId, 0, resourceName, resourcePrimKey,
226 portletActions, true, true);
227
228 resource = ResourceLocalServiceUtil.getResource(
229 companyId, resourceName, scope, resourcePrimKey);
230 }
231
232 resourcesMap.put(key, resource);
233 }
234
235 return resource;
236 }
237
238 protected Role getRole(long companyId, String roleName)
239 throws PortalException, SystemException {
240
241 Role role = rolesMap.get(roleName);
242
243 if (role == null) {
244 try {
245 role = RoleLocalServiceUtil.getRole(companyId, roleName);
246
247 rolesMap.put(roleName, role);
248 }
249 catch (NoSuchRoleException nsre) {
250 }
251 }
252
253 return role;
254 }
255
256 protected User getUser(long companyId, long groupId, String uuid)
257 throws SystemException {
258
259 List<User> users = usersMap.get(uuid);
260
261 if (users == null) {
262 LinkedHashMap<String, Object> params =
263 new LinkedHashMap<String, Object>();
264
265 params.put("usersGroups", new Long(groupId));
266
267 try {
268 User user = UserLocalServiceUtil.getUserByUuid(uuid);
269
270 users = UserLocalServiceUtil.search(
271 companyId, null, null, null, user.getScreenName(), null,
272 WorkflowConstants.STATUS_APPROVED, params, true, 0, 1,
273 (OrderByComparator)null);
274
275 }
276 catch (PortalException pe) {
277 }
278
279 usersMap.put(uuid, users);
280 }
281
282 if (users.size() == 0) {
283 return null;
284 }
285 else {
286 return users.get(0);
287 }
288 }
289
290 protected List<Role> getUserRoles(long userId) throws SystemException {
291 List<Role> userRoles = userRolesMap.get(userId);
292
293 if (userRoles == null) {
294 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
295
296 userRolesMap.put(userId, userRoles);
297 }
298
299 return userRoles;
300 }
301
302 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
303 protected Map<String, Map<String, Long>> entityMapMap =
304 new HashMap<String, Map<String, Long>>();
305 protected Map<Long, List<Role>> groupRolesMap =
306 new HashMap<Long, List<Role>>();
307 protected Map<Long, List<User>> groupUsersMap =
308 new HashMap<Long, List<User>>();
309 protected Map<String, Resource> resourcesMap =
310 new HashMap<String, Resource>();
311 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
312 protected Map<Long, List<Role>> userRolesMap =
313 new HashMap<Long, List<Role>>();
314 protected Map<String, List<User>> usersMap =
315 new HashMap<String, List<User>>();
316
317 }