1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.lar;
21  
22  import com.liferay.portal.NoSuchResourceException;
23  import com.liferay.portal.NoSuchRoleException;
24  import com.liferay.portal.PortalException;
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.Organization;
30  import com.liferay.portal.model.OrganizationConstants;
31  import com.liferay.portal.model.Resource;
32  import com.liferay.portal.model.Role;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.model.UserGroup;
35  import com.liferay.portal.security.permission.ResourceActionsUtil;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.service.OrganizationLocalServiceUtil;
38  import com.liferay.portal.service.ResourceLocalServiceUtil;
39  import com.liferay.portal.service.RoleLocalServiceUtil;
40  import com.liferay.portal.service.UserGroupLocalServiceUtil;
41  import com.liferay.portal.service.UserLocalServiceUtil;
42  
43  import java.util.HashMap;
44  import java.util.LinkedHashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  /**
49   * <a href="LayoutCache.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Charles May
52   *
53   */
54  public class LayoutCache {
55  
56      protected long getEntityGroupId(
57              long companyId, String entityName, String name)
58          throws SystemException {
59  
60          long entityGroupId = 0;
61  
62          Long entityGroupIdObj = entityGroupIdMap.get(entityName);
63  
64          if (entityGroupIdObj == null) {
65              if (entityName.equals("user-group")) {
66                  List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
67                      companyId, name, null, null, 0, 1, null);
68  
69                  if (userGroups.size() > 0) {
70                      UserGroup userGroup = userGroups.get(0);
71  
72                      Group group = userGroup.getGroup();
73  
74                      entityGroupId = group.getGroupId();
75                  }
76              }
77              else if (entityName.equals("organization") ||
78                       entityName.equals("location")) {
79  
80                  List<Organization> organizations = null;
81  
82                  if (entityName.equals("organization")) {
83                      organizations = OrganizationLocalServiceUtil.search(
84                          companyId,
85                          OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
86                          OrganizationConstants.TYPE_REGULAR, null, null, null,
87                          null, null, null, true, 0, 1);
88                  }
89                  else if (entityName.equals("location")) {
90                      organizations = OrganizationLocalServiceUtil.search(
91                          companyId,
92                          OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
93                          OrganizationConstants.TYPE_LOCATION, null, null, null,
94                          null, null, null, true, 0, 1);
95                  }
96  
97                  if (organizations.size() > 0) {
98                      Organization organization = organizations.get(0);
99  
100                     Group group = organization.getGroup();
101 
102                     entityGroupId = group.getGroupId();
103                 }
104             }
105 
106             entityGroupIdMap.put(entityName, entityGroupId);
107         }
108         else {
109             entityGroupId = entityGroupIdObj.longValue();
110         }
111 
112         return entityGroupId;
113     }
114 
115     protected Map<String, Long> getEntityMap(long companyId, String entityName)
116         throws SystemException {
117 
118         Map<String, Long> entityMap = entityMapMap.get(entityName);
119 
120         if (entityMap == null) {
121             entityMap = new HashMap<String, Long>();
122 
123             if (entityName.equals("user-group")) {
124                 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
125                     companyId, null, null, null, QueryUtil.ALL_POS,
126                     QueryUtil.ALL_POS, null);
127 
128                 for (int i = 0; i < userGroups.size(); i++) {
129                     UserGroup userGroup = userGroups.get(i);
130 
131                     Group group = userGroup.getGroup();
132 
133                     entityMap.put(userGroup.getName(), group.getGroupId());
134                 }
135             }
136             else if (entityName.equals("organization") ||
137                      entityName.equals("location")) {
138 
139                 List<Organization> organizations = null;
140 
141                 if (entityName.equals("organization")) {
142                     organizations = OrganizationLocalServiceUtil.search(
143                         companyId,
144                         OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
145                         OrganizationConstants.TYPE_REGULAR, null, null, null,
146                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
147                 }
148                 else if (entityName.equals("location")) {
149                     organizations = OrganizationLocalServiceUtil.search(
150                         companyId,
151                         OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
152                         OrganizationConstants.TYPE_LOCATION, null, null, null,
153                         QueryUtil.ALL_POS, QueryUtil.ALL_POS);
154                 }
155 
156                 for (int i = 0; i < organizations.size(); i++) {
157                     Organization organization = organizations.get(i);
158 
159                     Group group = organization.getGroup();
160 
161                     entityMap.put(organization.getName(), group.getGroupId());
162                 }
163             }
164 
165             entityMapMap.put(entityName, entityMap);
166         }
167 
168         return entityMap;
169     }
170 
171     protected List<Role> getGroupRoles_4(long groupId) throws SystemException {
172         List<Role> roles = groupRolesMap.get(groupId);
173 
174         if (roles == null) {
175             roles = RoleLocalServiceUtil.getGroupRoles(groupId);
176 
177             groupRolesMap.put(groupId, roles);
178         }
179 
180         return roles;
181     }
182 
183     protected List<Role> getGroupRoles_5(long groupId, String resourceName)
184         throws PortalException, SystemException {
185 
186         List<Role> roles = groupRolesMap.get(groupId);
187 
188         if (roles == null) {
189             Group group = GroupLocalServiceUtil.getGroup(groupId);
190 
191             roles = ResourceActionsUtil.getRoles(group, resourceName);
192 
193             groupRolesMap.put(groupId, roles);
194         }
195 
196         return roles;
197     }
198 
199     protected List<User> getGroupUsers(long groupId) throws SystemException {
200         List<User> users = groupUsersMap.get(groupId);
201 
202         if (users == null) {
203             users = UserLocalServiceUtil.getGroupUsers(groupId);
204 
205             groupUsersMap.put(groupId, users);
206         }
207 
208         return users;
209     }
210 
211     protected Resource getResource(
212             long companyId, long groupId, String resourceName, int scope,
213             String resourcePrimKey, boolean portletActions)
214         throws PortalException, SystemException {
215 
216         StringBuilder sb = new StringBuilder();
217 
218         sb.append(resourceName);
219         sb.append(StringPool.PIPE);
220         sb.append(scope);
221         sb.append(StringPool.PIPE);
222         sb.append(resourcePrimKey);
223 
224         String key = sb.toString();
225 
226         Resource resource = resourcesMap.get(key);
227 
228         if (resource == null) {
229             try {
230                 resource = ResourceLocalServiceUtil.getResource(
231                     companyId, resourceName, scope, resourcePrimKey);
232             }
233             catch (NoSuchResourceException nsre) {
234                 ResourceLocalServiceUtil.addResources(
235                     companyId, groupId, 0, resourceName, resourcePrimKey,
236                     portletActions, true, true);
237 
238                 resource = ResourceLocalServiceUtil.getResource(
239                     companyId, resourceName, scope, resourcePrimKey);
240             }
241 
242             resourcesMap.put(key, resource);
243         }
244 
245         return resource;
246     }
247 
248     protected Role getRole(long companyId, String roleName)
249         throws PortalException, SystemException {
250 
251         Role role = rolesMap.get(roleName);
252 
253         if (role == null) {
254             try {
255                 role = RoleLocalServiceUtil.getRole(companyId, roleName);
256 
257                 rolesMap.put(roleName, role);
258             }
259             catch (NoSuchRoleException nsre) {
260             }
261         }
262 
263         return role;
264     }
265 
266     protected User getUser(long companyId, long groupId, String emailAddress)
267         throws SystemException {
268 
269         List<User> users = usersMap.get(emailAddress);
270 
271         if (users == null) {
272             LinkedHashMap<String, Object> params =
273                 new LinkedHashMap<String, Object>();
274 
275             params.put("usersGroups", new Long(groupId));
276 
277             users = UserLocalServiceUtil.search(
278                 companyId, null, null, null, null, emailAddress, Boolean.TRUE,
279                 params, true, 0, 1, null);
280 
281             usersMap.put(emailAddress, users);
282         }
283 
284         if (users.size() == 0) {
285             return null;
286         }
287         else {
288             return users.get(0);
289         }
290     }
291 
292     protected List<Role> getUserRoles(long userId) throws SystemException {
293         List<Role> userRoles = userRolesMap.get(userId);
294 
295         if (userRoles == null) {
296             userRoles = RoleLocalServiceUtil.getUserRoles(userId);
297 
298             userRolesMap.put(userId, userRoles);
299         }
300 
301         return userRoles;
302     }
303 
304     protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
305     protected Map<String, Map<String, Long>> entityMapMap =
306         new HashMap<String, Map<String, Long>>();
307     protected Map<Long, List<Role>> groupRolesMap =
308         new HashMap<Long, List<Role>>();
309     protected Map<Long, List<User>> groupUsersMap =
310         new HashMap<Long, List<User>>();
311     protected Map<String, Resource> resourcesMap =
312         new HashMap<String, Resource>();
313     protected Map<String, Role> rolesMap = new HashMap<String, Role>();
314     protected Map<Long, List<Role>> userRolesMap =
315         new HashMap<Long, List<Role>>();
316     protected Map<String, List<User>> usersMap =
317         new HashMap<String, List<User>>();
318 
319 }