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