1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.ActionResponseImpl;
27  import com.liferay.portlet.messageboards.MessageBodyException;
28  import com.liferay.portlet.messageboards.MessageSubjectException;
29  import com.liferay.portlet.messageboards.NoSuchMessageException;
30  import com.liferay.portlet.messageboards.NoSuchThreadException;
31  import com.liferay.portlet.messageboards.RequiredMessageException;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.model.MBThread;
34  import com.liferay.portlet.messageboards.model.MBThreadConstants;
35  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
36  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
37  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
38  
39  import java.util.ArrayList;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletURL;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="MoveThreadAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Jorge Ferrer
56   */
57  public class MoveThreadAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              moveThread(actionRequest, actionResponse);
66          }
67          catch (Exception e) {
68              if (e instanceof PrincipalException ||
69                  e instanceof RequiredMessageException) {
70  
71                  SessionErrors.add(actionRequest, e.getClass().getName());
72  
73                  setForward(actionRequest, "portlet.message_boards.error");
74              }
75              else if (e instanceof MessageBodyException ||
76                       e instanceof MessageSubjectException ||
77                       e instanceof NoSuchThreadException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80              }
81              else {
82                  throw e;
83              }
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89              RenderRequest renderRequest, RenderResponse renderResponse)
90          throws Exception {
91  
92          try {
93              ActionUtil.getThreadMessage(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchMessageException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.message_boards.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(
109             getForward(renderRequest, "portlet.message_boards.move_thread"));
110     }
111 
112     protected void moveThread(
113             ActionRequest actionRequest, ActionResponse actionResponse)
114         throws Exception {
115 
116         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
117             WebKeys.THEME_DISPLAY);
118 
119         long groupId = themeDisplay.getScopeGroupId();
120         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
121         long threadId = ParamUtil.getLong(actionRequest, "threadId");
122 
123         MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
124 
125         MBThreadServiceUtil.moveThread(categoryId, threadId);
126 
127         boolean addExplanationPost = ParamUtil.getBoolean(
128             actionRequest, "addExplanationPost");
129 
130         if (addExplanationPost) {
131             String subject = ParamUtil.getString(actionRequest, "subject");
132             String body = ParamUtil.getString(actionRequest, "body");
133 
134             ServiceContext serviceContext = ServiceContextFactory.getInstance(
135                 MBMessage.class.getName(), actionRequest);
136 
137             MBMessageServiceUtil.addMessage(
138                 groupId, categoryId, threadId, thread.getRootMessageId(),
139                 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
140                 false, MBThreadConstants.PRIORITY_NOT_GIVEN, false,
141                 serviceContext);
142         }
143 
144         PortletURL portletURL =
145             ((ActionResponseImpl)actionResponse).createRenderURL();
146 
147         portletURL.setParameter(
148             "struts_action", "/message_boards/view_message");
149         portletURL.setParameter(
150             "messageId", String.valueOf(thread.getRootMessageId()));
151 
152         actionResponse.sendRedirect(portletURL.toString());
153     }
154 
155 }