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