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