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.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 mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    try {
052                            if (cmd.equals("team_user_groups")) {
053                                    updateTeamUserGroups(actionRequest);
054                            }
055                            else if (cmd.equals("team_users")) {
056                                    updateTeamUsers(actionRequest);
057                            }
058    
059                            if (Validator.isNotNull(cmd)) {
060                                    String redirect = ParamUtil.getString(
061                                            actionRequest, "assignmentsRedirect");
062    
063                                    sendRedirect(actionRequest, actionResponse, redirect);
064                            }
065                    }
066                    catch (Exception e) {
067                            if (e instanceof NoSuchTeamException ||
068                                    e instanceof PrincipalException) {
069    
070                                    SessionErrors.add(actionRequest, e.getClass().getName());
071    
072                                    setForward(actionRequest, "portlet.sites_admin.error");
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            @Override
081            public ActionForward render(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            RenderRequest renderRequest, RenderResponse renderResponse)
084                    throws Exception {
085    
086                    try {
087                            ActionUtil.getTeam(renderRequest);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchTeamException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(renderRequest, e.getClass().getName());
094    
095                                    return mapping.findForward("portlet.sites_admin.error");
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101    
102                    return mapping.findForward(getForward(
103                            renderRequest, "portlet.sites_admin.edit_team_assignments"));
104            }
105    
106            protected void updateTeamUserGroups(ActionRequest actionRequest)
107                    throws Exception {
108    
109                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
110    
111                    long[] addUserGroupIds = StringUtil.split(
112                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
113                    long[] removeUserGroupIds = StringUtil.split(
114                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
115    
116                    UserGroupServiceUtil.addTeamUserGroups(teamId, addUserGroupIds);
117                    UserGroupServiceUtil.unsetTeamUserGroups(teamId, removeUserGroupIds);
118            }
119    
120            protected void updateTeamUsers(ActionRequest actionRequest)
121                    throws Exception {
122    
123                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
124    
125                    long[] addUserIds = StringUtil.split(
126                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
127                    long[] removeUserIds = StringUtil.split(
128                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
129    
130                    UserServiceUtil.addTeamUsers(teamId, addUserIds);
131                    UserServiceUtil.unsetTeamUsers(teamId, removeUserIds);
132            }
133    
134    }