001    /**
002     * Copyright (c) 2000-2013 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.sites.action;
016    
017    import com.liferay.portal.NoSuchTeamException;
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.security.auth.PrincipalException;
024    import com.liferay.portal.service.UserGroupServiceUtil;
025    import com.liferay.portal.service.UserServiceUtil;
026    import com.liferay.portal.struts.PortletAction;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class EditTeamAssignmentsAction extends PortletAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals("team_user_groups")) {
054                                    updateTeamUserGroups(actionRequest);
055                            }
056                            else if (cmd.equals("team_users")) {
057                                    updateTeamUsers(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 NoSuchTeamException ||
069                                    e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass());
072    
073                                    setForward(actionRequest, "portlet.sites_admin.error");
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            @Override
082            public ActionForward render(
083                            ActionMapping actionMapping, ActionForm actionForm,
084                            PortletConfig portletConfig, RenderRequest renderRequest,
085                            RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getTeam(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchTeamException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass());
096    
097                                    return actionMapping.findForward("portlet.sites_admin.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    return actionMapping.findForward(
105                            getForward(
106                                    renderRequest, "portlet.sites_admin.edit_team_assignments"));
107            }
108    
109            protected void updateTeamUserGroups(ActionRequest actionRequest)
110                    throws Exception {
111    
112                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
113    
114                    long[] addUserGroupIds = StringUtil.split(
115                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
116                    long[] removeUserGroupIds = StringUtil.split(
117                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
118    
119                    UserGroupServiceUtil.addTeamUserGroups(teamId, addUserGroupIds);
120                    UserGroupServiceUtil.unsetTeamUserGroups(teamId, removeUserGroupIds);
121            }
122    
123            protected void updateTeamUsers(ActionRequest actionRequest)
124                    throws Exception {
125    
126                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
127    
128                    long[] addUserIds = StringUtil.split(
129                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
130                    long[] removeUserIds = StringUtil.split(
131                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
132    
133                    UserServiceUtil.addTeamUsers(teamId, addUserIds);
134                    UserServiceUtil.unsetTeamUsers(teamId, removeUserIds);
135            }
136    
137    }