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.communities.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.UserServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class EditTeamAssignmentsAction extends PortletAction {
041    
042            public void processAction(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
048    
049                    try {
050                            if (cmd.equals("team_users")) {
051                                    updateTeamUsers(actionRequest);
052                            }
053    
054                            if (Validator.isNotNull(cmd)) {
055                                    String redirect = ParamUtil.getString(
056                                            actionRequest, "assignmentsRedirect");
057    
058                                    sendRedirect(actionRequest, actionResponse, redirect);
059                            }
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchTeamException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.communities.error");
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            public ActionForward render(
076                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
077                            RenderRequest renderRequest, RenderResponse renderResponse)
078                    throws Exception {
079    
080                    try {
081                            ActionUtil.getTeam(renderRequest);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchTeamException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(renderRequest, e.getClass().getName());
088    
089                                    return mapping.findForward("portlet.communities.error");
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095    
096                    return mapping.findForward(getForward(
097                            renderRequest, "portlet.communities.edit_team_assignments"));
098            }
099    
100            protected void updateTeamUsers(ActionRequest actionRequest)
101                    throws Exception {
102    
103                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
104    
105                    long[] addUserIds = StringUtil.split(
106                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
107                    long[] removeUserIds = StringUtil.split(
108                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
109    
110                    UserServiceUtil.addTeamUsers(teamId, addUserIds);
111                    UserServiceUtil.unsetTeamUsers(teamId, removeUserIds);
112            }
113    
114    }