001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.action;
016    
017    import com.liferay.portal.NoSuchOrganizationException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Organization;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.OrganizationLocalServiceUtil;
026    import com.liferay.portal.service.UserGroupServiceUtil;
027    import com.liferay.portal.service.UserServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class EditOrganizationAssignmentsAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals("organization_user_groups")) {
055                                    updateOrganizationUserGroups(actionRequest);
056                            }
057                            else if (cmd.equals("organization_users")) {
058                                    updateOrganizationUsers(actionRequest);
059                            }
060    
061                            if (Validator.isNotNull(cmd)) {
062                                    String redirect = ParamUtil.getString(
063                                            actionRequest, "assignmentsRedirect");
064    
065                                    sendRedirect(actionRequest, actionResponse, redirect);
066                            }
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchOrganizationException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073    
074                                    setForward(actionRequest, "portlet.users_admin.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            RenderRequest renderRequest, RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getOrganization(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchOrganizationException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass().getName());
096    
097                                    return mapping.findForward("portlet.users_admin.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    return mapping.findForward(getForward(
105                            renderRequest,
106                            "portlet.users_admin.edit_organization_assignments"));
107            }
108    
109            protected void updateOrganizationUserGroups(ActionRequest actionRequest)
110                    throws Exception {
111    
112                    long organizationId = ParamUtil.getLong(
113                            actionRequest, "organizationId");
114    
115                    Organization organization =
116                            OrganizationLocalServiceUtil.getOrganization(organizationId);
117    
118                    long groupId = organization.getGroup().getGroupId();
119    
120                    long[] addUserGroupIds = StringUtil.split(
121                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
122                    long[] removeUserGroupIds = StringUtil.split(
123                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
124    
125                    UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
126                    UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
127            }
128    
129            protected void updateOrganizationUsers(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    long organizationId = ParamUtil.getLong(
133                            actionRequest, "organizationId");
134    
135                    long[] addUserIds = StringUtil.split(
136                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
137                    long[] removeUserIds = StringUtil.split(
138                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
139    
140                    UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
141                    UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
142            }
143    
144    }