001
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.kernel.workflow.WorkflowConstants;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.Organization;
028 import com.liferay.portal.model.OrganizationConstants;
029 import com.liferay.portal.model.Resource;
030 import com.liferay.portal.model.Role;
031 import com.liferay.portal.model.Team;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.model.UserGroup;
034 import com.liferay.portal.security.permission.ResourceActionsUtil;
035 import com.liferay.portal.service.GroupLocalServiceUtil;
036 import com.liferay.portal.service.OrganizationLocalServiceUtil;
037 import com.liferay.portal.service.ResourceLocalServiceUtil;
038 import com.liferay.portal.service.RoleLocalServiceUtil;
039 import com.liferay.portal.service.TeamLocalServiceUtil;
040 import com.liferay.portal.service.UserGroupLocalServiceUtil;
041 import com.liferay.portal.service.UserLocalServiceUtil;
042
043 import java.util.HashMap;
044 import java.util.LinkedHashMap;
045 import java.util.List;
046 import java.util.Map;
047
048
051 public class LayoutCache {
052
053 protected long getEntityGroupId(
054 long companyId, String entityName, String name)
055 throws PortalException, SystemException {
056
057 long entityGroupId = 0;
058
059 Long entityGroupIdObj = entityGroupIdMap.get(entityName);
060
061 if (entityGroupIdObj == null) {
062 if (entityName.equals("user-group")) {
063 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
064 companyId, null, null, 0, 1, null);
065
066 if (userGroups.size() > 0) {
067 UserGroup userGroup = userGroups.get(0);
068
069 Group group = userGroup.getGroup();
070
071 entityGroupId = group.getGroupId();
072 }
073 }
074 else if (entityName.equals("organization")) {
075 List<Organization> organizations =
076 OrganizationLocalServiceUtil.search(
077 companyId,
078 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, name,
079 null, null, null, null, null, null, null, true, 0, 1);
080
081 if (organizations.size() > 0) {
082 Organization organization = organizations.get(0);
083
084 Group group = organization.getGroup();
085
086 entityGroupId = group.getGroupId();
087 }
088 }
089
090 entityGroupIdMap.put(entityName, entityGroupId);
091 }
092 else {
093 entityGroupId = entityGroupIdObj.longValue();
094 }
095
096 return entityGroupId;
097 }
098
099 protected Map<String, Long> getEntityMap(long companyId, String entityName)
100 throws PortalException, SystemException {
101
102 Map<String, Long> entityMap = entityMapMap.get(entityName);
103
104 if (entityMap == null) {
105 entityMap = new HashMap<String, Long>();
106
107 if (entityName.equals("user-group")) {
108 List<UserGroup> userGroups = UserGroupLocalServiceUtil.search(
109 companyId, null, null, QueryUtil.ALL_POS,
110 QueryUtil.ALL_POS, null);
111
112 for (int i = 0; i < userGroups.size(); i++) {
113 UserGroup userGroup = userGroups.get(i);
114
115 Group group = userGroup.getGroup();
116
117 entityMap.put(userGroup.getName(), group.getGroupId());
118 }
119 }
120 else if (entityName.equals("organization")) {
121 List<Organization> organizations =
122 OrganizationLocalServiceUtil.search(
123 companyId,
124 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null,
125 OrganizationConstants.TYPE_REGULAR_ORGANIZATION, null,
126 null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
127
128 for (int i = 0; i < organizations.size(); i++) {
129 Organization organization = organizations.get(i);
130
131 Group group = organization.getGroup();
132
133 entityMap.put(organization.getName(), group.getGroupId());
134 }
135 }
136
137 entityMapMap.put(entityName, entityMap);
138 }
139
140 return entityMap;
141 }
142
143 protected List<Role> getGroupRoles_1to4(long groupId)
144 throws SystemException {
145
146 List<Role> roles = groupRolesMap.get(groupId);
147
148 if (roles == null) {
149 roles = RoleLocalServiceUtil.getGroupRoles(groupId);
150
151 groupRolesMap.put(groupId, roles);
152 }
153
154 return roles;
155 }
156
157 protected List<Role> getGroupRoles_5(long groupId, String resourceName)
158 throws PortalException, SystemException {
159
160 List<Role> roles = groupRolesMap.get(groupId);
161
162 if (roles == null) {
163 Group group = GroupLocalServiceUtil.getGroup(groupId);
164
165 roles = ResourceActionsUtil.getRoles(
166 group.getCompanyId(), group, resourceName, null);
167
168 List<Team> teams = TeamLocalServiceUtil.getGroupTeams(groupId);
169
170 for (Team team : teams) {
171 Role teamRole = RoleLocalServiceUtil.getTeamRole(
172 group.getCompanyId(), team.getTeamId());
173
174 teamRole.setName(
175 PermissionExporter.ROLE_TEAM_PREFIX + team.getName());
176 teamRole.setDescription(team.getDescription());
177
178 roles.add(teamRole);
179 }
180
181 groupRolesMap.put(groupId, roles);
182 }
183
184 return roles;
185 }
186
187 protected List<User> getGroupUsers(long groupId) throws SystemException {
188 List<User> users = groupUsersMap.get(groupId);
189
190 if (users == null) {
191 users = UserLocalServiceUtil.getGroupUsers(groupId);
192
193 groupUsersMap.put(groupId, users);
194 }
195
196 return users;
197 }
198
199 protected Resource getResource(
200 long companyId, long groupId, String resourceName, int scope,
201 String resourcePrimKey, boolean portletActions)
202 throws PortalException, SystemException {
203
204 StringBundler sb = new StringBundler(5);
205
206 sb.append(resourceName);
207 sb.append(StringPool.PIPE);
208 sb.append(scope);
209 sb.append(StringPool.PIPE);
210 sb.append(resourcePrimKey);
211
212 String key = sb.toString();
213
214 Resource resource = resourcesMap.get(key);
215
216 if (resource == null) {
217 try {
218 resource = ResourceLocalServiceUtil.getResource(
219 companyId, resourceName, scope, resourcePrimKey);
220 }
221 catch (NoSuchResourceException nsre) {
222 ResourceLocalServiceUtil.addResources(
223 companyId, groupId, 0, resourceName, resourcePrimKey,
224 portletActions, true, true);
225
226 resource = ResourceLocalServiceUtil.getResource(
227 companyId, resourceName, scope, resourcePrimKey);
228 }
229
230 resourcesMap.put(key, resource);
231 }
232
233 return resource;
234 }
235
236 protected Role getRole(long companyId, String roleName)
237 throws PortalException, SystemException {
238
239 Role role = rolesMap.get(roleName);
240
241 if (role == null) {
242 try {
243 role = RoleLocalServiceUtil.getRole(companyId, roleName);
244
245 rolesMap.put(roleName, role);
246 }
247 catch (NoSuchRoleException nsre) {
248 }
249 }
250
251 return role;
252 }
253
254 protected User getUser(long companyId, long groupId, String uuid)
255 throws SystemException {
256
257 List<User> users = usersMap.get(uuid);
258
259 if (users == null) {
260 LinkedHashMap<String, Object> params =
261 new LinkedHashMap<String, Object>();
262
263 params.put("usersGroups", new Long(groupId));
264
265 try {
266 User user = UserLocalServiceUtil.getUserByUuid(uuid);
267
268 users = UserLocalServiceUtil.search(
269 companyId, null, null, null, user.getScreenName(), null,
270 WorkflowConstants.STATUS_APPROVED, params, true, 0, 1,
271 (OrderByComparator)null);
272
273 }
274 catch (PortalException pe) {
275 }
276
277 usersMap.put(uuid, users);
278 }
279
280 if (users.size() == 0) {
281 return null;
282 }
283 else {
284 return users.get(0);
285 }
286 }
287
288 protected List<Role> getUserRoles(long userId) throws SystemException {
289 List<Role> userRoles = userRolesMap.get(userId);
290
291 if (userRoles == null) {
292 userRoles = RoleLocalServiceUtil.getUserRoles(userId);
293
294 userRolesMap.put(userId, userRoles);
295 }
296
297 return userRoles;
298 }
299
300 protected Map<String, Long> entityGroupIdMap = new HashMap<String, Long>();
301 protected Map<String, Map<String, Long>> entityMapMap =
302 new HashMap<String, Map<String, Long>>();
303 protected Map<Long, List<Role>> groupRolesMap =
304 new HashMap<Long, List<Role>>();
305 protected Map<Long, List<User>> groupUsersMap =
306 new HashMap<Long, List<User>>();
307 protected Map<String, Resource> resourcesMap =
308 new HashMap<String, Resource>();
309 protected Map<String, Role> rolesMap = new HashMap<String, Role>();
310 protected Map<Long, List<Role>> userRolesMap =
311 new HashMap<Long, List<Role>>();
312 protected Map<String, List<User>> usersMap =
313 new HashMap<String, List<User>>();
314
315 }