001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar;
016    
017    import com.liferay.portal.NoSuchResourceException;
018    import com.liferay.portal.NoSuchRoleException;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.model.OrganizationConstants;
028    import com.liferay.portal.model.Resource;
029    import com.liferay.portal.model.Role;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.model.UserGroup;
032    import com.liferay.portal.security.permission.ResourceActionsUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.OrganizationLocalServiceUtil;
035    import com.liferay.portal.service.ResourceLocalServiceUtil;
036    import com.liferay.portal.service.RoleLocalServiceUtil;
037    import com.liferay.portal.service.UserGroupLocalServiceUtil;
038    import com.liferay.portal.service.UserLocalServiceUtil;
039    
040    import java.util.HashMap;
041    import java.util.LinkedHashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    /**
046     * @author Charles May
047     */
048    public class LayoutCache {
049    
050            protected long getEntityGroupId(
051                            long companyId, String entityName, String name)
052                    throws PortalException, SystemException {
053    
054                    long entityGroupId = 0;
055    
056                    Long entityGroupIdObj = entityGroupIdMap.get(entityName);
057    
058                    if (entityGroupIdObj == null) {
059                            if (entityName.equals("user-group")) {
060                                    List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
061                                            companyId, name, null, null, 0, 1, null);
062    
063                                    if (userGroups.size() > 0) {
064                                            UserGroup userGroup = userGroups.get(0);
065    
066                                            Group group = userGroup.getGroup();
067    
068                                            entityGroupId = group.getGroupId();
069                                    }
070                            }
071                            else if (entityName.equals("organization")) {
072                                    List<Organization> organizations =
073                                            OrganizationLocalServiceUtil.search(
074                                                    companyId,
075                                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
076                                                    null, null, null, null, null, null, null, true, 0, 1);
077    
078                                    if (organizations.size() > 0) {
079                                            Organization organization = organizations.get(0);
080    
081                                            Group group = organization.getGroup();
082    
083                                            entityGroupId = group.getGroupId();
084                                    }
085                            }
086    
087                            entityGroupIdMap.put(entityName, entityGroupId);
088                    }
089                    else {
090                            entityGroupId = entityGroupIdObj.longValue();
091                    }
092    
093                    return entityGroupId;
094            }
095    
096            protected Map<String, Long> getEntityMap(long companyId, String entityName)
097                    throws PortalException, SystemException {
098    
099                    Map<String, Long> entityMap = entityMapMap.get(entityName);
100    
101                    if (entityMap == null) {
102                            entityMap = new HashMap<String, Long>();
103    
104                            if (entityName.equals("user-group")) {
105                                    List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
106                                            companyId, null, null, null, QueryUtil.ALL_POS,
107                                            QueryUtil.ALL_POS, null);
108    
109                                    for (int i = 0; i < userGroups.size(); i++) {
110                                            UserGroup userGroup = userGroups.get(i);
111    
112                                            Group group = userGroup.getGroup();
113    
114                                            entityMap.put(userGroup.getName(), group.getGroupId());
115                                    }
116                            }
117                            else if (entityName.equals("organization")) {
118                                    List<Organization> organizations =
119                                            OrganizationLocalServiceUtil.search(
120                                                    companyId,
121                                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
122                                                    OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
123                                                    null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
124    
125                                    for (int i = 0; i < organizations.size(); i++) {
126                                            Organization organization = organizations.get(i);
127    
128                                            Group group = organization.getGroup();
129    
130                                            entityMap.put(organization.getName(), group.getGroupId());
131                                    }
132                            }
133    
134                            entityMapMap.put(entityName, entityMap);
135                    }
136    
137                    return entityMap;
138            }
139    
140            protected List<Role> getGroupRoles_1to4(long groupId)
141                    throws SystemException {
142    
143                    List<Role> roles = groupRolesMap.get(groupId);
144    
145                    if (roles == null) {
146                            roles = RoleLocalServiceUtil.getGroupRoles(groupId);
147    
148                            groupRolesMap.put(groupId, roles);
149                    }
150    
151                    return roles;
152            }
153    
154            protected List<Role> getGroupRoles_5(long groupId, String resourceName)
155                    throws PortalException, SystemException {
156    
157                    List<Role> roles = groupRolesMap.get(groupId);
158    
159                    if (roles == null) {
160                            Group group = GroupLocalServiceUtil.getGroup(groupId);
161    
162                            roles = ResourceActionsUtil.getRoles(
163                                    group.getCompanyId(), group, resourceName);
164    
165                            groupRolesMap.put(groupId, roles);
166                    }
167    
168                    return roles;
169            }
170    
171            protected List<User> getGroupUsers(long groupId) throws SystemException {
172                    List<User> users = groupUsersMap.get(groupId);
173    
174                    if (users == null) {
175                            users = UserLocalServiceUtil.getGroupUsers(groupId);
176    
177                            groupUsersMap.put(groupId, users);
178                    }
179    
180                    return users;
181            }
182    
183            protected Resource getResource(
184                            long companyId, long groupId, String resourceName, int scope,
185                            String resourcePrimKey, boolean portletActions)
186                    throws PortalException, SystemException {
187    
188                    StringBundler sb = new StringBundler(5);
189    
190                    sb.append(resourceName);
191                    sb.append(StringPool.PIPE);
192                    sb.append(scope);
193                    sb.append(StringPool.PIPE);
194                    sb.append(resourcePrimKey);
195    
196                    String key = sb.toString();
197    
198                    Resource resource = resourcesMap.get(key);
199    
200                    if (resource == null) {
201                            try {
202                                    resource = ResourceLocalServiceUtil.getResource(
203                                            companyId, resourceName, scope, resourcePrimKey);
204                            }
205                            catch (NoSuchResourceException nsre) {
206                                    ResourceLocalServiceUtil.addResources(
207                                            companyId, groupId, 0, resourceName, resourcePrimKey,
208                                            portletActions, true, true);
209    
210                                    resource = ResourceLocalServiceUtil.getResource(
211                                            companyId, resourceName, scope, resourcePrimKey);
212                            }
213    
214                            resourcesMap.put(key, resource);
215                    }
216    
217                    return resource;
218            }
219    
220            protected Role getRole(long companyId, String roleName)
221                    throws PortalException, SystemException {
222    
223                    Role role = rolesMap.get(roleName);
224    
225                    if (role == null) {
226                            try {
227                                    role = RoleLocalServiceUtil.getRole(companyId, roleName);
228    
229                                    rolesMap.put(roleName, role);
230                            }
231                            catch (NoSuchRoleException nsre) {
232                            }
233                    }
234    
235                    return role;
236            }
237    
238            protected User getUser(long companyId, long groupId, String uuid)
239                    throws SystemException {
240    
241                    List<User> users = usersMap.get(uuid);
242    
243                    if (users == null) {
244                            LinkedHashMap<String, Object> params =
245                                    new LinkedHashMap<String, Object>();
246    
247                            params.put("usersGroups", new Long(groupId));
248    
249                            try {
250                                    User user = UserLocalServiceUtil.getUserByUuid(uuid);
251    
252                                    users = UserLocalServiceUtil.search(
253                                            companyId, null, null, null, user.getScreenName(), null,
254                                            Boolean.TRUE, params, true, 0, 1, (OrderByComparator)null);
255    
256                            }
257                            catch (PortalException pe) {
258                            }
259    
260                            usersMap.put(uuid, users);
261                    }
262    
263                    if (users.size() == 0) {
264                            return null;
265                    }
266                    else {
267                            return users.get(0);
268                    }
269            }
270    
271            protected List<Role> getUserRoles(long userId) throws SystemException {
272                    List<Role> userRoles = userRolesMap.get(userId);
273    
274                    if (userRoles == null) {
275                            userRoles = RoleLocalServiceUtil.getUserRoles(userId);
276    
277                            userRolesMap.put(userId, userRoles);
278                    }
279    
280                    return userRoles;
281            }
282    
283            protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
284            protected Map<String, Map<String, Long>> entityMapMap =
285                    new HashMap<String, Map<String, Long>>();
286            protected Map<Long, List<Role>> groupRolesMap =
287                    new HashMap<Long, List<Role>>();
288            protected Map<Long, List<User>> groupUsersMap =
289                    new HashMap<Long, List<User>>();
290            protected Map<String, Resource> resourcesMap =
291                    new HashMap<String, Resource>();
292            protected Map<String, Role> rolesMap = new HashMap<String, Role>();
293            protected Map<Long, List<Role>> userRolesMap =
294                    new HashMap<Long, List<Role>>();
295            protected Map<String, List<User>> usersMap =
296                    new HashMap<String, List<User>>();
297    
298    }