001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals("organization_user_groups")) {
054                                    updateOrganizationUserGroups(actionRequest);
055                            }
056                            else if (cmd.equals("organization_users")) {
057                                    updateOrganizationUsers(actionRequest);
058                            }
059    
060                            if (Validator.isNotNull(cmd)) {
061                                    String redirect = ParamUtil.getString(
062                                            actionRequest, "assignmentsRedirect");
063    
064                                    sendRedirect(actionRequest, actionResponse, redirect);
065                            }
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchOrganizationException ||
069                                    e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072    
073                                    setForward(actionRequest, "portlet.enterprise_admin.error");
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            public ActionForward render(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            RenderRequest renderRequest, RenderResponse renderResponse)
084                    throws Exception {
085    
086                    try {
087                            ActionUtil.getOrganization(renderRequest);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchOrganizationException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(renderRequest, e.getClass().getName());
094    
095                                    return mapping.findForward("portlet.enterprise_admin.error");
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101    
102                    return mapping.findForward(getForward(
103                            renderRequest,
104                            "portlet.enterprise_admin.edit_organization_assignments"));
105            }
106    
107            protected void updateOrganizationUserGroups(ActionRequest actionRequest)
108                    throws Exception {
109    
110                    long organizationId = ParamUtil.getLong(
111                            actionRequest, "organizationId");
112    
113                    Organization organization =
114                            OrganizationLocalServiceUtil.getOrganization(organizationId);
115    
116                    long groupId = organization.getGroup().getGroupId();
117    
118                    long[] addUserGroupIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
120                    long[] removeUserGroupIds = StringUtil.split(
121                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
122    
123                    UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
124                    UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
125            }
126    
127            protected void updateOrganizationUsers(ActionRequest actionRequest)
128                    throws Exception {
129    
130                    long organizationId = ParamUtil.getLong(
131                            actionRequest, "organizationId");
132    
133                    long[] addUserIds = StringUtil.split(
134                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
135                    long[] removeUserIds = StringUtil.split(
136                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
137    
138                    UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
139                    UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
140            }
141    
142    }