1   /**
2    * Copyright (c) 2000-2008 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.util.ParamUtil;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.ActionResponseImpl;
31  import com.liferay.portlet.messageboards.MessageBodyException;
32  import com.liferay.portlet.messageboards.MessageSubjectException;
33  import com.liferay.portlet.messageboards.NoSuchMessageException;
34  import com.liferay.portlet.messageboards.NoSuchThreadException;
35  import com.liferay.portlet.messageboards.RequiredMessageException;
36  import com.liferay.portlet.messageboards.model.MBThread;
37  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
38  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
39  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
40  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
41  import com.liferay.util.servlet.SessionErrors;
42  
43  import java.util.ArrayList;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  import javax.portlet.PortletURL;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="MoveThreadAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Jorge Ferrer
61   *
62   */
63  public class MoveThreadAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig config,
67              ActionRequest req, ActionResponse res)
68          throws Exception {
69  
70          try {
71              moveThread(req, res);
72          }
73          catch (Exception e) {
74              if (e instanceof PrincipalException ||
75                  e instanceof RequiredMessageException) {
76  
77                  SessionErrors.add(req, e.getClass().getName());
78  
79                  setForward(req, "portlet.message_boards.error");
80              }
81              else if (e instanceof MessageBodyException ||
82                       e instanceof MessageSubjectException ||
83                       e instanceof NoSuchThreadException) {
84  
85                  SessionErrors.add(req, e.getClass().getName());
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig config,
95              RenderRequest req, RenderResponse res)
96          throws Exception {
97  
98          try {
99              ActionUtil.getThreadMessage(req);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchMessageException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(req, 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(req, "portlet.message_boards.move_thread"));
116     }
117 
118     protected void moveThread(ActionRequest req, ActionResponse res)
119         throws Exception {
120 
121         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
122             WebKeys.THEME_DISPLAY);
123 
124         PortletPreferences prefs = req.getPreferences();
125 
126         long categoryId = ParamUtil.getLong(req, "categoryId");
127         long threadId = ParamUtil.getLong(req, "threadId");
128 
129         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
130 
131         MBThreadServiceUtil.moveThread(categoryId, threadId);
132 
133         boolean addExplanationPost = ParamUtil.getBoolean(
134             req, "addExplanationPost");
135 
136         if (addExplanationPost) {
137             String subject = ParamUtil.getString(req, "subject");
138             String body = ParamUtil.getString(req, "body");
139 
140             MBMessageServiceUtil.addMessage(
141                 categoryId, threadId, thread.getRootMessageId(), subject, body,
142                 new ArrayList(), false, MBThreadImpl.PRIORITY_NOT_GIVEN,
143                 null, prefs, true, true, themeDisplay);
144         }
145 
146         PortletURL portletURL = ((ActionResponseImpl)res).createRenderURL();
147 
148         portletURL.setParameter(
149             "struts_action", "/message_boards/view_message");
150         portletURL.setParameter(
151             "messageId", String.valueOf(thread.getRootMessageId()));
152 
153         res.sendRedirect(portletURL.toString());
154     }
155 
156 }