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