1
14
15 package com.liferay.portal.lar;
16
17 import com.liferay.portal.NoSuchResourceException;
18 import com.liferay.portal.NoSuchRoleException;
19 import com.liferay.portal.PortalException;
20 import com.liferay.portal.SystemException;
21 import com.liferay.portal.kernel.dao.orm.QueryUtil;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.model.Group;
25 import com.liferay.portal.model.Organization;
26 import com.liferay.portal.model.OrganizationConstants;
27 import com.liferay.portal.model.Resource;
28 import com.liferay.portal.model.Role;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.model.UserGroup;
31 import com.liferay.portal.security.permission.ResourceActionsUtil;
32 import com.liferay.portal.service.GroupLocalServiceUtil;
33 import com.liferay.portal.service.OrganizationLocalServiceUtil;
34 import com.liferay.portal.service.ResourceLocalServiceUtil;
35 import com.liferay.portal.service.RoleLocalServiceUtil;
36 import com.liferay.portal.service.UserGroupLocalServiceUtil;
37 import com.liferay.portal.service.UserLocalServiceUtil;
38
39 import java.util.HashMap;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43
44
49 public class LayoutCache {
50
51 protected long getEntityGroupId(
52 long companyId, String entityName, String name)
53 throws SystemException {
54
55 long entityGroupId = 0;
56
57 Long entityGroupIdObj = entityGroupIdMap.get(entityName);
58
59 if (entityGroupIdObj == null) {
60 if (entityName.equals("user-group")) {
61 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
62 companyId, name, null, null, 0, 1, null);
63
64 if (userGroups.size() > 0) {
65 UserGroup userGroup = userGroups.get(0);
66
67 Group group = userGroup.getGroup();
68
69 entityGroupId = group.getGroupId();
70 }
71 }
72 else if (entityName.equals("organization") ||
73 entityName.equals("location")) {
74
75 List<Organization> organizations = null;
76
77 if (entityName.equals("organization")) {
78 organizations = OrganizationLocalServiceUtil.search(
79 companyId,
80 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
81 OrganizationConstants.TYPE_REGULAR, null, null, null,
82 null, null, null, true, 0, 1);
83 }
84 else if (entityName.equals("location")) {
85 organizations = OrganizationLocalServiceUtil.search(
86 companyId,
87 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
88 OrganizationConstants.TYPE_LOCATION, null, null, null,
89 null, null, null, true, 0, 1);
90 }
91
92 if (organizations.size() > 0) {
93 Organization organization = organizations.get(0);
94
95 Group group = organization.getGroup();
96
97 entityGroupId = group.getGroupId();
98 }
99 }
100
101 entityGroupIdMap.put(entityName, entityGroupId);
102 }
103 else {
104 entityGroupId = entityGroupIdObj.longValue();
105 }
106
107 return entityGroupId;
108 }
109
110 protected Map<String, Long> getEntityMap(long companyId, String entityName)
111 throws SystemException {
112
113 Map<String, Long> entityMap = entityMapMap.get(entityName);
114
115 if (entityMap == null) {
116 entityMap = new HashMap<String, Long>();
117
118 if (entityName.equals("user-group")) {
119 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
120 companyId, null, null, null, QueryUtil.ALL_POS,
121 QueryUtil.ALL_POS, null);
122
123 for (int i = 0; i < userGroups.size(); i++) {
124 UserGroup userGroup = userGroups.get(i);
125
126 Group group = userGroup.getGroup();
127
128 entityMap.put(userGroup.getName(), group.getGroupId());
129 }
130 }
131 else if (entityName.equals("organization") ||
132 entityName.equals("location")) {
133
134 List<Organization> organizations = null;
135
136 if (entityName.equals("organization")) {
137 organizations = OrganizationLocalServiceUtil.search(
138 companyId,
139 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
140 OrganizationConstants.TYPE_REGULAR, null, null, null,
141 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
142 }
143 else if (entityName.equals("location")) {
144 organizations = OrganizationLocalServiceUtil.search(
145 companyId,
146 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
147 OrganizationConstants.TYPE_LOCATION, null, null, null,
148 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
149 }
150
151 for (int i = 0; i < organizations.size(); i++) {
152 Organization organization = organizations.get(i);
153
154 Group group = organization.getGroup();
155
156 entityMap.put(organization.getName(), group.getGroupId());
157 }
158 }
159
160 entityMapMap.put(entityName, entityMap);
161 }
162
163 return entityMap;
164 }
165
166 protected List<Role> getGroupRoles_4(long groupId) throws SystemException {
167 List<Role> roles = groupRolesMap.get(groupId);
168
169 if (roles == null) {
170 roles = RoleLocalServiceUtil.getGroupRoles(groupId);
171
172 groupRolesMap.put(groupId, roles);
173 }
174
175 return roles;
176 }
177
178 protected List<Role> getGroupRoles_5(long groupId, String resourceName)
179 throws PortalException, SystemException {
180
181 List<Role> roles = groupRolesMap.get(groupId);
182
183 if (roles == null) {
184 Group group = GroupLocalServiceUtil.getGroup(groupId);
185
186 roles = ResourceActionsUtil.getRoles(group, resourceName);
187
188 groupRolesMap.put(groupId, roles);
189 }
190
191 return roles;
192 }
193
194 protected List<User> getGroupUsers(long groupId) throws SystemException {
195 List<User> users = groupUsersMap.get(groupId);
196
197 if (users == null) {
198 users = UserLocalServiceUtil.getGroupUsers(groupId);
199
200 groupUsersMap.put(groupId, users);
201 }
202
203 return users;
204 }
205
206 protected Resource getResource(
207 long companyId, long groupId, String resourceName, int scope,
208 String resourcePrimKey, boolean portletActions)
209 throws PortalException, SystemException {
210
211 StringBundler sb = new StringBundler(5);
212
213 sb.append(resourceName);
214 sb.append(StringPool.PIPE);
215 sb.append(scope);
216 sb.append(StringPool.PIPE);
217 sb.append(resourcePrimKey);
218
219 String key = sb.toString();
220
221 Resource resource = resourcesMap.get(key);
222
223 if (resource == null) {
224 try {
225 resource = ResourceLocalServiceUtil.getResource(
226 companyId, resourceName, scope, resourcePrimKey);
227 }
228 catch (NoSuchResourceException nsre) {
229 ResourceLocalServiceUtil.addResources(
230 companyId, groupId, 0, resourceName, resourcePrimKey,
231 portletActions, true, true);
232
233 resource = ResourceLocalServiceUtil.getResource(
234 companyId, resourceName, scope, resourcePrimKey);
235 }
236
237 resourcesMap.put(key, resource);
238 }
239
240 return resource;
241 }
242
243 protected Role getRole(long companyId, String roleName)
244 throws PortalException, SystemException {
245
246 Role role = rolesMap.get(roleName);
247
248 if (role == null) {
249 try {
250 role = RoleLocalServiceUtil.getRole(companyId, roleName);
251
252 rolesMap.put(roleName, role);
253 }
254 catch (NoSuchRoleException nsre) {
255 }
256 }
257
258 return role;
259 }
260
261 protected User getUser(long companyId, long groupId, String emailAddress)
262 throws SystemException {
263
264 List<User> users = usersMap.get(emailAddress);
265
266 if (users == null) {
267 LinkedHashMap<String, Object> params =
268 new LinkedHashMap<String, Object>();
269
270 params.put("usersGroups", new Long(groupId));
271
272 users = UserLocalServiceUtil.search(
273 companyId, null, null, null, null, emailAddress, Boolean.TRUE,
274 params, true, 0, 1, null);
275
276 usersMap.put(emailAddress, users);
277 }
278
279 if (users.size() == 0) {
280 return null;
281 }
282 else {
283 return users.get(0);
284 }
285 }
286
287 protected List<Role> getUserRoles(long userId) throws SystemException {
288 List<Role> userRoles = userRolesMap.get(userId);
289
290 if (userRoles == null) {
291 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
292
293 userRolesMap.put(userId, userRoles);
294 }
295
296 return userRoles;
297 }
298
299 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
300 protected Map<String, Map<String, Long>> entityMapMap =
301 new HashMap<String, Map<String, Long>>();
302 protected Map<Long, List<Role>> groupRolesMap =
303 new HashMap<Long, List<Role>>();
304 protected Map<Long, List<User>> groupUsersMap =
305 new HashMap<Long, List<User>>();
306 protected Map<String, Resource> resourcesMap =
307 new HashMap<String, Resource>();
308 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
309 protected Map<Long, List<Role>> userRolesMap =
310 new HashMap<Long, List<Role>>();
311 protected Map<String, List<User>> usersMap =
312 new HashMap<String, List<User>>();
313
314 }