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.ObjectValuePair;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.ActionResponseImpl;
32  import com.liferay.portlet.messageboards.MessageBodyException;
33  import com.liferay.portlet.messageboards.MessageSubjectException;
34  import com.liferay.portlet.messageboards.NoSuchMessageException;
35  import com.liferay.portlet.messageboards.NoSuchThreadException;
36  import com.liferay.portlet.messageboards.RequiredMessageException;
37  import com.liferay.portlet.messageboards.model.MBThread;
38  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
39  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
40  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
41  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
42  import com.liferay.util.servlet.SessionErrors;
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   */
64  public class MoveThreadAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig config,
68              ActionRequest req, ActionResponse res)
69          throws Exception {
70  
71          try {
72              moveThread(req, res);
73          }
74          catch (Exception e) {
75              if (e instanceof PrincipalException ||
76                  e instanceof RequiredMessageException) {
77  
78                  SessionErrors.add(req, e.getClass().getName());
79  
80                  setForward(req, "portlet.message_boards.error");
81              }
82              else if (e instanceof MessageBodyException ||
83                       e instanceof MessageSubjectException ||
84                       e instanceof NoSuchThreadException) {
85  
86                  SessionErrors.add(req, e.getClass().getName());
87              }
88              else {
89                  throw e;
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig config,
96              RenderRequest req, RenderResponse res)
97          throws Exception {
98  
99          try {
100             ActionUtil.getThreadMessage(req);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchMessageException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(req, e.getClass().getName());
107 
108                 return mapping.findForward("portlet.message_boards.error");
109             }
110             else {
111                 throw e;
112             }
113         }
114 
115         return mapping.findForward(
116             getForward(req, "portlet.message_boards.move_thread"));
117     }
118 
119     protected void moveThread(ActionRequest req, ActionResponse res)
120         throws Exception {
121 
122         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
123             WebKeys.THEME_DISPLAY);
124 
125         PortletPreferences prefs = req.getPreferences();
126 
127         long categoryId = ParamUtil.getLong(req, "categoryId");
128         long threadId = ParamUtil.getLong(req, "threadId");
129 
130         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
131 
132         MBThreadServiceUtil.moveThread(categoryId, threadId);
133 
134         boolean addExplanationPost = ParamUtil.getBoolean(
135             req, "addExplanationPost");
136 
137         if (addExplanationPost) {
138             String subject = ParamUtil.getString(req, "subject");
139             String body = ParamUtil.getString(req, "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 = ((ActionResponseImpl)res).createRenderURL();
149 
150         portletURL.setParameter(
151             "struts_action", "/message_boards/view_message");
152         portletURL.setParameter(
153             "messageId", String.valueOf(thread.getRootMessageId()));
154 
155         res.sendRedirect(portletURL.toString());
156     }
157 
158 }