001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.communities.action;
022:
023: import com.liferay.portal.NoSuchGroupException;
024: import com.liferay.portal.kernel.util.Constants;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.service.OrganizationServiceUtil;
031: import com.liferay.portal.service.UserGroupRoleServiceUtil;
032: import com.liferay.portal.service.UserGroupServiceUtil;
033: import com.liferay.portal.service.UserServiceUtil;
034: import com.liferay.portal.struts.PortletAction;
035: import com.liferay.portal.util.LiveUsers;
036: import com.liferay.portal.util.PortalUtil;
037: import com.liferay.util.servlet.SessionErrors;
038:
039: import javax.portlet.ActionRequest;
040: import javax.portlet.ActionResponse;
041: import javax.portlet.PortletConfig;
042: import javax.portlet.RenderRequest;
043: import javax.portlet.RenderResponse;
044:
045: import org.apache.struts.action.ActionForm;
046: import org.apache.struts.action.ActionForward;
047: import org.apache.struts.action.ActionMapping;
048:
049: /**
050: * <a href="EditGroupAssignmentsAction.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class EditGroupAssignmentsAction extends PortletAction {
056:
057: public void processAction(ActionMapping mapping, ActionForm form,
058: PortletConfig config, ActionRequest req, ActionResponse res)
059: throws Exception {
060:
061: String cmd = ParamUtil.getString(req, Constants.CMD);
062:
063: try {
064: if (cmd.equals("group_organizations")) {
065: updateGroupOrganizations(req);
066: } else if (cmd.equals("group_user_groups")) {
067: updateGroupUserGroups(req);
068: } else if (cmd.equals("group_users")) {
069: updateGroupUsers(req);
070: } else if (cmd.equals("user_group_role")) {
071: updateUserGroupRole(req);
072: }
073:
074: if (Validator.isNotNull(cmd)) {
075: String redirect = ParamUtil.getString(req,
076: "assignmentsRedirect");
077:
078: if (Validator.isNull(redirect)) {
079: redirect = ParamUtil.getString(req, "redirect");
080: }
081:
082: sendRedirect(req, res, redirect);
083: }
084: } catch (Exception e) {
085: if (e instanceof NoSuchGroupException
086: || e instanceof PrincipalException) {
087:
088: SessionErrors.add(req, e.getClass().getName());
089:
090: setForward(req, "portlet.communities.error");
091: } else {
092: throw e;
093: }
094: }
095: }
096:
097: public ActionForward render(ActionMapping mapping, ActionForm form,
098: PortletConfig config, RenderRequest req, RenderResponse res)
099: throws Exception {
100:
101: try {
102: ActionUtil.getGroup(req);
103: } catch (Exception e) {
104: if (e instanceof NoSuchGroupException
105: || e instanceof PrincipalException) {
106:
107: SessionErrors.add(req, e.getClass().getName());
108:
109: return mapping.findForward("portlet.communities.error");
110: } else {
111: throw e;
112: }
113: }
114:
115: return mapping.findForward(getForward(req,
116: "portlet.communities.edit_community_assignments"));
117: }
118:
119: protected void updateGroupOrganizations(ActionRequest req)
120: throws Exception {
121:
122: long groupId = ParamUtil.getLong(req, "groupId");
123:
124: long[] addOrganizationIds = StringUtil.split(ParamUtil
125: .getString(req, "addOrganizationIds"), 0L);
126: long[] removeOrganizationIds = StringUtil.split(ParamUtil
127: .getString(req, "removeOrganizationIds"), 0L);
128:
129: OrganizationServiceUtil.addGroupOrganizations(groupId,
130: addOrganizationIds);
131: OrganizationServiceUtil.unsetGroupOrganizations(groupId,
132: removeOrganizationIds);
133: }
134:
135: protected void updateGroupUserGroups(ActionRequest req)
136: throws Exception {
137:
138: long groupId = ParamUtil.getLong(req, "groupId");
139:
140: long[] addUserGroupIds = StringUtil.split(ParamUtil.getString(
141: req, "addUserGroupIds"), 0L);
142: long[] removeUserGroupIds = StringUtil.split(ParamUtil
143: .getString(req, "removeUserGroupIds"), 0L);
144:
145: UserGroupServiceUtil.addGroupUserGroups(groupId,
146: addUserGroupIds);
147: UserGroupServiceUtil.unsetGroupUserGroups(groupId,
148: removeUserGroupIds);
149: }
150:
151: protected void updateGroupUsers(ActionRequest req) throws Exception {
152: long groupId = ParamUtil.getLong(req, "groupId");
153:
154: long[] addUserIds = StringUtil.split(ParamUtil.getString(req,
155: "addUserIds"), 0L);
156: long[] removeUserIds = StringUtil.split(ParamUtil.getString(
157: req, "removeUserIds"), 0L);
158:
159: UserServiceUtil.addGroupUsers(groupId, addUserIds);
160: UserServiceUtil.unsetGroupUsers(groupId, removeUserIds);
161:
162: LiveUsers.joinGroup(addUserIds, groupId);
163: LiveUsers.leaveGroup(removeUserIds, groupId);
164: }
165:
166: protected void updateUserGroupRole(ActionRequest req)
167: throws Exception {
168: User user = PortalUtil.getSelectedUser(req, false);
169:
170: if (user == null) {
171: return;
172: }
173:
174: long groupId = ParamUtil.getLong(req, "groupId");
175:
176: long[] addRoleIds = StringUtil.split(ParamUtil.getString(req,
177: "addRoleIds"), 0L);
178: long[] removeRoleIds = StringUtil.split(ParamUtil.getString(
179: req, "removeRoleIds"), 0L);
180:
181: UserGroupRoleServiceUtil.addUserGroupRoles(user.getUserId(),
182: groupId, addRoleIds);
183: UserGroupRoleServiceUtil.deleteUserGroupRoles(user.getUserId(),
184: groupId, removeRoleIds);
185: }
186:
187: }
|