All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.exportimport.lar.LayoutCache Maven / Gradle / Ivy

There is a newer version: 4.0.5
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.exportimport.lar;

import com.liferay.exportimport.internal.util.ExportImportPermissionUtil;
import com.liferay.portal.kernel.exception.NoSuchRoleException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.Team;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.security.permission.ResourceActionsUtil;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.util.ListUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Charles May
 */
public class LayoutCache {

	public Role getNameRole(long companyId, String roleName)
		throws PortalException {

		Role role = nameRolesMap.get(roleName);

		if (role == null) {
			try {
				role = RoleLocalServiceUtil.getRole(companyId, roleName);

				nameRolesMap.put(roleName, role);
			}
			catch (NoSuchRoleException nsre) {
			}
		}

		return role;
	}

	public Role getUuidRole(long companyId, String uuid)
		throws PortalException {

		Role role = uuidRolesMap.get(uuid);

		if (role == null) {
			try {
				role = RoleLocalServiceUtil.getRoleByUuidAndCompanyId(
					uuid, companyId);

				uuidRolesMap.put(uuid, role);
			}
			catch (NoSuchRoleException nsre) {
			}
		}

		return role;
	}

	protected List getGroupRoles(long groupId, String resourceName)
		throws PortalException {

		List roles = groupRolesMap.get(groupId);

		if (roles != null) {
			return roles;
		}

		Group group = GroupLocalServiceUtil.getGroup(groupId);

		roles = ListUtil.copy(
			ResourceActionsUtil.getRoles(
				group.getCompanyId(), group, resourceName, null));

		Map teamRoleMap = RoleLocalServiceUtil.getTeamRoleMap(
			groupId);

		for (Map.Entry entry : teamRoleMap.entrySet()) {
			Team team = entry.getKey();
			Role teamRole = entry.getValue();

			teamRole.setName(
				ExportImportPermissionUtil.getTeamRoleName(team.getName()));
			teamRole.setDescription(team.getDescription());

			roles.add(teamRole);
		}

		groupRolesMap.put(groupId, roles);

		return roles;
	}

	protected List getGroupUsers(long groupId) {
		List users = groupUsersMap.get(groupId);

		if (users == null) {
			users = UserLocalServiceUtil.getGroupUsers(groupId);

			groupUsersMap.put(groupId, users);
		}

		return users;
	}

	protected List getUserRoles(long userId) {
		List userRoles = userRolesMap.get(userId);

		if (userRoles == null) {
			userRoles = RoleLocalServiceUtil.getUserRoles(userId);

			userRolesMap.put(userId, userRoles);
		}

		return userRoles;
	}

	protected Map> groupRolesMap = new HashMap<>();
	protected Map> groupUsersMap = new HashMap<>();
	protected Map nameRolesMap = new HashMap<>();
	protected Map> userRolesMap = new HashMap<>();
	protected Map uuidRolesMap = new HashMap<>();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy