001    /**
002     * Copyright (c) 2000-2012 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.NoSuchRoleException;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Organization;
023    import com.liferay.portal.model.OrganizationConstants;
024    import com.liferay.portal.model.Role;
025    import com.liferay.portal.model.Team;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.model.UserGroup;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.OrganizationLocalServiceUtil;
031    import com.liferay.portal.service.RoleLocalServiceUtil;
032    import com.liferay.portal.service.TeamLocalServiceUtil;
033    import com.liferay.portal.service.UserGroupLocalServiceUtil;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Map;
039    
040    /**
041     * @author Charles May
042     */
043    public class LayoutCache {
044    
045            protected long getEntityGroupId(
046                            long companyId, String entityName, String name)
047                    throws PortalException, SystemException {
048    
049                    long entityGroupId = 0;
050    
051                    Long entityGroupIdObj = entityGroupIdMap.get(entityName);
052    
053                    if (entityGroupIdObj == null) {
054                            if (entityName.equals("user-group")) {
055                                    List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
056                                            companyId, null, null, 0, 1, null);
057    
058                                    if (userGroups.size() > 0) {
059                                            UserGroup userGroup = userGroups.get(0);
060    
061                                            Group group = userGroup.getGroup();
062    
063                                            entityGroupId = group.getGroupId();
064                                    }
065                            }
066                            else if (entityName.equals("organization")) {
067                                    List<Organization> organizations =
068                                            OrganizationLocalServiceUtil.search(
069                                                    companyId,
070                                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
071                                                    null, null, null, null, null, null, null, true, 0, 1);
072    
073                                    if (organizations.size() > 0) {
074                                            Organization organization = organizations.get(0);
075    
076                                            Group group = organization.getGroup();
077    
078                                            entityGroupId = group.getGroupId();
079                                    }
080                            }
081    
082                            entityGroupIdMap.put(entityName, entityGroupId);
083                    }
084                    else {
085                            entityGroupId = entityGroupIdObj.longValue();
086                    }
087    
088                    return entityGroupId;
089            }
090    
091            protected Map<String, Long> getEntityMap(long companyId, String entityName)
092                    throws PortalException, SystemException {
093    
094                    Map<String, Long> entityMap = entityMapMap.get(entityName);
095    
096                    if (entityMap == null) {
097                            entityMap = new HashMap<String, Long>();
098    
099                            if (entityName.equals("user-group")) {
100                                    List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
101                                            companyId, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
102                                            null);
103    
104                                    for (int i = 0; i < userGroups.size(); i++) {
105                                            UserGroup userGroup = userGroups.get(i);
106    
107                                            Group group = userGroup.getGroup();
108    
109                                            entityMap.put(userGroup.getName(), group.getGroupId());
110                                    }
111                            }
112                            else if (entityName.equals("organization")) {
113                                    List<Organization> organizations =
114                                            OrganizationLocalServiceUtil.search(
115                                                    companyId,
116                                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
117                                                    OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
118                                                    null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
119    
120                                    for (int i = 0; i < organizations.size(); i++) {
121                                            Organization organization = organizations.get(i);
122    
123                                            Group group = organization.getGroup();
124    
125                                            entityMap.put(organization.getName(), group.getGroupId());
126                                    }
127                            }
128    
129                            entityMapMap.put(entityName, entityMap);
130                    }
131    
132                    return entityMap;
133            }
134    
135            protected List<Role> getGroupRoles_1to4(long groupId)
136                    throws SystemException {
137    
138                    List<Role> roles = groupRolesMap.get(groupId);
139    
140                    if (roles == null) {
141                            roles = RoleLocalServiceUtil.getGroupRoles(groupId);
142    
143                            groupRolesMap.put(groupId, roles);
144                    }
145    
146                    return roles;
147            }
148    
149            protected List<Role> getGroupRoles_5(long groupId, String resourceName)
150                    throws PortalException, SystemException {
151    
152                    List<Role> roles = groupRolesMap.get(groupId);
153    
154                    if (roles == null) {
155                            Group group = GroupLocalServiceUtil.getGroup(groupId);
156    
157                            roles = ResourceActionsUtil.getRoles(
158                                    group.getCompanyId(), group, resourceName, null);
159    
160                            List<Team> teams = TeamLocalServiceUtil.getGroupTeams(groupId);
161    
162                            for (Team team : teams) {
163                                    Role teamRole = RoleLocalServiceUtil.getTeamRole(
164                                            group.getCompanyId(), team.getTeamId());
165    
166                                    teamRole.setName(
167                                            PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
168                                    teamRole.setDescription(team.getDescription());
169    
170                                    roles.add(teamRole);
171                            }
172    
173                            groupRolesMap.put(groupId, roles);
174                    }
175    
176                    return roles;
177            }
178    
179            protected List<User> getGroupUsers(long groupId) throws SystemException {
180                    List<User> users = groupUsersMap.get(groupId);
181    
182                    if (users == null) {
183                            users = UserLocalServiceUtil.getGroupUsers(groupId);
184    
185                            groupUsersMap.put(groupId, users);
186                    }
187    
188                    return users;
189            }
190    
191            protected Role getRole(long companyId, String roleName)
192                    throws PortalException, SystemException {
193    
194                    Role role = rolesMap.get(roleName);
195    
196                    if (role == null) {
197                            try {
198                                    role = RoleLocalServiceUtil.getRole(companyId, roleName);
199    
200                                    rolesMap.put(roleName, role);
201                            }
202                            catch (NoSuchRoleException nsre) {
203                            }
204                    }
205    
206                    return role;
207            }
208    
209            protected List<Role> getUserRoles(long userId) throws SystemException {
210                    List<Role> userRoles = userRolesMap.get(userId);
211    
212                    if (userRoles == null) {
213                            userRoles = RoleLocalServiceUtil.getUserRoles(userId);
214    
215                            userRolesMap.put(userId, userRoles);
216                    }
217    
218                    return userRoles;
219            }
220    
221            protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
222            protected Map<String, Map<String, Long>> entityMapMap =
223                    new HashMap<String, Map<String, Long>>();
224            protected Map<Long, List<Role>> groupRolesMap =
225                    new HashMap<Long, List<Role>>();
226            protected Map<Long, List<User>> groupUsersMap =
227                    new HashMap<Long, List<User>>();
228            protected Map<String, Role> rolesMap = new HashMap<String, Role>();
229            protected Map<Long, List<Role>> userRolesMap =
230                    new HashMap<Long, List<Role>>();
231    
232    }