1
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
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 }