1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ObjectValuePair;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.struts.PortletAction;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.ActionResponseImpl;
25  import com.liferay.portlet.messageboards.MessageBodyException;
26  import com.liferay.portlet.messageboards.MessageSubjectException;
27  import com.liferay.portlet.messageboards.NoSuchMessageException;
28  import com.liferay.portlet.messageboards.NoSuchThreadException;
29  import com.liferay.portlet.messageboards.RequiredMessageException;
30  import com.liferay.portlet.messageboards.model.MBThread;
31  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
32  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
33  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
34  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
35  
36  import java.util.ArrayList;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletConfig;
41  import javax.portlet.PortletPreferences;
42  import javax.portlet.PortletURL;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="MoveThreadAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Jorge Ferrer
54   */
55  public class MoveThreadAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          try {
63              moveThread(actionRequest, actionResponse);
64          }
65          catch (Exception e) {
66              if (e instanceof PrincipalException ||
67                  e instanceof RequiredMessageException) {
68  
69                  SessionErrors.add(actionRequest, e.getClass().getName());
70  
71                  setForward(actionRequest, "portlet.message_boards.error");
72              }
73              else if (e instanceof MessageBodyException ||
74                       e instanceof MessageSubjectException ||
75                       e instanceof NoSuchThreadException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78              }
79              else {
80                  throw e;
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          try {
91              ActionUtil.getThreadMessage(renderRequest);
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchMessageException ||
95                  e instanceof PrincipalException) {
96  
97                  SessionErrors.add(renderRequest, e.getClass().getName());
98  
99                  return mapping.findForward("portlet.message_boards.error");
100             }
101             else {
102                 throw e;
103             }
104         }
105 
106         return mapping.findForward(
107             getForward(renderRequest, "portlet.message_boards.move_thread"));
108     }
109 
110     protected void moveThread(
111             ActionRequest actionRequest, ActionResponse actionResponse)
112         throws Exception {
113 
114         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
115             WebKeys.THEME_DISPLAY);
116 
117         PortletPreferences prefs = actionRequest.getPreferences();
118 
119         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
120         long threadId = ParamUtil.getLong(actionRequest, "threadId");
121 
122         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
123 
124         MBThreadServiceUtil.moveThread(categoryId, threadId);
125 
126         boolean addExplanationPost = ParamUtil.getBoolean(
127             actionRequest, "addExplanationPost");
128 
129         if (addExplanationPost) {
130             String subject = ParamUtil.getString(actionRequest, "subject");
131             String body = ParamUtil.getString(actionRequest, "body");
132 
133             MBMessageServiceUtil.addMessage(
134                 categoryId, threadId, thread.getRootMessageId(), subject, body,
135                 new ArrayList<ObjectValuePair<String, byte[]>>(), false,
136                 MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
137                 themeDisplay);
138         }
139 
140         PortletURL portletURL =
141             ((ActionResponseImpl)actionResponse).createRenderURL();
142 
143         portletURL.setParameter(
144             "struts_action", "/message_boards/view_message");
145         portletURL.setParameter(
146             "messageId", String.valueOf(thread.getRootMessageId()));
147 
148         actionResponse.sendRedirect(portletURL.toString());
149     }
150 
151 }