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