Java tutorial
/** * Copyright (c) 2000-2012 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.portlet.sites.action; import com.liferay.portal.NoSuchGroupException; import com.liferay.portal.NoSuchRoleException; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.Constants; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.model.Role; import com.liferay.portal.model.RoleConstants; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.UserGroupGroupRoleServiceUtil; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.util.WebKeys; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author Brett Swaim */ public class EditUserGroupRolesAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { String cmd = ParamUtil.getString(actionRequest, Constants.CMD); try { if (cmd.equals("user_group_group_role_users")) { updateUserGroupGroupRoleUsers(actionRequest); } sendRedirect(actionRequest, actionResponse); } catch (Exception e) { if (e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.sites_admin.error"); } else { throw e; } } } @Override public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { try { ActionUtil.getGroup(renderRequest); ActionUtil.getRole(renderRequest); Role role = (Role) renderRequest.getAttribute(WebKeys.ROLE); if (role != null) { String name = role.getName(); if (name.equals(RoleConstants.ORGANIZATION_USER) || name.equals(RoleConstants.SITE_MEMBER)) { throw new NoSuchRoleException(); } } } catch (Exception e) { if (e instanceof NoSuchGroupException || e instanceof NoSuchRoleException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass().getName()); return mapping.findForward("portlet.sites_admin.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.sites_admin.edit_user_group_roles")); } protected void updateUserGroupGroupRoleUsers(ActionRequest actionRequest) throws Exception { long groupId = ParamUtil.getLong(actionRequest, "groupId"); long roleId = ParamUtil.getLong(actionRequest, "roleId"); long[] addUserGroupIds = StringUtil.split(ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L); long[] removeUserGroupIds = StringUtil.split(ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L); UserGroupGroupRoleServiceUtil.addUserGroupGroupRoles(addUserGroupIds, groupId, roleId); UserGroupGroupRoleServiceUtil.deleteUserGroupGroupRoles(removeUserGroupIds, groupId, roleId); } }