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