001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.sites.action;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.NoSuchTeamException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.UserGroupServiceUtil;
026    import com.liferay.portal.service.UserServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class EditTeamAssignmentsAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping actionMapping, ActionForm actionForm,
047                            PortletConfig portletConfig, ActionRequest actionRequest,
048                            ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals("team_user_groups")) {
055                                    updateTeamUserGroups(actionRequest);
056                            }
057                            else if (cmd.equals("team_users")) {
058                                    updateTeamUsers(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 NoSuchTeamException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073    
074                                    setForward(actionRequest, "portlet.sites_admin.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, RenderRequest renderRequest,
086                            RenderResponse renderResponse)
087                    throws Exception {
088    
089                    try {
090                            ActionUtil.getTeam(renderRequest);
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchGroupException ||
094                                    e instanceof NoSuchTeamException ||
095                                    e instanceof PrincipalException) {
096    
097                                    SessionErrors.add(renderRequest, e.getClass());
098    
099                                    return actionMapping.findForward("portlet.sites_admin.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return actionMapping.findForward(
107                            getForward(
108                                    renderRequest, "portlet.sites_admin.edit_team_assignments"));
109            }
110    
111            protected void updateTeamUserGroups(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
115    
116                    long[] addUserGroupIds = StringUtil.split(
117                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
118                    long[] removeUserGroupIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
120    
121                    UserGroupServiceUtil.addTeamUserGroups(teamId, addUserGroupIds);
122                    UserGroupServiceUtil.unsetTeamUserGroups(teamId, removeUserGroupIds);
123            }
124    
125            protected void updateTeamUsers(ActionRequest actionRequest)
126                    throws Exception {
127    
128                    long teamId = ParamUtil.getLong(actionRequest, "teamId");
129    
130                    long[] addUserIds = StringUtil.split(
131                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
132                    long[] removeUserIds = StringUtil.split(
133                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
134    
135                    UserServiceUtil.addTeamUsers(teamId, addUserIds);
136                    UserServiceUtil.unsetTeamUsers(teamId, removeUserIds);
137            }
138    
139    }