1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ObjectValuePair;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionResponseImpl;
33  import com.liferay.portlet.messageboards.MessageBodyException;
34  import com.liferay.portlet.messageboards.MessageSubjectException;
35  import com.liferay.portlet.messageboards.NoSuchMessageException;
36  import com.liferay.portlet.messageboards.NoSuchThreadException;
37  import com.liferay.portlet.messageboards.RequiredMessageException;
38  import com.liferay.portlet.messageboards.model.MBThread;
39  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
40  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
41  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
42  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
43  
44  import java.util.ArrayList;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
49  import javax.portlet.PortletPreferences;
50  import javax.portlet.PortletURL;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="MoveThreadAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Jorge Ferrer
62   */
63  public class MoveThreadAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          try {
71              moveThread(actionRequest, actionResponse);
72          }
73          catch (Exception e) {
74              if (e instanceof PrincipalException ||
75                  e instanceof RequiredMessageException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.message_boards.error");
80              }
81              else if (e instanceof MessageBodyException ||
82                       e instanceof MessageSubjectException ||
83                       e instanceof NoSuchThreadException) {
84  
85                  SessionErrors.add(actionRequest, e.getClass().getName());
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          try {
99              ActionUtil.getThreadMessage(renderRequest);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchMessageException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(renderRequest, e.getClass().getName());
106 
107                 return mapping.findForward("portlet.message_boards.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113 
114         return mapping.findForward(
115             getForward(renderRequest, "portlet.message_boards.move_thread"));
116     }
117 
118     protected void moveThread(
119             ActionRequest actionRequest, ActionResponse actionResponse)
120         throws Exception {
121 
122         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
123             WebKeys.THEME_DISPLAY);
124 
125         PortletPreferences prefs = actionRequest.getPreferences();
126 
127         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
128         long threadId = ParamUtil.getLong(actionRequest, "threadId");
129 
130         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
131 
132         MBThreadServiceUtil.moveThread(categoryId, threadId);
133 
134         boolean addExplanationPost = ParamUtil.getBoolean(
135             actionRequest, "addExplanationPost");
136 
137         if (addExplanationPost) {
138             String subject = ParamUtil.getString(actionRequest, "subject");
139             String body = ParamUtil.getString(actionRequest, "body");
140 
141             MBMessageServiceUtil.addMessage(
142                 categoryId, threadId, thread.getRootMessageId(), subject, body,
143                 new ArrayList<ObjectValuePair<String, byte[]>>(), false,
144                 MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
145                 themeDisplay);
146         }
147 
148         PortletURL portletURL =
149             ((ActionResponseImpl)actionResponse).createRenderURL();
150 
151         portletURL.setParameter(
152             "struts_action", "/message_boards/view_message");
153         portletURL.setParameter(
154             "messageId", String.valueOf(thread.getRootMessageId()));
155 
156         actionResponse.sendRedirect(portletURL.toString());
157     }
158 
159 }