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.captcha.CaptchaTextException;
026: import com.liferay.portal.captcha.CaptchaUtil;
027: import com.liferay.portal.kernel.util.Constants;
028: import com.liferay.portal.kernel.util.ObjectValuePair;
029: import com.liferay.portal.kernel.util.ParamUtil;
030: import com.liferay.portal.kernel.util.StringUtil;
031: import com.liferay.portal.security.auth.PrincipalException;
032: import com.liferay.portal.struts.PortletAction;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portal.util.WebKeys;
036: import com.liferay.portlet.ActionResponseImpl;
037: import com.liferay.portlet.messageboards.MessageBodyException;
038: import com.liferay.portlet.messageboards.MessageSubjectException;
039: import com.liferay.portlet.messageboards.NoSuchMessageException;
040: import com.liferay.portlet.messageboards.RequiredMessageException;
041: import com.liferay.portlet.messageboards.model.MBMessage;
042: import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
043: import com.liferay.portlet.tags.TagsEntryException;
044: import com.liferay.util.FileUtil;
045: import com.liferay.util.servlet.SessionErrors;
046: import com.liferay.util.servlet.UploadPortletRequest;
047:
048: import java.io.File;
049:
050: import java.util.ArrayList;
051: import java.util.List;
052:
053: import javax.portlet.ActionRequest;
054: import javax.portlet.ActionResponse;
055: import javax.portlet.PortletConfig;
056: import javax.portlet.PortletPreferences;
057: import javax.portlet.PortletURL;
058: import javax.portlet.RenderRequest;
059: import javax.portlet.RenderResponse;
060:
061: import org.apache.struts.action.ActionForm;
062: import org.apache.struts.action.ActionForward;
063: import org.apache.struts.action.ActionMapping;
064:
065: /**
066: * <a href="EditMessageAction.java.html"><b><i>View Source</i></b></a>
067: *
068: * @author Brian Wing Shun Chan
069: *
070: */
071: public class EditMessageAction extends PortletAction {
072:
073: public void processAction(ActionMapping mapping, ActionForm form,
074: PortletConfig config, ActionRequest req, ActionResponse res)
075: throws Exception {
076:
077: String cmd = ParamUtil.getString(req, Constants.CMD);
078:
079: try {
080: if (cmd.equals(Constants.ADD)
081: || cmd.equals(Constants.UPDATE)) {
082: updateMessage(req, res);
083: } else if (cmd.equals(Constants.DELETE)) {
084: deleteMessage(req);
085: } else if (cmd.equals(Constants.SUBSCRIBE)) {
086: subscribeMessage(req);
087: } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
088: unsubscribeMessage(req);
089: }
090:
091: if (cmd.equals(Constants.DELETE)
092: || cmd.equals(Constants.SUBSCRIBE)
093: || cmd.equals(Constants.UNSUBSCRIBE)) {
094:
095: sendRedirect(req, res);
096: }
097: } catch (Exception e) {
098: if (e instanceof NoSuchMessageException
099: || e instanceof PrincipalException
100: || e instanceof RequiredMessageException) {
101:
102: SessionErrors.add(req, e.getClass().getName());
103:
104: setForward(req, "portlet.message_boards.error");
105: } else if (e instanceof CaptchaTextException
106: || e instanceof FileNameException
107: || e instanceof FileSizeException
108: || e instanceof MessageBodyException
109: || e instanceof MessageSubjectException) {
110:
111: SessionErrors.add(req, e.getClass().getName());
112: } else if (e instanceof TagsEntryException) {
113: SessionErrors.add(req, e.getClass().getName(), e);
114: } else {
115: throw e;
116: }
117: }
118: }
119:
120: public ActionForward render(ActionMapping mapping, ActionForm form,
121: PortletConfig config, RenderRequest req, RenderResponse res)
122: throws Exception {
123:
124: try {
125: ActionUtil.getMessage(req);
126: } catch (Exception e) {
127: if (e instanceof NoSuchMessageException
128: || e instanceof PrincipalException) {
129:
130: SessionErrors.add(req, e.getClass().getName());
131:
132: return mapping
133: .findForward("portlet.message_boards.error");
134: } else {
135: throw e;
136: }
137: }
138:
139: return mapping.findForward(getForward(req,
140: "portlet.message_boards.edit_message"));
141: }
142:
143: protected void deleteMessage(ActionRequest req) throws Exception {
144: long messageId = ParamUtil.getLong(req, "messageId");
145:
146: MBMessageServiceUtil.deleteMessage(messageId);
147: }
148:
149: protected void subscribeMessage(ActionRequest req) throws Exception {
150: long messageId = ParamUtil.getLong(req, "messageId");
151:
152: MBMessageServiceUtil.subscribeMessage(messageId);
153: }
154:
155: protected void unsubscribeMessage(ActionRequest req)
156: throws Exception {
157: long messageId = ParamUtil.getLong(req, "messageId");
158:
159: MBMessageServiceUtil.unsubscribeMessage(messageId);
160: }
161:
162: protected void updateMessage(ActionRequest req, ActionResponse res)
163: throws Exception {
164:
165: ThemeDisplay themeDisplay = (ThemeDisplay) req
166: .getAttribute(WebKeys.THEME_DISPLAY);
167:
168: PortletPreferences prefs = req.getPreferences();
169:
170: long messageId = ParamUtil.getLong(req, "messageId");
171:
172: long categoryId = ParamUtil.getLong(req, "categoryId");
173: long threadId = ParamUtil.getLong(req, "threadId");
174: long parentMessageId = ParamUtil
175: .getLong(req, "parentMessageId");
176: String subject = ParamUtil.getString(req, "subject");
177: String body = ParamUtil.getString(req, "body");
178: boolean attachments = ParamUtil.getBoolean(req, "attachments");
179:
180: List files = new ArrayList();
181:
182: if (attachments) {
183: UploadPortletRequest uploadReq = PortalUtil
184: .getUploadPortletRequest(req);
185:
186: for (int i = 1; i <= 5; i++) {
187: File file = uploadReq.getFile("msgFile" + i);
188: String fileName = uploadReq.getFileName("msgFile" + i);
189: byte[] bytes = FileUtil.getBytes(file);
190:
191: if ((bytes != null) && (bytes.length > 0)) {
192: ObjectValuePair ovp = new ObjectValuePair(fileName,
193: bytes);
194:
195: files.add(ovp);
196: }
197: }
198: }
199:
200: boolean anonymous = ParamUtil.getBoolean(req, "anonymous");
201: double priority = ParamUtil.getDouble(req, "priority");
202:
203: String[] tagsEntries = StringUtil.split(ParamUtil.getString(
204: req, "tagsEntries"));
205:
206: String[] communityPermissions = req
207: .getParameterValues("communityPermissions");
208: String[] guestPermissions = req
209: .getParameterValues("guestPermissions");
210:
211: MBMessage message = null;
212:
213: if (messageId <= 0) {
214: CaptchaUtil.check(req);
215:
216: if (threadId <= 0) {
217:
218: // Post new thread
219:
220: message = MBMessageServiceUtil.addMessage(categoryId,
221: subject, body, files, anonymous, priority,
222: tagsEntries, prefs, communityPermissions,
223: guestPermissions, themeDisplay);
224: } else {
225:
226: // Post reply
227:
228: message = MBMessageServiceUtil.addMessage(categoryId,
229: threadId, parentMessageId, subject, body,
230: files, anonymous, priority, tagsEntries, prefs,
231: communityPermissions, guestPermissions,
232: themeDisplay);
233: }
234: } else {
235:
236: // Update message
237:
238: message = MBMessageServiceUtil.updateMessage(messageId,
239: subject, body, files, priority, tagsEntries, prefs,
240: themeDisplay);
241: }
242:
243: PortletURL portletURL = ((ActionResponseImpl) res)
244: .createRenderURL();
245:
246: portletURL.setParameter("struts_action",
247: "/message_boards/view_message");
248: portletURL.setParameter("messageId", String.valueOf(message
249: .getMessageId()));
250:
251: res.sendRedirect(portletURL.toString());
252: }
253:
254: }
|