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