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.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
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.MBMessage;
39  import com.liferay.portlet.messageboards.model.MBThread;
40  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
41  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
42  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
43  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
44  
45  import java.util.ArrayList;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
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 portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          try {
72              moveThread(actionRequest, actionResponse);
73          }
74          catch (Exception e) {
75              if (e instanceof PrincipalException ||
76                  e instanceof RequiredMessageException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.message_boards.error");
81              }
82              else if (e instanceof MessageBodyException ||
83                       e instanceof MessageSubjectException ||
84                       e instanceof NoSuchThreadException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87              }
88              else {
89                  throw e;
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          try {
100             ActionUtil.getThreadMessage(renderRequest);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchMessageException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.message_boards.move_thread"));
117     }
118 
119     protected void moveThread(
120             ActionRequest actionRequest, ActionResponse actionResponse)
121         throws Exception {
122 
123         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
124         long threadId = ParamUtil.getLong(actionRequest, "threadId");
125 
126         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
127 
128         MBThreadServiceUtil.moveThread(categoryId, threadId);
129 
130         boolean addExplanationPost = ParamUtil.getBoolean(
131             actionRequest, "addExplanationPost");
132 
133         if (addExplanationPost) {
134             String subject = ParamUtil.getString(actionRequest, "subject");
135             String body = ParamUtil.getString(actionRequest, "body");
136 
137             ServiceContext serviceContext = ServiceContextFactory.getInstance(
138                 MBMessage.class.getName(), actionRequest);
139 
140             MBMessageServiceUtil.addMessage(
141                 categoryId, threadId, thread.getRootMessageId(), subject, body,
142                 new ArrayList<ObjectValuePair<String, byte[]>>(), false,
143                 MBThreadImpl.PRIORITY_NOT_GIVEN, serviceContext);
144         }
145 
146         PortletURL portletURL =
147             ((ActionResponseImpl)actionResponse).createRenderURL();
148 
149         portletURL.setParameter(
150             "struts_action", "/message_boards/view_message");
151         portletURL.setParameter(
152             "messageId", String.valueOf(thread.getRootMessageId()));
153 
154         actionResponse.sendRedirect(portletURL.toString());
155     }
156 
157 }