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