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.usersadmin.action; import com.liferay.portal.NoSuchOrganizationException; 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.kernel.util.Validator; import com.liferay.portal.model.Organization; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.UserGroupServiceUtil; import com.liferay.portal.service.UserServiceUtil; import com.liferay.portal.struts.PortletAction; 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 Brian Wing Shun Chan */ public class EditOrganizationAssignmentsAction 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("organization_user_groups")) { updateOrganizationUserGroups(actionRequest); } else if (cmd.equals("organization_users")) { updateOrganizationUsers(actionRequest); } if (Validator.isNotNull(cmd)) { String redirect = ParamUtil.getString(actionRequest, "assignmentsRedirect"); sendRedirect(actionRequest, actionResponse, redirect); } } catch (Exception e) { if (e instanceof NoSuchOrganizationException || e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.users_admin.error"); } else { throw e; } } } @Override public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { try { ActionUtil.getOrganization(renderRequest); } catch (Exception e) { if (e instanceof NoSuchOrganizationException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass().getName()); return mapping.findForward("portlet.users_admin.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.users_admin.edit_organization_assignments")); } protected void updateOrganizationUserGroups(ActionRequest actionRequest) throws Exception { long organizationId = ParamUtil.getLong(actionRequest, "organizationId"); Organization organization = OrganizationLocalServiceUtil.getOrganization(organizationId); long groupId = organization.getGroup().getGroupId(); long[] addUserGroupIds = StringUtil.split(ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L); long[] removeUserGroupIds = StringUtil.split(ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L); UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds); UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds); } protected void updateOrganizationUsers(ActionRequest actionRequest) throws Exception { long organizationId = ParamUtil.getLong(actionRequest, "organizationId"); long[] addUserIds = StringUtil.split(ParamUtil.getString(actionRequest, "addUserIds"), 0L); long[] removeUserIds = StringUtil.split(ParamUtil.getString(actionRequest, "removeUserIds"), 0L); UserServiceUtil.addOrganizationUsers(organizationId, addUserIds); UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds); } }