001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portlet.htmlblock;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.GenericPortlet;
028: import javax.portlet.PortletException;
029: import javax.portlet.PortletMode;
030: import javax.portlet.PortletModeException;
031: import javax.portlet.PortletSecurityException;
032: import javax.portlet.RenderRequest;
033: import javax.portlet.RenderResponse;
034:
035: import com.nabhinc.portal.api.EntityLockedException;
036: import com.nabhinc.portal.api.PortalInformationStoreLocator;
037: import com.nabhinc.portal.container.ActionRequestImpl;
038: import com.nabhinc.portal.container.PortalInterface;
039: import com.nabhinc.portal.container.RenderRequestImpl;
040: import com.nabhinc.portal.core.PortalConstants;
041: import com.nabhinc.portal.model.PortalApplication;
042: import com.nabhinc.portal.model.PortalApplicationView;
043: import com.nabhinc.portlet.common.StatefulPortlet;
044: import com.nabhinc.util.StringUtil;
045: import com.nabhinc.ws.core.WebServiceSecurityException;
046:
047: /**
048: *
049: *
050: * @author Padmanabh Dabke
051: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
052: */
053: public class HTMLBlockPortlet extends GenericPortlet implements
054: StatefulPortlet {
055:
056: public void render(RenderRequest request, RenderResponse response)
057: throws PortletException, java.io.IOException {
058: String pMode = request.getPortletMode().toString()
059: .toLowerCase();
060: if (pMode.equals("view"))
061: doView(request, response);
062: else if (pMode.equals("config"))
063: doConfig(request, response);
064: else if (pMode.equals("help"))
065: doHelp(request, response);
066: else if (pMode.equals("preview"))
067: doPreview(request, response);
068: else
069: throw new PortletException("Unsupported portlet mode: "
070: + pMode);
071: }
072:
073: /**
074: * Displays portlet content configured via "content" parameter.
075: * The default implementation throws an exception.
076: * @param request The portlet request.
077: * @param response The render response.
078: * @exception PortletException If the portlet cannot fulfilling the request.
079: * @exception IOException If the streaming causes an I/O problem
080: */
081: protected void doView(RenderRequest request, RenderResponse response)
082: throws PortletException, java.io.IOException {
083: viewHelper(request, response, false);
084: }
085:
086: protected void doPreview(RenderRequest request,
087: RenderResponse response) throws PortletException,
088: java.io.IOException {
089: viewHelper(request, response, true);
090: }
091:
092: private void viewHelper(RenderRequest request,
093: RenderResponse response, boolean isPreview)
094: throws PortletException, java.io.IOException {
095: Writer w = response.getWriter();
096: w.write("<script type=\"text/javascript\">/*<![CDATA[*/");
097: w.write("sb_register_unload_callback(null, '");
098: w.write(response.getNamespace());
099: w.write("');");
100: w.write("sb_register_load_callback(null, '");
101: w.write(response.getNamespace());
102: w.write("');");
103: w.write("/*]]>*/</script>");
104:
105: String title = "Rich Text";
106: String content = "";
107: PortalInterface pi = ((RenderRequestImpl) request)
108: .getPortalInterface();
109: HTMLBlockConfig config = (HTMLBlockConfig) pi
110: .getPortletWindowConfig();
111: if (config != null) {
112: if (isPreview) {
113: title = config.getDraftTitle() == null ? config
114: .getTitle() : config.getDraftTitle();
115: content = config.getDraftContent() == null ? config
116: .getContent() : config.getDraftContent();
117: } else {
118: if (config.getTitle() != null)
119: title = config.getTitle();
120: if (config.getContent() != null)
121: content = config.getContent();
122: }
123: response.setTitle(title);
124: }
125:
126: w.write(content);
127: w.flush();
128: }
129:
130: /**
131: * Helper method to serve up the <code>edit</code> mode.
132: * The default implementation throws an exception.
133: * @param request The portlet request
134: * @param response The render response
135: * @exception PortletException If the portlet cannot fulfilling the request
136: * @exception IOException If the streaming causes an I/O problem
137: */
138: protected void doConfig(RenderRequest request,
139: RenderResponse response) throws PortletException,
140: java.io.IOException {
141: Writer w = response.getWriter();
142:
143: String title = "Rich Text";
144: String content = "";
145: PortalInterface pi = ((RenderRequestImpl) request)
146: .getPortalInterface();
147: HTMLBlockConfig config = (HTMLBlockConfig) pi
148: .getPortletWindowConfig();
149: if (config != null) {
150: title = config.getDraftTitle() == null ? config.getTitle()
151: : config.getDraftTitle();
152: content = config.getDraftContent() == null ? config
153: .getContent() : config.getDraftContent();
154: response.setTitle(title);
155: }
156: String textAreaId = response.getNamespace() + "_textarea";
157: w.write("<form method=\"post\" action=\"");
158: w.write(response.createActionURL().toString());
159: w.write("\">");
160: w.write("<p><b>Title</b><br/>");
161: w.write("<input name=\"title\" value=\"");
162: w.write(title);
163: w.write("\" /></p><b>Content</b><br/>");
164: w.write("<textarea id=\"");
165: w.write(textAreaId);
166: w.write("\" name=\"content\">");
167: w.write(content);
168: w.write("</textarea>");
169: w.write("<p style=\"text-align: left;\">");
170: w
171: .write("<input class=\"button\" type=\"submit\" name=\"save_draft\" value=\"Save Draft\" onclick=\"tinyMCE.triggerSave();return true;\" />");
172: w
173: .write("<input class=\"button\" type=\"submit\" name=\"publish\" value=\"Publish\" onclick=\"tinyMCE.triggerSave();return true;\" />");
174: w
175: .write("<input class=\"button\" type=\"submit\" name=\"cancel\" value=\"Cancel\" />");
176:
177: w.write("</p>");
178: w.write("</center></form>");
179: w.write("<script type=\"text/javascript\">");
180: w
181: .write("sb_register_unload_callback(sb_remove_tiny_mce_control, '");
182: w.write(response.getNamespace());
183: w.write("');");
184: w.write("tinyMCE.execCommand('mceAddControl', true, '");
185: w.write(textAreaId);
186: w.write("');");
187: w.write("</script>");
188:
189: }
190:
191: /**
192: * Helper method to serve up the <code>help</code> mode.
193: * The default implementation throws an exception.
194: * @param request The portlet request
195: * @param response The render response
196: * @exception PortletException If the portlet cannot fulfilling the request
197: * @exception IOException If the streaming causes an I/O problem
198: */
199: protected void doHelp(RenderRequest request, RenderResponse response)
200: throws PortletException, java.io.IOException {
201: Writer w = response.getWriter();
202: w.write(request.getPreferences().getValue("help",
203: "Help is not available."));
204: w.flush();
205: }
206:
207: public void processAction(ActionRequest request,
208: ActionResponse response) throws PortletException,
209: IOException {
210:
211: String content = request.getParameter("content");
212: if (request.getParameter("cancel") != null) {
213: try {
214: response.setPortletMode(PortletMode.VIEW);
215: } catch (PortletModeException e) {
216: // Ignore. we are supporting view mode
217: e.printStackTrace();
218: }
219: return;
220: }
221: if (StringUtil.isNullOrEmpty(content))
222: return;
223:
224: PortalApplication portalApp = ((PortalApplicationView) request
225: .getAttribute(PortalConstants.CURRENT_PORTAL_APP_VIEW))
226: .getPortalApplication();
227: synchronized (portalApp) {
228: PortalInterface pi = ((ActionRequestImpl) request)
229: .getPortalInterface();
230: HTMLBlockConfig blockState = (HTMLBlockConfig) pi
231: .getPortletWindowConfig();
232: if (blockState == null) {
233: blockState = new HTMLBlockConfig();
234: pi.setPortletWindowConfig(blockState);
235: }
236: String title = request.getParameter("title");
237: if (StringUtil.isNullOrEmpty(title))
238: title = "Rich Text";
239: if (request.getParameter("publish") != null) {
240: blockState.setTitle(title);
241: blockState.setContent(content);
242: blockState.setDraftTitle(null);
243: blockState.setDraftContent(null);
244: try {
245: response.setPortletMode(PortletMode.VIEW);
246: } catch (PortletModeException e) {
247: // this will never be thrown
248: e.printStackTrace();
249: }
250: } else {
251: blockState.setDraftContent(content);
252: blockState.setDraftTitle(title);
253: try {
254: response.setPortletMode(new PortletMode("preview"));
255: } catch (PortletModeException e) {
256: // this will never be thrown
257: e.printStackTrace();
258: }
259:
260: }
261: try {
262: PortalInformationStoreLocator
263: .getPortalInformationStore()
264: .savePortalApplication(portalApp, true);
265: } catch (EntityLockedException e) {
266: throw new PortletException(
267: "You changes could not be persisted since the psite is locked by another user.");
268: } catch (WebServiceSecurityException e) {
269: throw new PortletSecurityException(e);
270: }
271: }
272: }
273:
274: }
|