001
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
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 return entityMap;
099 }
100
101 entityMap = new HashMap<String, Long>();
102
103 if (entityName.equals("user-group")) {
104 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
105 companyId, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
106 (OrderByComparator)null);
107
108 for (int i = 0; i < userGroups.size(); i++) {
109 UserGroup userGroup = userGroups.get(i);
110
111 Group group = userGroup.getGroup();
112
113 entityMap.put(userGroup.getName(), group.getGroupId());
114 }
115 }
116 else if (entityName.equals("organization")) {
117 List<Organization> organizations =
118 OrganizationLocalServiceUtil.search(
119 companyId, OrganizationConstants.ANY_PARENT_ORGANIZATION_ID,
120 null, OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
121 null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
122
123 for (int i = 0; i < organizations.size(); i++) {
124 Organization organization = organizations.get(i);
125
126 Group group = organization.getGroup();
127
128 entityMap.put(organization.getName(), group.getGroupId());
129 }
130 }
131
132 entityMapMap.put(entityName, entityMap);
133
134 return entityMap;
135 }
136
137 protected List<Role> getGroupRoles_1to4(long groupId)
138 throws SystemException {
139
140 List<Role> roles = groupRolesMap.get(groupId);
141
142 if (roles == null) {
143 roles = RoleLocalServiceUtil.getGroupRoles(groupId);
144
145 groupRolesMap.put(groupId, roles);
146 }
147
148 return roles;
149 }
150
151 protected List<Role> getGroupRoles_5(long groupId, String resourceName)
152 throws PortalException, SystemException {
153
154 List<Role> roles = groupRolesMap.get(groupId);
155
156 if (roles != null) {
157 return roles;
158 }
159
160 Group group = GroupLocalServiceUtil.getGroup(groupId);
161
162 roles = ResourceActionsUtil.getRoles(
163 group.getCompanyId(), group, resourceName, null);
164
165 List<Team> teams = TeamLocalServiceUtil.getGroupTeams(groupId);
166
167 for (Team team : teams) {
168 Role teamRole = RoleLocalServiceUtil.getTeamRole(
169 group.getCompanyId(), team.getTeamId());
170
171 teamRole.setName(
172 PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
173 teamRole.setDescription(team.getDescription());
174
175 roles.add(teamRole);
176 }
177
178 groupRolesMap.put(groupId, roles);
179
180 return roles;
181 }
182
183 protected List<User> getGroupUsers(long groupId) throws SystemException {
184 List<User> users = groupUsersMap.get(groupId);
185
186 if (users == null) {
187 users = UserLocalServiceUtil.getGroupUsers(groupId);
188
189 groupUsersMap.put(groupId, users);
190 }
191
192 return users;
193 }
194
195 protected Role getRole(long companyId, String roleName)
196 throws PortalException, SystemException {
197
198 Role role = rolesMap.get(roleName);
199
200 if (role == null) {
201 try {
202 role = RoleLocalServiceUtil.getRole(companyId, roleName);
203
204 rolesMap.put(roleName, role);
205 }
206 catch (NoSuchRoleException nsre) {
207 }
208 }
209
210 return role;
211 }
212
213 protected List<Role> getUserRoles(long userId) throws SystemException {
214 List<Role> userRoles = userRolesMap.get(userId);
215
216 if (userRoles == null) {
217 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
218
219 userRolesMap.put(userId, userRoles);
220 }
221
222 return userRoles;
223 }
224
225 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
226 protected Map<String, Map<String, Long>> entityMapMap =
227 new HashMap<String, Map<String, Long>>();
228 protected Map<Long, List<Role>> groupRolesMap =
229 new HashMap<Long, List<Role>>();
230 protected Map<Long, List<User>> groupUsersMap =
231 new HashMap<Long, List<User>>();
232 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
233 protected Map<Long, List<Role>> userRolesMap =
234 new HashMap<Long, List<Role>>();
235
236 }