/*
* (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package com.nabhinc.portlet.htmlblock;
import java.io.IOException;
import java.io.Writer;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletSecurityException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.nabhinc.portal.api.EntityLockedException;
import com.nabhinc.portal.api.PortalInformationStoreLocator;
import com.nabhinc.portal.container.ActionRequestImpl;
import com.nabhinc.portal.container.PortalInterface;
import com.nabhinc.portal.container.RenderRequestImpl;
import com.nabhinc.portal.core.PortalConstants;
import com.nabhinc.portal.model.PortalApplication;
import com.nabhinc.portal.model.PortalApplicationView;
import com.nabhinc.portlet.common.StatefulPortlet;
import com.nabhinc.util.StringUtil;
import com.nabhinc.ws.core.WebServiceSecurityException;
/**
*
*
* @author Padmanabh Dabke
* (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
*/
public class HTMLBlockPortlet extends GenericPortlet implements StatefulPortlet {
public void render(RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException {
String pMode = request.getPortletMode().toString().toLowerCase();
if (pMode.equals("view")) doView(request, response);
else if (pMode.equals("config")) doConfig(request, response);
else if (pMode.equals("help")) doHelp(request, response);
else if (pMode.equals("preview")) doPreview(request, response);
else throw new PortletException("Unsupported portlet mode: " + pMode);
}
/**
* Displays portlet content configured via "content" parameter.
* The default implementation throws an exception.
* @param request The portlet request.
* @param response The render response.
* @exception PortletException If the portlet cannot fulfilling the request.
* @exception IOException If the streaming causes an I/O problem
*/
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException {
viewHelper(request, response, false);
}
protected void doPreview(RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException {
viewHelper(request, response, true);
}
private void viewHelper(RenderRequest request, RenderResponse response, boolean isPreview)
throws PortletException, java.io.IOException {
Writer w = response.getWriter();
w.write("<script type=\"text/javascript\">/*<![CDATA[*/");
w.write("sb_register_unload_callback(null, '");
w.write(response.getNamespace());
w.write("');");
w.write("sb_register_load_callback(null, '");
w.write(response.getNamespace());
w.write("');");
w.write("/*]]>*/</script>");
String title = "Rich Text";
String content = "";
PortalInterface pi = ((RenderRequestImpl) request).getPortalInterface();
HTMLBlockConfig config = (HTMLBlockConfig) pi.getPortletWindowConfig();
if (config != null) {
if (isPreview) {
title = config.getDraftTitle() == null ? config.getTitle() : config.getDraftTitle();
content = config.getDraftContent() == null ? config.getContent() : config.getDraftContent();
} else {
if (config.getTitle() != null) title = config.getTitle();
if (config.getContent() != null) content = config.getContent();
}
response.setTitle(title);
}
w.write(content);
w.flush();
}
/**
* Helper method to serve up the <code>edit</code> mode.
* The default implementation throws an exception.
* @param request The portlet request
* @param response The render response
* @exception PortletException If the portlet cannot fulfilling the request
* @exception IOException If the streaming causes an I/O problem
*/
protected void doConfig(RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException {
Writer w = response.getWriter();
String title = "Rich Text";
String content = "";
PortalInterface pi = ((RenderRequestImpl) request).getPortalInterface();
HTMLBlockConfig config = (HTMLBlockConfig) pi.getPortletWindowConfig();
if (config != null) {
title = config.getDraftTitle() == null ? config.getTitle() : config.getDraftTitle();
content = config.getDraftContent() == null ? config.getContent() : config.getDraftContent();
response.setTitle(title);
}
String textAreaId = response.getNamespace() + "_textarea";
w.write("<form method=\"post\" action=\"");
w.write(response.createActionURL().toString());
w.write("\">");
w.write("<p><b>Title</b><br/>");
w.write("<input name=\"title\" value=\"");
w.write(title);
w.write("\" /></p><b>Content</b><br/>");
w.write("<textarea id=\"");
w.write(textAreaId);
w.write("\" name=\"content\">");
w.write(content);
w.write("</textarea>");
w.write("<p style=\"text-align: left;\">");
w.write("<input class=\"button\" type=\"submit\" name=\"save_draft\" value=\"Save Draft\" onclick=\"tinyMCE.triggerSave();return true;\" />");
w.write("<input class=\"button\" type=\"submit\" name=\"publish\" value=\"Publish\" onclick=\"tinyMCE.triggerSave();return true;\" />");
w.write("<input class=\"button\" type=\"submit\" name=\"cancel\" value=\"Cancel\" />");
w.write("</p>");
w.write("</center></form>");
w.write("<script type=\"text/javascript\">");
w.write("sb_register_unload_callback(sb_remove_tiny_mce_control, '");
w.write(response.getNamespace());
w.write("');");
w.write("tinyMCE.execCommand('mceAddControl', true, '");
w.write(textAreaId);
w.write("');");
w.write("</script>");
}
/**
* Helper method to serve up the <code>help</code> mode.
* The default implementation throws an exception.
* @param request The portlet request
* @param response The render response
* @exception PortletException If the portlet cannot fulfilling the request
* @exception IOException If the streaming causes an I/O problem
*/
protected void doHelp(RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException {
Writer w = response.getWriter();
w.write(request.getPreferences().getValue("help", "Help is not available."));
w.flush();
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
String content = request.getParameter("content");
if (request.getParameter("cancel") != null) {
try {
response.setPortletMode(PortletMode.VIEW);
} catch (PortletModeException e) {
// Ignore. we are supporting view mode
e.printStackTrace();
}
return;
}
if (StringUtil.isNullOrEmpty(content)) return;
PortalApplication portalApp = ((PortalApplicationView) request.getAttribute(PortalConstants.CURRENT_PORTAL_APP_VIEW)).getPortalApplication();
synchronized (portalApp) {
PortalInterface pi = ((ActionRequestImpl) request).getPortalInterface();
HTMLBlockConfig blockState = (HTMLBlockConfig) pi.getPortletWindowConfig();
if (blockState == null) {
blockState = new HTMLBlockConfig();
pi.setPortletWindowConfig(blockState);
}
String title = request.getParameter("title");
if (StringUtil.isNullOrEmpty(title)) title = "Rich Text";
if (request.getParameter("publish") != null) {
blockState.setTitle(title);
blockState.setContent(content);
blockState.setDraftTitle(null);
blockState.setDraftContent(null);
try {
response.setPortletMode(PortletMode.VIEW);
} catch (PortletModeException e) {
// this will never be thrown
e.printStackTrace();
}
} else {
blockState.setDraftContent(content);
blockState.setDraftTitle(title);
try {
response.setPortletMode(new PortletMode("preview"));
} catch (PortletModeException e) {
// this will never be thrown
e.printStackTrace();
}
}
try {
PortalInformationStoreLocator.getPortalInformationStore().savePortalApplication(portalApp, true);
} catch (EntityLockedException e) {
throw new PortletException("You changes could not be persisted since the psite is locked by another user.");
} catch (WebServiceSecurityException e) {
throw new PortletSecurityException(e);
}
}
}
}
|