1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.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  
44  import java.util.HashMap;
45  import java.util.LinkedHashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  /**
50   * <a href="LayoutCache.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Charles May
53   *
54   */
55  public class LayoutCache {
56  
57      protected long getEntityGroupId(
58              long companyId, String entityName, String name)
59          throws SystemException {
60  
61          long entityGroupId = 0;
62  
63          Long entityGroupIdObj = entityGroupIdMap.get(entityName);
64  
65          if (entityGroupIdObj == null) {
66              if (entityName.equals("user-group")) {
67                  List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
68                      companyId, name, null, null, 0, 1, null);
69  
70                  if (userGroups.size() > 0) {
71                      UserGroup userGroup = userGroups.get(0);
72  
73                      Group group = userGroup.getGroup();
74  
75                      entityGroupId = group.getGroupId();
76                  }
77              }
78              else if (entityName.equals("organization") ||
79                       entityName.equals("location")) {
80  
81                  List<Organization> organizations = null;
82  
83                  if (entityName.equals("organization")) {
84                      organizations = OrganizationLocalServiceUtil.search(
85                          companyId,
86                          OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
87                          OrganizationConstants.TYPE_REGULAR, null, null, null,
88                          null, null, null, true, 0, 1);
89                  }
90                  else if (entityName.equals("location")) {
91                      organizations = OrganizationLocalServiceUtil.search(
92                          companyId,
93                          OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
94                          OrganizationConstants.TYPE_LOCATION, null, null, null,
95                          null, null, null, true, 0, 1);
96                  }
97  
98                  if (organizations.size() > 0) {
99                      Organization organization = organizations.get(0);
100 
101                     Group group = organization.getGroup();
102 
103                     entityGroupId = group.getGroupId();
104                 }
105             }
106 
107             entityGroupIdMap.put(entityName, entityGroupId);
108         }
109         else {
110             entityGroupId = entityGroupIdObj.longValue();
111         }
112 
113         return entityGroupId;
114     }
115 
116     protected Map<String, Long> getEntityMap(long companyId, String entityName)
117         throws SystemException {
118 
119         Map<String, Long> entityMap = entityMapMap.get(entityName);
120 
121         if (entityMap == null) {
122             entityMap = new HashMap<String, Long>();
123 
124             if (entityName.equals("user-group")) {
125                 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
126                     companyId, null, null, null, QueryUtil.ALL_POS,
127                     QueryUtil.ALL_POS, null);
128 
129                 for (int i = 0; i < userGroups.size(); i++) {
130                     UserGroup userGroup = userGroups.get(i);
131 
132                     Group group = userGroup.getGroup();
133 
134                     entityMap.put(userGroup.getName(), group.getGroupId());
135                 }
136             }
137             else if (entityName.equals("organization") ||
138                      entityName.equals("location")) {
139 
140                 List<Organization> organizations = null;
141 
142                 if (entityName.equals("organization")) {
143                     organizations = OrganizationLocalServiceUtil.search(
144                         companyId,
145                         OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
146                         OrganizationConstants.TYPE_REGULAR, null, null, null,
147                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
148                 }
149                 else if (entityName.equals("location")) {
150                     organizations = OrganizationLocalServiceUtil.search(
151                         companyId,
152                         OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
153                         OrganizationConstants.TYPE_LOCATION, null, null, null,
154                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
155                 }
156 
157                 for (int i = 0; i < organizations.size(); i++) {
158                     Organization organization = organizations.get(i);
159 
160                     Group group = organization.getGroup();
161 
162                     entityMap.put(organization.getName(), group.getGroupId());
163                 }
164             }
165 
166             entityMapMap.put(entityName, entityMap);
167         }
168 
169         return entityMap;
170     }
171 
172     protected List<Role> getGroupRoles(long groupId) throws SystemException {
173         List<Role> roles = groupRolesMap.get(groupId);
174 
175         if (roles == null) {
176             roles = RoleLocalServiceUtil.getGroupRoles(groupId);
177 
178             groupRolesMap.put(groupId, roles);
179         }
180 
181         return roles;
182     }
183 
184     protected List<User> getGroupUsers(long groupId) throws SystemException {
185         List<User> users = groupUsersMap.get(groupId);
186 
187         if (users == null) {
188             users = UserLocalServiceUtil.getGroupUsers(groupId);
189 
190             groupUsersMap.put(groupId, users);
191         }
192 
193         return users;
194     }
195 
196     protected Resource getResource(
197             long companyId, long groupId, String resourceName, int scope,
198             String resourcePrimKey, boolean portletActions)
199         throws PortalException, SystemException {
200 
201         StringBuilder sb = new StringBuilder();
202 
203         sb.append(resourceName);
204         sb.append(StringPool.PIPE);
205         sb.append(scope);
206         sb.append(StringPool.PIPE);
207         sb.append(resourcePrimKey);
208 
209         String key = sb.toString();
210 
211         Resource resource = resourcesMap.get(key);
212 
213         if (resource == null) {
214             try {
215                 resource = ResourceLocalServiceUtil.getResource(
216                     companyId, resourceName, scope, resourcePrimKey);
217             }
218             catch (NoSuchResourceException nsre) {
219                 ResourceLocalServiceUtil.addResources(
220                     companyId, groupId, 0, resourceName, resourcePrimKey,
221                     portletActions, true, true);
222 
223                 resource = ResourceLocalServiceUtil.getResource(
224                     companyId, resourceName, scope, resourcePrimKey);
225             }
226 
227             resourcesMap.put(key, resource);
228         }
229 
230         return resource;
231     }
232 
233     protected Role getRole(long companyId, String roleName)
234         throws PortalException, SystemException {
235 
236         Role role = rolesMap.get(roleName);
237 
238         if (role == null) {
239             try {
240                 role = RoleLocalServiceUtil.getRole(companyId, roleName);
241 
242                 rolesMap.put(roleName, role);
243             }
244             catch (NoSuchRoleException nsre) {
245             }
246         }
247 
248         return role;
249     }
250 
251     protected User getUser(long companyId, long groupId, String emailAddress)
252         throws SystemException {
253 
254         List<User> users = usersMap.get(emailAddress);
255 
256         if (users == null) {
257             LinkedHashMap<String, Object> params =
258                 new LinkedHashMap<String, Object>();
259 
260             params.put("usersGroups", new Long(groupId));
261 
262             users = UserLocalServiceUtil.search(
263                 companyId, null, null, null, null, emailAddress, Boolean.TRUE,
264                 params, true, 0, 1, null);
265 
266             usersMap.put(emailAddress, 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 }