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