1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.exception.PortalException;
21  import com.liferay.portal.kernel.exception.SystemException;
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  /**
46   * <a href="LayoutCache.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Charles May
49   */
50  public class LayoutCache {
51  
52      protected long getEntityGroupId(
53              long companyId, String entityName, String name)
54          throws PortalException, 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 PortalException, 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(group, resourceName);
165 
166             groupRolesMap.put(groupId, roles);
167         }
168 
169         return roles;
170     }
171 
172     protected List<User> getGroupUsers(long groupId) throws SystemException {
173         List<User> users = groupUsersMap.get(groupId);
174 
175         if (users == null) {
176             users = UserLocalServiceUtil.getGroupUsers(groupId);
177 
178             groupUsersMap.put(groupId, users);
179         }
180 
181         return users;
182     }
183 
184     protected Resource getResource(
185             long companyId, long groupId, String resourceName, int scope,
186             String resourcePrimKey, boolean portletActions)
187         throws PortalException, SystemException {
188 
189         StringBundler sb = new StringBundler(5);
190 
191         sb.append(resourceName);
192         sb.append(StringPool.PIPE);
193         sb.append(scope);
194         sb.append(StringPool.PIPE);
195         sb.append(resourcePrimKey);
196 
197         String key = sb.toString();
198 
199         Resource resource = resourcesMap.get(key);
200 
201         if (resource == null) {
202             try {
203                 resource = ResourceLocalServiceUtil.getResource(
204                     companyId, resourceName, scope, resourcePrimKey);
205             }
206             catch (NoSuchResourceException nsre) {
207                 ResourceLocalServiceUtil.addResources(
208                     companyId, groupId, 0, resourceName, resourcePrimKey,
209                     portletActions, true, true);
210 
211                 resource = ResourceLocalServiceUtil.getResource(
212                     companyId, resourceName, scope, resourcePrimKey);
213             }
214 
215             resourcesMap.put(key, resource);
216         }
217 
218         return resource;
219     }
220 
221     protected Role getRole(long companyId, String roleName)
222         throws PortalException, SystemException {
223 
224         Role role = rolesMap.get(roleName);
225 
226         if (role == null) {
227             try {
228                 role = RoleLocalServiceUtil.getRole(companyId, roleName);
229 
230                 rolesMap.put(roleName, role);
231             }
232             catch (NoSuchRoleException nsre) {
233             }
234         }
235 
236         return role;
237     }
238 
239     protected User getUser(long companyId, long groupId, String uuid)
240         throws SystemException {
241 
242         List<User> users = usersMap.get(uuid);
243 
244         if (users == null) {
245             LinkedHashMap<String, Object> params =
246                 new LinkedHashMap<String, Object>();
247 
248             params.put("usersGroups", new Long(groupId));
249 
250             try {
251                 User user = UserLocalServiceUtil.getUserByUuid(uuid);
252 
253                 users = UserLocalServiceUtil.search(
254                     companyId, null, null, null, user.getScreenName(), null,
255                     Boolean.TRUE, params, true, 0, 1, (OrderByComparator)null);
256 
257             }
258             catch (PortalException pe) {
259             }
260 
261             usersMap.put(uuid, users);
262         }
263 
264         if (users.size() == 0) {
265             return null;
266         }
267         else {
268             return users.get(0);
269         }
270     }
271 
272     protected List<Role> getUserRoles(long userId) throws SystemException {
273         List<Role> userRoles = userRolesMap.get(userId);
274 
275         if (userRoles == null) {
276             userRoles = RoleLocalServiceUtil.getUserRoles(userId);
277 
278             userRolesMap.put(userId, userRoles);
279         }
280 
281         return userRoles;
282     }
283 
284     protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
285     protected Map<String, Map<String, Long>> entityMapMap =
286         new HashMap<String, Map<String, Long>>();
287     protected Map<Long, List<Role>> groupRolesMap =
288         new HashMap<Long, List<Role>>();
289     protected Map<Long, List<User>> groupUsersMap =
290         new HashMap<Long, List<User>>();
291     protected Map<String, Resource> resourcesMap =
292         new HashMap<String, Resource>();
293     protected Map<String, Role> rolesMap = new HashMap<String, Role>();
294     protected Map<Long, List<Role>> userRolesMap =
295         new HashMap<Long, List<Role>>();
296     protected Map<String, List<User>> usersMap =
297         new HashMap<String, List<User>>();
298 
299 }