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.NoSuchGroupException;
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.liveusers.LiveUsers;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.OrganizationServiceUtil;
027    import com.liferay.portal.service.UserGroupRoleServiceUtil;
028    import com.liferay.portal.service.UserGroupServiceUtil;
029    import com.liferay.portal.service.UserServiceUtil;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditGroupAssignmentsAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals("group_organizations")) {
059                                    updateGroupOrganizations(actionRequest);
060                            }
061                            else if (cmd.equals("group_user_groups")) {
062                                    updateGroupUserGroups(actionRequest);
063                            }
064                            else if (cmd.equals("group_users")) {
065                                    updateGroupUsers(actionRequest);
066                            }
067                            else if (cmd.equals("user_group_role")) {
068                                    updateUserGroupRole(actionRequest);
069                            }
070    
071                            if (Validator.isNotNull(cmd)) {
072                                    String redirect = ParamUtil.getString(
073                                            actionRequest, "assignmentsRedirect");
074    
075                                    if (Validator.isNull(redirect)) {
076                                            redirect = ParamUtil.getString(actionRequest, "redirect");
077                                    }
078    
079                                    sendRedirect(actionRequest, actionResponse, redirect);
080                            }
081                    }
082                    catch (Exception e) {
083                            if (e instanceof NoSuchGroupException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass().getName());
087    
088                                    setForward(actionRequest, "portlet.communities.error");
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            public ActionForward render(
097                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
098                            RenderRequest renderRequest, RenderResponse renderResponse)
099                    throws Exception {
100    
101                    try {
102                            ActionUtil.getGroup(renderRequest);
103                    }
104                    catch (Exception e) {
105                            if (e instanceof NoSuchGroupException ||
106                                    e instanceof PrincipalException) {
107    
108                                    SessionErrors.add(renderRequest, e.getClass().getName());
109    
110                                    return mapping.findForward("portlet.communities.error");
111                            }
112                            else {
113                                    throw e;
114                            }
115                    }
116    
117                    return mapping.findForward(getForward(renderRequest,
118                            "portlet.communities.edit_community_assignments"));
119            }
120    
121            protected void updateGroupOrganizations(ActionRequest actionRequest)
122                    throws Exception {
123    
124                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
125    
126                    long[] addOrganizationIds = StringUtil.split(
127                            ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
128                    long[] removeOrganizationIds = StringUtil.split(
129                            ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
130    
131                    OrganizationServiceUtil.addGroupOrganizations(
132                            groupId, addOrganizationIds);
133                    OrganizationServiceUtil.unsetGroupOrganizations(
134                            groupId, removeOrganizationIds);
135            }
136    
137            protected void updateGroupUserGroups(ActionRequest actionRequest)
138                    throws Exception {
139    
140                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
141    
142                    long[] addUserGroupIds = StringUtil.split(
143                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
144                    long[] removeUserGroupIds = StringUtil.split(
145                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
146    
147                    UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
148                    UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
149            }
150    
151            protected void updateGroupUsers(ActionRequest actionRequest)
152                    throws Exception {
153    
154                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
155                            WebKeys.THEME_DISPLAY);
156    
157                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
158    
159                    long[] addUserIds = StringUtil.split(
160                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
161                    long[] removeUserIds = StringUtil.split(
162                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
163    
164                    UserServiceUtil.addGroupUsers(groupId, addUserIds);
165                    UserServiceUtil.unsetGroupUsers(groupId, removeUserIds);
166    
167                    LiveUsers.joinGroup(themeDisplay.getCompanyId(), groupId, addUserIds);
168                    LiveUsers.leaveGroup(
169                            themeDisplay.getCompanyId(), groupId, removeUserIds);
170            }
171    
172            protected void updateUserGroupRole(ActionRequest actionRequest)
173                    throws Exception {
174    
175                    User user = PortalUtil.getSelectedUser(actionRequest, false);
176    
177                    if (user == null) {
178                            return;
179                    }
180    
181                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
182    
183                    long[] addRoleIds = StringUtil.split(
184                            ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
185                    long[] removeRoleIds = StringUtil.split(
186                            ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
187    
188                    UserGroupRoleServiceUtil.addUserGroupRoles(
189                            user.getUserId(), groupId, addRoleIds);
190                    UserGroupRoleServiceUtil.deleteUserGroupRoles(
191                            user.getUserId(), groupId, removeRoleIds);
192            }
193    
194    }