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)
173         throws PortalException, SystemException {
174 
175         List<Role> roles = groupRolesMap.get(groupId);
176 
177         if (roles == null) {
178             roles = RoleLocalServiceUtil.getGroupRoles(groupId);
179 
180             groupRolesMap.put(groupId, roles);
181         }
182 
183         return roles;
184     }
185 
186     protected List<User> getGroupUsers(long groupId)
187         throws PortalException, SystemException {
188 
189         List<User> users = groupUsersMap.get(groupId);
190 
191         if (users == null) {
192             users = UserLocalServiceUtil.getGroupUsers(groupId);
193 
194             groupUsersMap.put(groupId, users);
195         }
196 
197         return users;
198     }
199 
200     protected Resource getResource(
201             long companyId, long groupId, String resourceName, int scope,
202             String resourcePrimKey, boolean portletActions)
203         throws PortalException, SystemException {
204 
205         StringBuilder sb = new StringBuilder();
206 
207         sb.append(resourceName);
208         sb.append(StringPool.PIPE);
209         sb.append(scope);
210         sb.append(StringPool.PIPE);
211         sb.append(resourcePrimKey);
212 
213         String key = sb.toString();
214 
215         Resource resource = resourcesMap.get(key);
216 
217         if (resource == null) {
218             try {
219                 resource = ResourceLocalServiceUtil.getResource(
220                     companyId, resourceName, scope, resourcePrimKey);
221             }
222             catch (NoSuchResourceException nsre) {
223                 ResourceLocalServiceUtil.addResources(
224                     companyId, groupId, 0, resourceName, resourcePrimKey,
225                     portletActions, true, true);
226 
227                 resource = ResourceLocalServiceUtil.getResource(
228                     companyId, resourceName, scope, resourcePrimKey);
229             }
230 
231             resourcesMap.put(key, resource);
232         }
233 
234         return resource;
235     }
236 
237     protected Role getRole(long companyId, String roleName)
238         throws PortalException, SystemException {
239 
240         Role role = rolesMap.get(roleName);
241 
242         if (role == null) {
243             try {
244                 role = RoleLocalServiceUtil.getRole(companyId, roleName);
245 
246                 rolesMap.put(roleName, role);
247             }
248             catch (NoSuchRoleException nsre) {
249             }
250         }
251 
252         return role;
253     }
254 
255     protected User getUser(long companyId, long groupId, String emailAddress)
256         throws SystemException {
257 
258         List<User> users = usersMap.get(emailAddress);
259 
260         if (users == null) {
261             LinkedHashMap<String, Object> params =
262                 new LinkedHashMap<String, Object>();
263 
264             params.put("usersGroups", new Long(groupId));
265 
266             users = UserLocalServiceUtil.search(
267                 companyId, null, null, null, null, emailAddress, Boolean.TRUE,
268                 params, true, 0, 1, null);
269 
270             usersMap.put(emailAddress, users);
271         }
272 
273         if (users.size() == 0) {
274             return null;
275         }
276         else {
277             return users.get(0);
278         }
279     }
280 
281     protected List<Role> getUserRoles(long userId)
282         throws PortalException, SystemException {
283 
284         List<Role> userRoles = userRolesMap.get(userId);
285 
286         if (userRoles == null) {
287             userRoles = RoleLocalServiceUtil.getUserRoles(userId);
288 
289             userRolesMap.put(userId, userRoles);
290         }
291 
292         return userRoles;
293     }
294 
295     protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
296     protected Map<String, Map<String, Long>> entityMapMap =
297         new HashMap<String, Map<String, Long>>();
298     protected Map<Long, List<Role>> groupRolesMap =
299         new HashMap<Long, List<Role>>();
300     protected Map<Long, List<User>> groupUsersMap =
301         new HashMap<Long, List<User>>();
302     protected Map<String, Resource> resourcesMap =
303         new HashMap<String, Resource>();
304     protected Map<String, Role> rolesMap = new HashMap<String, Role>();
305     protected Map<Long, List<Role>> userRolesMap =
306         new HashMap<Long, List<Role>>();
307     protected Map<String, List<User>> usersMap =
308         new HashMap<String, List<User>>();
309 
310 }