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.DuplicateGroupException;
024: import com.liferay.portal.GroupFriendlyURLException;
025: import com.liferay.portal.GroupNameException;
026: import com.liferay.portal.NoSuchGroupException;
027: import com.liferay.portal.RequiredGroupException;
028: import com.liferay.portal.kernel.util.Constants;
029: import com.liferay.portal.kernel.util.ParamUtil;
030: import com.liferay.portal.model.Group;
031: import com.liferay.portal.security.auth.PrincipalException;
032: import com.liferay.portal.service.GroupServiceUtil;
033: import com.liferay.portal.struts.PortletAction;
034: import com.liferay.portal.util.LiveUsers;
035: import com.liferay.portal.util.PortalUtil;
036: import com.liferay.util.servlet.SessionErrors;
037:
038: import javax.portlet.ActionRequest;
039: import javax.portlet.ActionResponse;
040: import javax.portlet.PortletConfig;
041: import javax.portlet.RenderRequest;
042: import javax.portlet.RenderResponse;
043:
044: import org.apache.struts.action.ActionForm;
045: import org.apache.struts.action.ActionForward;
046: import org.apache.struts.action.ActionMapping;
047:
048: /**
049: * <a href="EditGroupAction.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: *
053: */
054: public class EditGroupAction extends PortletAction {
055:
056: public void processAction(ActionMapping mapping, ActionForm form,
057: PortletConfig config, ActionRequest req, ActionResponse res)
058: throws Exception {
059:
060: String cmd = ParamUtil.getString(req, Constants.CMD);
061:
062: try {
063: if (cmd.equals(Constants.ADD)
064: || cmd.equals(Constants.UPDATE)) {
065: updateGroup(req);
066: } else if (cmd.equals(Constants.DELETE)) {
067: deleteGroup(req);
068: }
069:
070: sendRedirect(req, res);
071: } catch (Exception e) {
072: if (e instanceof NoSuchGroupException
073: || e instanceof PrincipalException) {
074:
075: SessionErrors.add(req, e.getClass().getName());
076:
077: setForward(req, "portlet.communities.error");
078: } else if (e instanceof DuplicateGroupException
079: || e instanceof GroupFriendlyURLException
080: || e instanceof GroupNameException
081: || e instanceof RequiredGroupException) {
082:
083: SessionErrors.add(req, e.getClass().getName(), e);
084:
085: if (cmd.equals(Constants.DELETE)) {
086: res.sendRedirect(ParamUtil.getString(req,
087: "redirect"));
088: }
089: } else {
090: throw e;
091: }
092: }
093: }
094:
095: public ActionForward render(ActionMapping mapping, ActionForm form,
096: PortletConfig config, RenderRequest req, RenderResponse res)
097: throws Exception {
098:
099: try {
100: ActionUtil.getGroup(req);
101: } catch (Exception e) {
102: if (e instanceof NoSuchGroupException
103: || e instanceof PrincipalException) {
104:
105: SessionErrors.add(req, e.getClass().getName());
106:
107: return mapping.findForward("portlet.communities.error");
108: } else {
109: throw e;
110: }
111: }
112:
113: return mapping.findForward(getForward(req,
114: "portlet.communities.edit_community"));
115: }
116:
117: protected void deleteGroup(ActionRequest req) throws Exception {
118: long groupId = ParamUtil.getLong(req, "groupId");
119:
120: GroupServiceUtil.deleteGroup(groupId);
121:
122: LiveUsers.deleteGroup(groupId);
123: }
124:
125: protected void updateGroup(ActionRequest req) throws Exception {
126: long userId = PortalUtil.getUserId(req);
127:
128: long groupId = ParamUtil.getLong(req, "groupId");
129:
130: String name = ParamUtil.getString(req, "name");
131: String description = ParamUtil.getString(req, "description");
132: int type = ParamUtil.getInteger(req, "type");
133: String friendlyURL = ParamUtil.getString(req, "friendlyURL");
134: boolean active = ParamUtil.getBoolean(req, "active");
135:
136: if (groupId <= 0) {
137:
138: // Add group
139:
140: Group group = GroupServiceUtil.addGroup(name, description,
141: type, friendlyURL, active);
142:
143: LiveUsers.joinGroup(userId, group.getGroupId());
144: } else {
145:
146: // Update group
147:
148: GroupServiceUtil.updateGroup(groupId, name, description,
149: type, friendlyURL, active);
150: }
151: }
152:
153: }
|