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.messageboards.action;
022:
023: import com.liferay.documentlibrary.FileNameException;
024: import com.liferay.documentlibrary.FileSizeException;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.struts.PortletAction;
029: import com.liferay.portal.theme.ThemeDisplay;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portal.util.WebKeys;
032: import com.liferay.portlet.ActionResponseImpl;
033: import com.liferay.portlet.messageboards.MessageBodyException;
034: import com.liferay.portlet.messageboards.MessageSubjectException;
035: import com.liferay.portlet.messageboards.NoSuchMessageException;
036: import com.liferay.portlet.messageboards.RequiredMessageException;
037: import com.liferay.portlet.messageboards.model.MBMessage;
038: import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
039: import com.liferay.util.servlet.SessionErrors;
040:
041: import javax.portlet.ActionRequest;
042: import javax.portlet.ActionResponse;
043: import javax.portlet.PortletConfig;
044:
045: import org.apache.struts.action.ActionForm;
046: import org.apache.struts.action.ActionMapping;
047:
048: /**
049: * <a href="EditDiscussionAction.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: *
053: */
054: public class EditDiscussionAction extends PortletAction {
055:
056: public void processAction(ActionMapping mapping, ActionForm form,
057: PortletConfig config, ActionRequest req, ActionResponse res)
058: throws Exception {
059:
060: ActionResponseImpl resImpl = (ActionResponseImpl) res;
061:
062: String cmd = ParamUtil.getString(req, Constants.CMD);
063:
064: try {
065: String redirect = ParamUtil.getString(req, "redirect");
066:
067: if (cmd.equals(Constants.ADD)
068: || cmd.equals(Constants.UPDATE)) {
069: MBMessage message = updateMessage(req);
070:
071: redirect += "#" + resImpl.getNamespace()
072: + "messageScroll" + message.getMessageId();
073: } else if (cmd.equals(Constants.DELETE)) {
074: deleteMessage(req);
075: }
076:
077: sendRedirect(req, res, redirect);
078: } catch (Exception e) {
079: if (e instanceof NoSuchMessageException
080: || e instanceof PrincipalException
081: || e instanceof RequiredMessageException) {
082:
083: SessionErrors.add(req, e.getClass().getName());
084:
085: setForward(req, "portlet.message_boards.error");
086: } else if (e instanceof FileNameException
087: || e instanceof FileSizeException
088: || e instanceof MessageBodyException
089: || e instanceof MessageSubjectException) {
090:
091: SessionErrors.add(req, e.getClass().getName());
092:
093: sendRedirect(req, res);
094: } else {
095: throw e;
096: }
097: }
098: }
099:
100: protected void deleteMessage(ActionRequest req) throws Exception {
101: long groupId = PortalUtil.getPortletGroupId(req);
102: String className = ParamUtil.getString(req, "className");
103: long classPK = ParamUtil.getLong(req, "classPK");
104:
105: long messageId = ParamUtil.getLong(req, "messageId");
106:
107: MBMessageServiceUtil.deleteDiscussionMessage(groupId,
108: className, classPK, messageId);
109: }
110:
111: protected boolean isCheckMethodOnProcessAction() {
112: return _CHECK_METHOD_ON_PROCESS_ACTION;
113: }
114:
115: protected MBMessage updateMessage(ActionRequest req)
116: throws Exception {
117: ThemeDisplay themeDisplay = (ThemeDisplay) req
118: .getAttribute(WebKeys.THEME_DISPLAY);
119:
120: long groupId = PortalUtil.getPortletGroupId(req);
121: String className = ParamUtil.getString(req, "className");
122: long classPK = ParamUtil.getLong(req, "classPK");
123:
124: long messageId = ParamUtil.getLong(req, "messageId");
125:
126: long threadId = ParamUtil.getLong(req, "threadId");
127: long parentMessageId = ParamUtil
128: .getLong(req, "parentMessageId");
129: String subject = ParamUtil.getString(req, "subject");
130: String body = ParamUtil.getString(req, "body");
131:
132: MBMessage message = null;
133:
134: if (messageId <= 0) {
135:
136: // Add message
137:
138: message = MBMessageServiceUtil.addDiscussionMessage(
139: groupId, className, classPK, threadId,
140: parentMessageId, subject, body, themeDisplay);
141: } else {
142:
143: // Update message
144:
145: message = MBMessageServiceUtil.updateDiscussionMessage(
146: groupId, className, classPK, messageId, subject,
147: body);
148: }
149:
150: return message;
151: }
152:
153: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
154:
155: }
|