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.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_1to4(long groupId)
143 throws SystemException {
144
145 List<Role> roles = groupRolesMap.get(groupId);
146
147 if (roles == null) {
148 roles = RoleLocalServiceUtil.getGroupRoles(groupId);
149
150 groupRolesMap.put(groupId, roles);
151 }
152
153 return roles;
154 }
155
156 protected List<Role> getGroupRoles_5(long groupId, String resourceName)
157 throws PortalException, SystemException {
158
159 List<Role> roles = groupRolesMap.get(groupId);
160
161 if (roles == null) {
162 Group group = GroupLocalServiceUtil.getGroup(groupId);
163
164 roles = ResourceActionsUtil.getRoles(
165 group.getCompanyId(), group, resourceName);
166
167 groupRolesMap.put(groupId, roles);
168 }
169
170 return roles;
171 }
172
173 protected List<User> getGroupUsers(long groupId) throws SystemException {
174 List<User> users = groupUsersMap.get(groupId);
175
176 if (users == null) {
177 users = UserLocalServiceUtil.getGroupUsers(groupId);
178
179 groupUsersMap.put(groupId, users);
180 }
181
182 return users;
183 }
184
185 protected Resource getResource(
186 long companyId, long groupId, String resourceName, int scope,
187 String resourcePrimKey, boolean portletActions)
188 throws PortalException, SystemException {
189
190 StringBundler sb = new StringBundler(5);
191
192 sb.append(resourceName);
193 sb.append(StringPool.PIPE);
194 sb.append(scope);
195 sb.append(StringPool.PIPE);
196 sb.append(resourcePrimKey);
197
198 String key = sb.toString();
199
200 Resource resource = resourcesMap.get(key);
201
202 if (resource == null) {
203 try {
204 resource = ResourceLocalServiceUtil.getResource(
205 companyId, resourceName, scope, resourcePrimKey);
206 }
207 catch (NoSuchResourceException nsre) {
208 ResourceLocalServiceUtil.addResources(
209 companyId, groupId, 0, resourceName, resourcePrimKey,
210 portletActions, true, true);
211
212 resource = ResourceLocalServiceUtil.getResource(
213 companyId, resourceName, scope, resourcePrimKey);
214 }
215
216 resourcesMap.put(key, resource);
217 }
218
219 return resource;
220 }
221
222 protected Role getRole(long companyId, String roleName)
223 throws PortalException, SystemException {
224
225 Role role = rolesMap.get(roleName);
226
227 if (role == null) {
228 try {
229 role = RoleLocalServiceUtil.getRole(companyId, roleName);
230
231 rolesMap.put(roleName, role);
232 }
233 catch (NoSuchRoleException nsre) {
234 }
235 }
236
237 return role;
238 }
239
240 protected User getUser(long companyId, long groupId, String uuid)
241 throws SystemException {
242
243 List<User> users = usersMap.get(uuid);
244
245 if (users == null) {
246 LinkedHashMap<String, Object> params =
247 new LinkedHashMap<String, Object>();
248
249 params.put("usersGroups", new Long(groupId));
250
251 try {
252 User user = UserLocalServiceUtil.getUserByUuid(uuid);
253
254 users = UserLocalServiceUtil.search(
255 companyId, null, null, null, user.getScreenName(), null,
256 Boolean.TRUE, params, true, 0, 1, (OrderByComparator)null);
257
258 }
259 catch (PortalException pe) {
260 }
261
262 usersMap.put(uuid, users);
263 }
264
265 if (users.size() == 0) {
266 return null;
267 }
268 else {
269 return users.get(0);
270 }
271 }
272
273 protected List<Role> getUserRoles(long userId) throws SystemException {
274 List<Role> userRoles = userRolesMap.get(userId);
275
276 if (userRoles == null) {
277 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
278
279 userRolesMap.put(userId, userRoles);
280 }
281
282 return userRoles;
283 }
284
285 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
286 protected Map<String, Map<String, Long>> entityMapMap =
287 new HashMap<String, Map<String, Long>>();
288 protected Map<Long, List<Role>> groupRolesMap =
289 new HashMap<Long, List<Role>>();
290 protected Map<Long, List<User>> groupUsersMap =
291 new HashMap<Long, List<User>>();
292 protected Map<String, Resource> resourcesMap =
293 new HashMap<String, Resource>();
294 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
295 protected Map<Long, List<Role>> userRolesMap =
296 new HashMap<Long, List<Role>>();
297 protected Map<String, List<User>> usersMap =
298 new HashMap<String, List<User>>();
299
300 }