Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.messageboards.action; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.ObjectValuePair; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.service.ServiceContextFactory; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.portal.util.WebKeys; import com.liferay.portlet.ActionResponseImpl; import com.liferay.portlet.messageboards.MessageBodyException; import com.liferay.portlet.messageboards.MessageSubjectException; import com.liferay.portlet.messageboards.NoSuchMessageException; import com.liferay.portlet.messageboards.NoSuchThreadException; import com.liferay.portlet.messageboards.RequiredMessageException; import com.liferay.portlet.messageboards.SplitThreadException; import com.liferay.portlet.messageboards.model.MBMessage; import com.liferay.portlet.messageboards.model.MBMessageConstants; import com.liferay.portlet.messageboards.model.MBThread; import com.liferay.portlet.messageboards.model.MBThreadConstants; import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil; import com.liferay.portlet.messageboards.service.MBMessageServiceUtil; import com.liferay.portlet.messageboards.service.MBThreadServiceUtil; import java.io.InputStream; import java.util.Collections; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.PortletPreferences; import javax.portlet.PortletURL; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author Jorge Ferrer */ public class SplitThreadAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { try { splitThread(actionRequest, actionResponse); } catch (Exception e) { if (e instanceof PrincipalException || e instanceof RequiredMessageException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.message_boards.error"); } else if (e instanceof MessageBodyException || e instanceof MessageSubjectException || e instanceof NoSuchThreadException || e instanceof SplitThreadException) { SessionErrors.add(actionRequest, e.getClass().getName()); } else { throw e; } } } @Override public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { try { ActionUtil.getMessage(renderRequest); } catch (Exception e) { if (e instanceof NoSuchMessageException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass().getName()); return mapping.findForward("portlet.message_boards.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.message_boards.split_thread")); } protected void splitThread(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { PortletPreferences preferences = actionRequest.getPreferences(); ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); long messageId = ParamUtil.getLong(actionRequest, "messageId"); String splitThreadSubject = ParamUtil.getString(actionRequest, "splitThreadSubject"); ServiceContext serviceContext = ServiceContextFactory.getInstance(MBThread.class.getName(), actionRequest); MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId); long oldThreadId = message.getThreadId(); long oldParentMessageId = message.getParentMessageId(); MBThread newThread = MBThreadServiceUtil.splitThread(messageId, splitThreadSubject, serviceContext); boolean addExplanationPost = ParamUtil.getBoolean(actionRequest, "addExplanationPost"); if (addExplanationPost) { String subject = ParamUtil.getString(actionRequest, "subject"); String body = ParamUtil.getString(actionRequest, "body"); String format = GetterUtil.getString(preferences.getValue("messageFormat", null), MBMessageConstants.DEFAULT_FORMAT); String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay); String newThreadURL = layoutFullURL + "/-/message_boards/view_message/" + message.getMessageId(); body = StringUtil.replace(body, new String[] { "${newThreadURL}", "[url=]" }, new String[] { newThreadURL, "[url=" + newThreadURL + "]" }); serviceContext.setAddGroupPermissions(true); serviceContext.setAddGuestPermissions(true); MBMessageServiceUtil.addMessage(message.getGroupId(), message.getCategoryId(), oldThreadId, oldParentMessageId, subject, body, format, Collections.<ObjectValuePair<String, InputStream>>emptyList(), false, MBThreadConstants.PRIORITY_NOT_GIVEN, message.getAllowPingbacks(), serviceContext); } PortletURL portletURL = ((ActionResponseImpl) actionResponse).createRenderURL(); portletURL.setParameter("struts_action", "/message_boards/view_message"); portletURL.setParameter("messageId", String.valueOf(newThread.getRootMessageId())); actionResponse.sendRedirect(portletURL.toString()); } }