1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.service.ServiceContext;
22  import com.liferay.portal.service.ServiceContextFactory;
23  import com.liferay.portal.struts.PortletAction;
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.MBMessage;
31  import com.liferay.portlet.messageboards.model.MBThread;
32  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
33  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
34  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
35  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
36  
37  import java.util.ArrayList;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
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         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
115         long threadId = ParamUtil.getLong(actionRequest, "threadId");
116 
117         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
118 
119         MBThreadServiceUtil.moveThread(categoryId, threadId);
120 
121         boolean addExplanationPost = ParamUtil.getBoolean(
122             actionRequest, "addExplanationPost");
123 
124         if (addExplanationPost) {
125             String subject = ParamUtil.getString(actionRequest, "subject");
126             String body = ParamUtil.getString(actionRequest, "body");
127 
128             ServiceContext serviceContext = ServiceContextFactory.getInstance(
129                 MBMessage.class.getName(), actionRequest);
130 
131             MBMessageServiceUtil.addMessage(
132                 categoryId, threadId, thread.getRootMessageId(), subject, body,
133                 new ArrayList<ObjectValuePair<String, byte[]>>(), false,
134                 MBThreadImpl.PRIORITY_NOT_GIVEN, serviceContext);
135         }
136 
137         PortletURL portletURL =
138             ((ActionResponseImpl)actionResponse).createRenderURL();
139 
140         portletURL.setParameter(
141             "struts_action", "/message_boards/view_message");
142         portletURL.setParameter(
143             "messageId", String.valueOf(thread.getRootMessageId()));
144 
145         actionResponse.sendRedirect(portletURL.toString());
146     }
147 
148 }