001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
018    import com.liferay.portal.kernel.captcha.CaptchaTextException;
019    import com.liferay.portal.kernel.captcha.CaptchaUtil;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.upload.UploadPortletRequest;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.ObjectValuePair;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.permission.ActionKeys;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.ServiceContextFactory;
035    import com.liferay.portal.struts.PortletAction;
036    import com.liferay.portal.theme.ThemeDisplay;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PropsValues;
039    import com.liferay.portal.util.WebKeys;
040    import com.liferay.portlet.ActionResponseImpl;
041    import com.liferay.portlet.asset.AssetCategoryException;
042    import com.liferay.portlet.asset.AssetTagException;
043    import com.liferay.portlet.documentlibrary.FileExtensionException;
044    import com.liferay.portlet.documentlibrary.FileNameException;
045    import com.liferay.portlet.documentlibrary.FileSizeException;
046    import com.liferay.portlet.messageboards.LockedThreadException;
047    import com.liferay.portlet.messageboards.MessageBodyException;
048    import com.liferay.portlet.messageboards.MessageSubjectException;
049    import com.liferay.portlet.messageboards.NoSuchMessageException;
050    import com.liferay.portlet.messageboards.RequiredMessageException;
051    import com.liferay.portlet.messageboards.model.MBMessage;
052    import com.liferay.portlet.messageboards.model.MBMessageConstants;
053    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
054    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
055    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
056    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
057    
058    import java.io.InputStream;
059    
060    import java.util.ArrayList;
061    import java.util.List;
062    
063    import javax.portlet.ActionRequest;
064    import javax.portlet.ActionResponse;
065    import javax.portlet.PortletConfig;
066    import javax.portlet.PortletPreferences;
067    import javax.portlet.PortletURL;
068    import javax.portlet.RenderRequest;
069    import javax.portlet.RenderResponse;
070    
071    import org.apache.struts.action.ActionForm;
072    import org.apache.struts.action.ActionForward;
073    import org.apache.struts.action.ActionMapping;
074    
075    /**
076     * @author Brian Wing Shun Chan
077     * @author Daniel Sanz
078     * @author Shuyang Zhou
079     */
080    public class EditMessageAction extends PortletAction {
081    
082            @Override
083            public void processAction(
084                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085                            ActionRequest actionRequest, ActionResponse actionResponse)
086                    throws Exception {
087    
088                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
089    
090                    try {
091                            MBMessage message = null;
092    
093                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
094                                    message = updateMessage(actionRequest, actionResponse);
095                            }
096                            else if (cmd.equals(Constants.DELETE)) {
097                                    deleteMessage(actionRequest);
098                            }
099                            else if (cmd.equals(Constants.LOCK)) {
100                                    lockThreads(actionRequest);
101                            }
102                            else if (cmd.equals(Constants.SUBSCRIBE)) {
103                                    subscribeMessage(actionRequest);
104                            }
105                            else if (cmd.equals(Constants.UNLOCK)) {
106                                    unlockThreads(actionRequest);
107                            }
108                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
109                                    unsubscribeMessage(actionRequest);
110                            }
111    
112                            if (Validator.isNotNull(cmd)) {
113                                    String redirect = getRedirect(
114                                            actionRequest, actionResponse, message);
115    
116                                    sendRedirect(actionRequest, actionResponse, redirect);
117                            }
118                    }
119                    catch (Exception e) {
120                            if (e instanceof NoSuchMessageException ||
121                                    e instanceof PrincipalException ||
122                                    e instanceof RequiredMessageException) {
123    
124                                    SessionErrors.add(actionRequest, e.getClass().getName());
125    
126                                    setForward(actionRequest, "portlet.message_boards.error");
127                            }
128                            else if (e instanceof CaptchaMaxChallengesException ||
129                                             e instanceof CaptchaTextException ||
130                                             e instanceof FileExtensionException ||
131                                             e instanceof FileNameException ||
132                                             e instanceof FileSizeException ||
133                                             e instanceof LockedThreadException ||
134                                             e instanceof MessageBodyException ||
135                                             e instanceof MessageSubjectException) {
136    
137                                    SessionErrors.add(actionRequest, e.getClass().getName());
138                            }
139                            else if (e instanceof AssetCategoryException ||
140                                             e instanceof AssetTagException) {
141    
142                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
143                            }
144                            else {
145                                    throw e;
146                            }
147                    }
148            }
149    
150            @Override
151            public ActionForward render(
152                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
153                            RenderRequest renderRequest, RenderResponse renderResponse)
154                    throws Exception {
155    
156                    try {
157                            ActionUtil.getMessage(renderRequest);
158                    }
159                    catch (Exception e) {
160                            if (e instanceof NoSuchMessageException ||
161                                    e instanceof PrincipalException) {
162    
163                                    SessionErrors.add(renderRequest, e.getClass().getName());
164    
165                                    return mapping.findForward("portlet.message_boards.error");
166                            }
167                            else {
168                                    throw e;
169                            }
170                    }
171    
172                    return mapping.findForward(
173                            getForward(renderRequest, "portlet.message_boards.edit_message"));
174            }
175    
176            protected void deleteMessage(ActionRequest actionRequest) throws Exception {
177                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
178    
179                    MBMessageServiceUtil.deleteMessage(messageId);
180            }
181    
182            protected String getRedirect(
183                    ActionRequest actionRequest, ActionResponse actionResponse,
184                    MBMessage message) {
185    
186                    if (message == null) {
187                            String redirect = ParamUtil.getString(actionRequest, "redirect");
188    
189                            return redirect;
190                    }
191    
192                    int workflowAction = ParamUtil.getInteger(
193                            actionRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
194    
195                    if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
196                            return getSaveAndContinueRedirect(
197                                    actionRequest, actionResponse, message);
198                    }
199                    else if (message == null) {
200                            return ParamUtil.getString(actionRequest, "redirect");
201                    }
202    
203                    ActionResponseImpl actionResponseImpl =
204                            (ActionResponseImpl)actionResponse;
205    
206                    PortletURL portletURL = actionResponseImpl.createRenderURL();
207    
208                    portletURL.setParameter(
209                            "struts_action", "/message_boards/view_message");
210                    portletURL.setParameter(
211                            "messageId", String.valueOf(message.getMessageId()));
212    
213                    return portletURL.toString();
214            }
215    
216            protected String getSaveAndContinueRedirect(
217                    ActionRequest actionRequest, ActionResponse actionResponse,
218                    MBMessage message) {
219    
220                    String redirect = ParamUtil.getString(actionRequest, "redirect");
221    
222                    boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
223    
224                    PortletURL portletURL =
225                            ((ActionResponseImpl)actionResponse).createRenderURL();
226    
227                    portletURL.setParameter(
228                            "struts_action", "/message_boards/edit_message");
229                    portletURL.setParameter("redirect", redirect);
230                    portletURL.setParameter(
231                            "messageId", String.valueOf(message.getMessageId()));
232                    portletURL.setParameter("preview", String.valueOf(preview));
233    
234                    return portletURL.toString();
235            }
236    
237            protected void lockThreads(ActionRequest actionRequest) throws Exception {
238                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
239    
240                    if (threadId > 0) {
241                            MBThreadServiceUtil.lockThread(threadId);
242                    }
243                    else {
244                            long[] threadIds = StringUtil.split(
245                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
246    
247                            for (int i = 0; i < threadIds.length; i++) {
248                                    MBThreadServiceUtil.lockThread(threadIds[i]);
249                            }
250                    }
251            }
252    
253            protected void subscribeMessage(ActionRequest actionRequest)
254                    throws Exception {
255    
256                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
257    
258                    MBMessageServiceUtil.subscribeMessage(messageId);
259            }
260    
261            protected void unlockThreads(ActionRequest actionRequest) throws Exception {
262                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
263    
264                    if (threadId > 0) {
265                            MBThreadServiceUtil.unlockThread(threadId);
266                    }
267                    else {
268                            long[] threadIds = StringUtil.split(
269                                    ParamUtil.getString(actionRequest, "threadIds"), 0L);
270    
271                            for (int i = 0; i < threadIds.length; i++) {
272                                    MBThreadServiceUtil.unlockThread(threadIds[i]);
273                            }
274                    }
275            }
276    
277            protected void unsubscribeMessage(ActionRequest actionRequest)
278                    throws Exception {
279    
280                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
281    
282                    MBMessageServiceUtil.unsubscribeMessage(messageId);
283            }
284    
285            protected MBMessage updateMessage(
286                            ActionRequest actionRequest, ActionResponse actionResponse)
287                    throws Exception {
288    
289                    PortletPreferences preferences = actionRequest.getPreferences();
290    
291                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
292                            WebKeys.THEME_DISPLAY);
293    
294                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
295    
296                    long groupId = themeDisplay.getScopeGroupId();
297                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
298                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
299                    long parentMessageId = ParamUtil.getLong(
300                            actionRequest, "parentMessageId");
301                    String subject = ParamUtil.getString(actionRequest, "subject");
302                    String body = ParamUtil.getString(actionRequest, "body");
303    
304                    String format = GetterUtil.getString(
305                            preferences.getValue("messageFormat", null),
306                            MBMessageConstants.DEFAULT_FORMAT);
307    
308                    boolean attachments = ParamUtil.getBoolean(
309                            actionRequest, "attachments");
310    
311                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
312                            new ArrayList<ObjectValuePair<String, InputStream>>(5);
313    
314                    try {
315                            if (attachments) {
316                                    UploadPortletRequest uploadPortletRequest =
317                                            PortalUtil.getUploadPortletRequest(actionRequest);
318    
319                                    for (int i = 1; i <= 5; i++) {
320                                            String fileName = uploadPortletRequest.getFileName(
321                                                    "msgFile" + i);
322                                            InputStream inputStream =
323                                                    uploadPortletRequest.getFileAsStream("msgFile" + i);
324    
325                                            if (inputStream == null) {
326                                                    continue;
327                                            }
328    
329                                            ObjectValuePair<String, InputStream> inputStreamOVP =
330                                                    new ObjectValuePair<String, InputStream>(
331                                                            fileName, inputStream);
332    
333                                            inputStreamOVPs.add(inputStreamOVP);
334                                    }
335                            }
336    
337                            boolean question = ParamUtil.getBoolean(actionRequest, "question");
338                            boolean anonymous = ParamUtil.getBoolean(
339                                    actionRequest, "anonymous");
340                            double priority = ParamUtil.getDouble(actionRequest, "priority");
341                            boolean allowPingbacks = ParamUtil.getBoolean(
342                                    actionRequest, "allowPingbacks");
343    
344                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
345                                    MBMessage.class.getName(), actionRequest);
346    
347                            boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
348    
349                            serviceContext.setAttribute("preview", preview);
350    
351                            MBMessage message = null;
352    
353                            if (messageId <= 0) {
354                                    if (PropsValues.
355                                                    CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
356    
357                                            CaptchaUtil.check(actionRequest);
358                                    }
359    
360                                    if (threadId <= 0) {
361    
362                                            // Post new thread
363    
364                                            message = MBMessageServiceUtil.addMessage(
365                                                    groupId, categoryId, subject, body, format,
366                                                    inputStreamOVPs, anonymous, priority, allowPingbacks,
367                                                    serviceContext);
368    
369                                            if (question) {
370                                                    MBThreadLocalServiceUtil.updateQuestion(
371                                                            message.getThreadId(), true);
372                                            }
373                                    }
374                                    else {
375    
376                                            // Post reply
377    
378                                            message = MBMessageServiceUtil.addMessage(
379                                                    groupId, categoryId, threadId, parentMessageId, subject,
380                                                    body, format, inputStreamOVPs, anonymous, priority,
381                                                    allowPingbacks, serviceContext);
382                                    }
383                            }
384                            else {
385                                    List<String> existingFiles = new ArrayList<String>();
386    
387                                    for (int i = 1; i <= 5; i++) {
388                                            String path = ParamUtil.getString(
389                                                    actionRequest, "existingPath" + i);
390    
391                                            if (Validator.isNotNull(path)) {
392                                                    existingFiles.add(path);
393                                            }
394                                    }
395    
396                                    // Update message
397    
398                                    message = MBMessageServiceUtil.updateMessage(
399                                            messageId, subject, body, inputStreamOVPs, existingFiles,
400                                            priority, allowPingbacks, serviceContext);
401    
402                                    if (message.isRoot()) {
403                                            MBThreadLocalServiceUtil.updateQuestion(
404                                                    message.getThreadId(), question);
405                                    }
406                            }
407    
408                            PermissionChecker permissionChecker =
409                                    themeDisplay.getPermissionChecker();
410    
411                            boolean subscribe = ParamUtil.getBoolean(
412                                    actionRequest, "subscribe");
413    
414                            if (subscribe &&
415                                    MBMessagePermission.contains(
416                                            permissionChecker, message, ActionKeys.SUBSCRIBE)) {
417    
418                                    MBMessageServiceUtil.subscribeMessage(message.getMessageId());
419                            }
420    
421                            return message;
422                    }
423                    finally {
424                            if (attachments) {
425                                    for (ObjectValuePair<String, InputStream> inputStreamOVP :
426                                                    inputStreamOVPs) {
427    
428                                            InputStream inputStream = inputStreamOVP.getValue();
429    
430                                            StreamUtil.cleanUp(inputStream);
431                                    }
432                            }
433                    }
434            }
435    
436    }