001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.action;
022:
023: import com.liferay.portal.kernel.util.ParamUtil;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.security.auth.PrincipalException;
026: import com.liferay.portal.struts.PortletAction;
027: import com.liferay.portal.theme.ThemeDisplay;
028: import com.liferay.portal.util.PortalUtil;
029: import com.liferay.portal.util.WebKeys;
030: import com.liferay.portlet.ActionResponseImpl;
031: import com.liferay.portlet.messageboards.MessageBodyException;
032: import com.liferay.portlet.messageboards.MessageSubjectException;
033: import com.liferay.portlet.messageboards.NoSuchMessageException;
034: import com.liferay.portlet.messageboards.NoSuchThreadException;
035: import com.liferay.portlet.messageboards.RequiredMessageException;
036: import com.liferay.portlet.messageboards.model.MBMessage;
037: import com.liferay.portlet.messageboards.model.MBThread;
038: import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
039: import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
040: import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
041: import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
042: import com.liferay.util.servlet.SessionErrors;
043:
044: import java.util.ArrayList;
045:
046: import javax.portlet.ActionRequest;
047: import javax.portlet.ActionResponse;
048: import javax.portlet.PortletConfig;
049: import javax.portlet.PortletPreferences;
050: import javax.portlet.PortletURL;
051: import javax.portlet.RenderRequest;
052: import javax.portlet.RenderResponse;
053:
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * <a href="SplitThreadAction.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Jorge Ferrer
062: *
063: */
064: public class SplitThreadAction extends PortletAction {
065:
066: public void processAction(ActionMapping mapping, ActionForm form,
067: PortletConfig config, ActionRequest req, ActionResponse res)
068: throws Exception {
069:
070: try {
071: splitThread(req, res);
072: } catch (Exception e) {
073: if (e instanceof PrincipalException
074: || e instanceof RequiredMessageException) {
075:
076: SessionErrors.add(req, e.getClass().getName());
077:
078: setForward(req, "portlet.message_boards.error");
079: } else if (e instanceof MessageBodyException
080: || e instanceof MessageSubjectException
081: || e instanceof NoSuchThreadException) {
082:
083: SessionErrors.add(req, e.getClass().getName());
084: } else {
085: throw e;
086: }
087: }
088: }
089:
090: public ActionForward render(ActionMapping mapping, ActionForm form,
091: PortletConfig config, RenderRequest req, RenderResponse res)
092: throws Exception {
093:
094: try {
095: ActionUtil.getMessage(req);
096: } catch (Exception e) {
097: if (e instanceof NoSuchMessageException
098: || e instanceof PrincipalException) {
099:
100: SessionErrors.add(req, e.getClass().getName());
101:
102: return mapping
103: .findForward("portlet.message_boards.error");
104: } else {
105: throw e;
106: }
107: }
108:
109: return mapping.findForward(getForward(req,
110: "portlet.message_boards.split_thread"));
111: }
112:
113: protected void splitThread(ActionRequest req, ActionResponse res)
114: throws Exception {
115:
116: ThemeDisplay themeDisplay = (ThemeDisplay) req
117: .getAttribute(WebKeys.THEME_DISPLAY);
118:
119: PortletPreferences prefs = req.getPreferences();
120:
121: long messageId = ParamUtil.getLong(req, "messageId");
122:
123: MBMessage message = MBMessageLocalServiceUtil
124: .getMessage(messageId);
125:
126: long oldThreadId = message.getThreadId();
127: long oldParentMessageId = message.getParentMessageId();
128:
129: MBThread newThread = MBThreadServiceUtil.splitThread(messageId,
130: prefs, themeDisplay);
131:
132: boolean addExplanationPost = ParamUtil.getBoolean(req,
133: "addExplanationPost");
134:
135: if (addExplanationPost) {
136: String subject = ParamUtil.getString(req, "subject");
137: String body = ParamUtil.getString(req, "body");
138:
139: String portalURL = PortalUtil.getPortalURL(themeDisplay);
140: String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
141:
142: String newThreadURL = portalURL + layoutURL
143: + "/message_boards/message/"
144: + message.getMessageId();
145:
146: body = StringUtil.replace(body,
147: new String[] { "[$NEW_THREAD_URL$]", },
148: new String[] { newThreadURL });
149:
150: MBMessageServiceUtil.addMessage(message.getCategoryId(),
151: oldThreadId, oldParentMessageId, subject, body,
152: new ArrayList(), false,
153: MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true,
154: true, themeDisplay);
155: }
156:
157: PortletURL portletURL = ((ActionResponseImpl) res)
158: .createRenderURL();
159:
160: portletURL.setParameter("struts_action",
161: "/message_boards/view_message");
162: portletURL.setParameter("messageId", String.valueOf(newThread
163: .getRootMessageId()));
164:
165: res.sendRedirect(portletURL.toString());
166: }
167:
168: }
|