001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.NoSuchResourceException;
024: import com.liferay.portal.NoSuchRoleException;
025: import com.liferay.portal.PortalException;
026: import com.liferay.portal.SystemException;
027: import com.liferay.portal.kernel.util.StringMaker;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.model.Group;
030: import com.liferay.portal.model.Organization;
031: import com.liferay.portal.model.Resource;
032: import com.liferay.portal.model.Role;
033: import com.liferay.portal.model.User;
034: import com.liferay.portal.model.UserGroup;
035: import com.liferay.portal.model.impl.OrganizationImpl;
036: import com.liferay.portal.service.OrganizationLocalServiceUtil;
037: import com.liferay.portal.service.ResourceLocalServiceUtil;
038: import com.liferay.portal.service.RoleLocalServiceUtil;
039: import com.liferay.portal.service.UserGroupLocalServiceUtil;
040: import com.liferay.portal.service.UserLocalServiceUtil;
041: import com.liferay.util.CollectionFactory;
042: import com.liferay.util.dao.hibernate.QueryUtil;
043:
044: import java.util.HashMap;
045: import java.util.LinkedHashMap;
046: import java.util.List;
047: import java.util.Map;
048:
049: /**
050: * <a href="LayoutCache.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Charles May
053: *
054: */
055: public class LayoutCache {
056:
057: protected long getEntityGroupId(long companyId, String entityName,
058: String name) throws PortalException, SystemException {
059:
060: long entityGroupId = 0;
061:
062: Long entityGroupIdObj = (Long) entityGroupIdMap.get(entityName);
063:
064: if (entityGroupIdObj == null) {
065: if (entityName.equals("user-group")) {
066: List userGroups = UserGroupLocalServiceUtil.search(
067: companyId, name, null, null, 0, 1, null);
068:
069: if (userGroups.size() > 0) {
070: UserGroup userGroup = (UserGroup) userGroups.get(0);
071:
072: Group group = userGroup.getGroup();
073:
074: entityGroupId = group.getGroupId();
075: }
076: } else if (entityName.equals("organization")
077: || entityName.equals("location")) {
078:
079: List organizations = null;
080:
081: if (entityName.equals("organization")) {
082: organizations = OrganizationLocalServiceUtil
083: .search(
084: companyId,
085: OrganizationImpl.ANY_PARENT_ORGANIZATION_ID,
086: name,
087: OrganizationImpl.TYPE_REGULAR,
088: null, null, null, null, null, null,
089: true, 0, 1);
090: } else if (entityName.equals("location")) {
091: organizations = OrganizationLocalServiceUtil
092: .search(
093: companyId,
094: OrganizationImpl.ANY_PARENT_ORGANIZATION_ID,
095: name,
096: OrganizationImpl.TYPE_LOCATION,
097: null, null, null, null, null, null,
098: true, 0, 1);
099: }
100:
101: if (organizations.size() > 0) {
102: Organization organization = (Organization) organizations
103: .get(0);
104:
105: Group group = organization.getGroup();
106:
107: entityGroupId = group.getGroupId();
108: }
109: }
110:
111: entityGroupIdMap.put(entityName, entityGroupIdObj);
112: } else {
113: entityGroupId = entityGroupIdObj.longValue();
114: }
115:
116: return entityGroupId;
117: }
118:
119: protected Map getEntityMap(long companyId, String entityName)
120: throws PortalException, SystemException {
121:
122: Map entityMap = (Map) entityMapMap.get(entityName);
123:
124: if (entityMap == null) {
125: entityMap = new HashMap();
126:
127: if (entityName.equals("user-group")) {
128: List userGroups = UserGroupLocalServiceUtil.search(
129: companyId, null, null, null, QueryUtil.ALL_POS,
130: QueryUtil.ALL_POS, null);
131:
132: for (int i = 0; i < userGroups.size(); i++) {
133: UserGroup userGroup = (UserGroup) userGroups.get(i);
134:
135: Group group = userGroup.getGroup();
136:
137: entityMap.put(userGroup.getName(), new Long(group
138: .getGroupId()));
139: }
140: } else if (entityName.equals("organization")
141: || entityName.equals("location")) {
142:
143: List organizations = null;
144:
145: if (entityName.equals("organization")) {
146: organizations = OrganizationLocalServiceUtil
147: .search(
148: companyId,
149: OrganizationImpl.ANY_PARENT_ORGANIZATION_ID,
150: null,
151: OrganizationImpl.TYPE_REGULAR,
152: null, null, null,
153: QueryUtil.ALL_POS,
154: QueryUtil.ALL_POS);
155: } else if (entityName.equals("location")) {
156: organizations = OrganizationLocalServiceUtil
157: .search(
158: companyId,
159: OrganizationImpl.ANY_PARENT_ORGANIZATION_ID,
160: null,
161: OrganizationImpl.TYPE_LOCATION,
162: null, null, null,
163: QueryUtil.ALL_POS,
164: QueryUtil.ALL_POS);
165: }
166:
167: for (int i = 0; i < organizations.size(); i++) {
168: Organization organization = (Organization) organizations
169: .get(i);
170:
171: Group group = organization.getGroup();
172:
173: entityMap.put(organization.getName(), new Long(
174: group.getGroupId()));
175: }
176: }
177:
178: entityMapMap.put(entityName, entityMap);
179: }
180:
181: return entityMap;
182: }
183:
184: protected List getGroupRoles(long groupId) throws PortalException,
185: SystemException {
186:
187: Long groupIdObj = new Long(groupId);
188:
189: List roles = (List) groupRolesMap.get(groupIdObj);
190:
191: if (roles == null) {
192: roles = RoleLocalServiceUtil.getGroupRoles(groupId);
193:
194: groupRolesMap.put(groupIdObj, roles);
195: }
196:
197: return roles;
198: }
199:
200: protected List getGroupUsers(long groupId) throws PortalException,
201: SystemException {
202:
203: List users = (List) groupUsersMap.get(new Long(groupId));
204:
205: if (users == null) {
206: users = UserLocalServiceUtil.getGroupUsers(groupId);
207:
208: groupUsersMap.put(new Long(groupId), users);
209: }
210:
211: return users;
212: }
213:
214: protected Resource getResource(long companyId, long groupId,
215: String resourceName, int scope, String resourcePrimKey,
216: boolean portletActions) throws PortalException,
217: SystemException {
218:
219: StringMaker sm = new StringMaker();
220:
221: sm.append(resourceName);
222: sm.append(StringPool.PIPE);
223: sm.append(scope);
224: sm.append(StringPool.PIPE);
225: sm.append(resourcePrimKey);
226:
227: String key = sm.toString();
228:
229: Resource resource = (Resource) resourcesMap.get(key);
230:
231: if (resource == null) {
232: try {
233: resource = ResourceLocalServiceUtil
234: .getResource(companyId, resourceName, scope,
235: resourcePrimKey);
236: } catch (NoSuchResourceException nsre) {
237: ResourceLocalServiceUtil.addResources(companyId,
238: groupId, 0, resourceName, resourcePrimKey,
239: portletActions, true, true);
240:
241: resource = ResourceLocalServiceUtil
242: .getResource(companyId, resourceName, scope,
243: resourcePrimKey);
244: }
245:
246: resourcesMap.put(key, resource);
247: }
248:
249: return resource;
250: }
251:
252: protected Role getRole(long companyId, String roleName)
253: throws PortalException, SystemException {
254:
255: Role role = (Role) rolesMap.get(roleName);
256:
257: if (role == null) {
258: try {
259: role = RoleLocalServiceUtil
260: .getRole(companyId, roleName);
261:
262: rolesMap.put(roleName, role);
263: } catch (NoSuchRoleException nsre) {
264: }
265: }
266:
267: return role;
268: }
269:
270: protected User getUser(long companyId, long groupId,
271: String emailAddress) throws PortalException,
272: SystemException {
273:
274: List users = (List) usersMap.get(emailAddress);
275:
276: if (users == null) {
277: LinkedHashMap params = new LinkedHashMap();
278:
279: params.put("usersGroups", new Long(groupId));
280:
281: users = UserLocalServiceUtil.search(companyId, null, null,
282: null, null, emailAddress, Boolean.TRUE, params,
283: true, 0, 1, null);
284:
285: usersMap.put(emailAddress, users);
286: }
287:
288: if (users.size() == 0) {
289: return null;
290: } else {
291: return (User) users.get(0);
292: }
293: }
294:
295: protected List getUserRoles(long userId) throws PortalException,
296: SystemException {
297:
298: Long userIdObj = new Long(userId);
299:
300: List userRoles = (List) userRolesMap.get(userIdObj);
301:
302: if (userRoles == null) {
303: userRoles = RoleLocalServiceUtil.getUserRoles(userId);
304:
305: userRolesMap.put(userIdObj, userRoles);
306: }
307:
308: return userRoles;
309: }
310:
311: protected Map entityGroupIdMap = CollectionFactory.getHashMap();
312: protected Map entityMapMap = CollectionFactory.getHashMap();
313: protected Map groupRolesMap = CollectionFactory.getHashMap();
314: protected Map groupUsersMap = CollectionFactory.getHashMap();
315: protected Map resourcesMap = CollectionFactory.getHashMap();
316: protected Map rolesMap = CollectionFactory.getHashMap();
317: protected Map userRolesMap = CollectionFactory.getHashMap();
318: protected Map usersMap = CollectionFactory.getHashMap();
319:
320: }
|