1
14
15 package com.liferay.portlet.messageboards.action;
16
17 import com.liferay.documentlibrary.FileNameException;
18 import com.liferay.documentlibrary.FileSizeException;
19 import com.liferay.portal.kernel.captcha.CaptchaTextException;
20 import com.liferay.portal.kernel.captcha.CaptchaUtil;
21 import com.liferay.portal.kernel.servlet.SessionErrors;
22 import com.liferay.portal.kernel.upload.UploadPortletRequest;
23 import com.liferay.portal.kernel.util.Constants;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.ObjectValuePair;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.security.auth.PrincipalException;
29 import com.liferay.portal.service.ServiceContext;
30 import com.liferay.portal.service.ServiceContextFactory;
31 import com.liferay.portal.struts.PortletAction;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.PropsValues;
34 import com.liferay.portlet.ActionResponseImpl;
35 import com.liferay.portlet.messageboards.LockedThreadException;
36 import com.liferay.portlet.messageboards.MessageBodyException;
37 import com.liferay.portlet.messageboards.MessageSubjectException;
38 import com.liferay.portlet.messageboards.NoSuchMessageException;
39 import com.liferay.portlet.messageboards.RequiredMessageException;
40 import com.liferay.portlet.messageboards.model.MBMessage;
41 import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
42 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
43 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
44 import com.liferay.portlet.tags.TagsEntryException;
45
46 import java.io.File;
47
48 import java.util.ArrayList;
49 import java.util.List;
50
51 import javax.portlet.ActionRequest;
52 import javax.portlet.ActionResponse;
53 import javax.portlet.PortletConfig;
54 import javax.portlet.PortletURL;
55 import javax.portlet.RenderRequest;
56 import javax.portlet.RenderResponse;
57
58 import org.apache.struts.action.ActionForm;
59 import org.apache.struts.action.ActionForward;
60 import org.apache.struts.action.ActionMapping;
61
62
67 public class EditMessageAction extends PortletAction {
68
69 public void processAction(
70 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71 ActionRequest actionRequest, ActionResponse actionResponse)
72 throws Exception {
73
74 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
75
76 try {
77 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
78 updateMessage(actionRequest, actionResponse);
79 }
80 else if (cmd.equals(Constants.DELETE)) {
81 deleteMessage(actionRequest);
82 }
83 else if (cmd.equals(Constants.LOCK)) {
84 lockThread(actionRequest);
85 }
86 else if (cmd.equals(Constants.SUBSCRIBE)) {
87 subscribeMessage(actionRequest);
88 }
89 else if (cmd.equals(Constants.UNLOCK)) {
90 unlockThread(actionRequest);
91 }
92 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
93 unsubscribeMessage(actionRequest);
94 }
95
96 if (cmd.equals(Constants.DELETE) ||
97 cmd.equals(Constants.LOCK) ||
98 cmd.equals(Constants.SUBSCRIBE) ||
99 cmd.equals(Constants.UNSUBSCRIBE) ||
100 cmd.equals(Constants.UNLOCK)) {
101
102 sendRedirect(actionRequest, actionResponse);
103 }
104 }
105 catch (Exception e) {
106 if (e instanceof NoSuchMessageException ||
107 e instanceof PrincipalException ||
108 e instanceof RequiredMessageException) {
109
110 SessionErrors.add(actionRequest, e.getClass().getName());
111
112 setForward(actionRequest, "portlet.message_boards.error");
113 }
114 else if (e instanceof CaptchaTextException ||
115 e instanceof FileNameException ||
116 e instanceof FileSizeException ||
117 e instanceof LockedThreadException ||
118 e instanceof MessageBodyException ||
119 e instanceof MessageSubjectException) {
120
121 SessionErrors.add(actionRequest, e.getClass().getName());
122 }
123 else if (e instanceof TagsEntryException) {
124 SessionErrors.add(actionRequest, e.getClass().getName(), e);
125 }
126 else {
127 throw e;
128 }
129 }
130 }
131
132 public ActionForward render(
133 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
134 RenderRequest renderRequest, RenderResponse renderResponse)
135 throws Exception {
136
137 try {
138 ActionUtil.getMessage(renderRequest);
139 }
140 catch (Exception e) {
141 if (e instanceof NoSuchMessageException ||
142 e instanceof PrincipalException) {
143
144 SessionErrors.add(renderRequest, e.getClass().getName());
145
146 return mapping.findForward("portlet.message_boards.error");
147 }
148 else {
149 throw e;
150 }
151 }
152
153 return mapping.findForward(
154 getForward(renderRequest, "portlet.message_boards.edit_message"));
155 }
156
157 protected void deleteMessage(ActionRequest actionRequest) throws Exception {
158 long messageId = ParamUtil.getLong(actionRequest, "messageId");
159
160 MBMessageServiceUtil.deleteMessage(messageId);
161 }
162
163 protected void lockThread(ActionRequest actionRequest) throws Exception {
164 long threadId = ParamUtil.getLong(actionRequest, "threadId");
165
166 MBThreadServiceUtil.lockThread(threadId);
167 }
168
169 protected void subscribeMessage(ActionRequest actionRequest)
170 throws Exception {
171
172 long messageId = ParamUtil.getLong(actionRequest, "messageId");
173
174 MBMessageServiceUtil.subscribeMessage(messageId);
175 }
176
177 protected void unlockThread(ActionRequest actionRequest) throws Exception {
178 long threadId = ParamUtil.getLong(actionRequest, "threadId");
179
180 MBThreadServiceUtil.unlockThread(threadId);
181 }
182
183 protected void unsubscribeMessage(ActionRequest actionRequest)
184 throws Exception {
185
186 long messageId = ParamUtil.getLong(actionRequest, "messageId");
187
188 MBMessageServiceUtil.unsubscribeMessage(messageId);
189 }
190
191 protected void updateMessage(
192 ActionRequest actionRequest, ActionResponse actionResponse)
193 throws Exception {
194
195 long messageId = ParamUtil.getLong(actionRequest, "messageId");
196
197 long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
198 long threadId = ParamUtil.getLong(actionRequest, "threadId");
199 long parentMessageId = ParamUtil.getLong(
200 actionRequest, "parentMessageId");
201 String subject = ParamUtil.getString(actionRequest, "subject");
202 String body = ParamUtil.getString(actionRequest, "body");
203 boolean attachments = ParamUtil.getBoolean(
204 actionRequest, "attachments");
205
206 List<ObjectValuePair<String, byte[]>> files =
207 new ArrayList<ObjectValuePair<String, byte[]>>();
208
209 if (attachments) {
210 UploadPortletRequest uploadRequest =
211 PortalUtil.getUploadPortletRequest(actionRequest);
212
213 for (int i = 1; i <= 5; i++) {
214 File file = uploadRequest.getFile("msgFile" + i);
215 String fileName = uploadRequest.getFileName("msgFile" + i);
216 byte[] bytes = FileUtil.getBytes(file);
217
218 if ((bytes != null) && (bytes.length > 0)) {
219 ObjectValuePair<String, byte[]> ovp =
220 new ObjectValuePair<String, byte[]>(fileName, bytes);
221
222 files.add(ovp);
223 }
224 }
225 }
226
227 boolean question = ParamUtil.getBoolean(actionRequest, "question");
228 boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
229 double priority = ParamUtil.getDouble(actionRequest, "priority");
230 boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
231
232 ServiceContext serviceContext = ServiceContextFactory.getInstance(
233 MBMessage.class.getName(), actionRequest);
234
235 MBMessage message = null;
236
237 if (messageId <= 0) {
238 if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
239 CaptchaUtil.check(actionRequest);
240 }
241
242 if (threadId <= 0) {
243
244
246 message = MBMessageServiceUtil.addMessage(
247 categoryId, subject, body, files, anonymous, priority,
248 serviceContext);
249
250 if (question) {
251 MBMessageFlagLocalServiceUtil.addQuestionFlag(
252 message.getMessageId());
253 }
254 }
255 else {
256
257
259 message = MBMessageServiceUtil.addMessage(
260 categoryId, threadId, parentMessageId, subject, body, files,
261 anonymous, priority, serviceContext);
262 }
263 }
264 else {
265 List<String> existingFiles = new ArrayList<String>();
266
267 for (int i = 1; i <= 5; i++) {
268 String path = ParamUtil.getString(
269 actionRequest, "existingPath" + i);
270
271 if (Validator.isNotNull(path)) {
272 existingFiles.add(path);
273 }
274 }
275
276
278 message = MBMessageServiceUtil.updateMessage(
279 messageId, subject, body, files, existingFiles, priority,
280 serviceContext);
281
282 if (message.isRoot()) {
283 if (question) {
284 MBMessageFlagLocalServiceUtil.addQuestionFlag(messageId);
285 }
286 else {
287 MBMessageFlagLocalServiceUtil.deleteQuestionAndAnswerFlags(
288 message.getThreadId());
289 }
290 }
291 }
292
293 PortletURL portletURL =
294 ((ActionResponseImpl)actionResponse).createRenderURL();
295
296 portletURL.setParameter(
297 "struts_action", "/message_boards/view_message");
298 portletURL.setParameter(
299 "messageId", String.valueOf(message.getMessageId()));
300 portletURL.setParameter("preview", String.valueOf(preview));
301
302 actionResponse.sendRedirect(portletURL.toString());
303 }
304
305 }