001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * for use in the design, construction, operation or maintenance of
035: * any nuclear facility.
036: */
037: package com.sun.portal.portlet.samples.invoice;
038:
039: import java.io.IOException;
040:
041: import javax.portlet.ActionRequest;
042: import javax.portlet.ActionResponse;
043: import javax.portlet.GenericPortlet;
044: import javax.portlet.PortletException;
045: import javax.portlet.PortletPreferences;
046: import javax.portlet.PortletRequestDispatcher;
047: import javax.portlet.PortletSession;
048: import javax.portlet.RenderRequest;
049: import javax.portlet.RenderResponse;
050:
051: public class InvoicePortlet extends GenericPortlet {
052:
053: private static final String viewPage = "/WEB-INF/jsp/view.jsp";
054: private static final String editPage = "/WEB-INF/jsp/edit.jsp";
055: private static final String session_invoice = "com.sun.portal.portlet.samples.invoice.invoicenumber";
056: private static final String invoice_pref = "invoice";
057:
058: protected void doView(RenderRequest request, RenderResponse response)
059: throws PortletException, IOException {
060:
061: if (getPortletContext().getResourceAsStream(viewPage) != null) {
062: try {
063: // dispatch view request to view.jsp
064: PortletRequestDispatcher dispatcher = getPortletContext()
065: .getRequestDispatcher(viewPage);
066: dispatcher.include(request, response);
067: } catch (IOException e) {
068: throw new PortletException(
069: "InvoicePortlet.doView exception", e);
070: }
071: } else {
072: throw new PortletException("view.jsp missing.");
073: }
074:
075: }
076:
077: protected void doEdit(RenderRequest request, RenderResponse response)
078: throws PortletException, IOException {
079:
080: if (getPortletContext().getResourceAsStream(editPage) != null) {
081: try {
082: // dispatch edit request to edit.jsp
083: PortletRequestDispatcher dispatcher = getPortletContext()
084: .getRequestDispatcher(editPage);
085: dispatcher.include(request, response);
086: } catch (IOException e) {
087: throw new PortletException(
088: "InvoicePortlet.doEdit exception", e);
089: }
090: } else {
091: throw new PortletException("edit.jsp missing.");
092: }
093:
094: }
095:
096: public void render(RenderRequest request, RenderResponse response)
097: throws PortletException, IOException {
098:
099: // set response content-type to value in request
100: response.setContentType(request.getResponseContentType());
101:
102: // if set, get invoice from preferences, else null
103: String prefInvoice = request.getPreferences().getValue(
104: invoice_pref, null);
105:
106: // if set, get invoice from session, else null
107: String sessionInvoice = (String) request.getPortletSession()
108: .getAttribute(session_invoice,
109: PortletSession.APPLICATION_SCOPE);
110:
111: if (sessionInvoice == null && prefInvoice != null) {
112: // if new session and pref is set, put pref in session
113: request.getPortletSession().setAttribute(session_invoice,
114: prefInvoice, PortletSession.APPLICATION_SCOPE);
115: }
116:
117: super .render(request, response);
118:
119: }
120:
121: public void processAction(ActionRequest request,
122: ActionResponse response) throws PortletException,
123: IOException {
124:
125: String invoice = request.getParameter(invoice_pref);
126: if (invoice != null) {
127: PortletPreferences prefs = request.getPreferences();
128: prefs.setValue(invoice_pref, invoice);
129: prefs.store();
130:
131: // set new preference in session
132: request.getPortletSession().setAttribute(session_invoice,
133: invoice, PortletSession.APPLICATION_SCOPE);
134:
135: }
136: }
137:
138: }
|