001: /*
002: * (C) Copyright 2006 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: package com.nabhinc.portal.core;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.PrintStream;
027: import java.io.Serializable;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import javax.portlet.PortletMode;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpSession;
035:
036: /**
037: *
038: *
039: * @author Padmanabh Dabke
040: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class NavigationState implements Serializable {
043: private static final long serialVersionUID = -4210812290562144236L;
044: public transient PortletMode portletMode = PortletMode.VIEW;
045: public String portletModeStr = "view";
046:
047: public HashMap<String, String[]> paramMap = new HashMap<String, String[]>(
048: 10);
049:
050: public NavigationState copy() {
051: NavigationState copy = new NavigationState();
052: copy.portletMode = this .portletMode;
053: copy.paramMap.putAll(this .paramMap);
054: return copy;
055: }
056:
057: @SuppressWarnings("unchecked")
058: public void setParams(HttpServletRequest request) {
059: Map<String, String[]> paramMap = request.getParameterMap();
060: this .paramMap.clear();
061: this .paramMap.putAll(paramMap);
062: }
063:
064: public static NavigationState getNavigationState(
065: HttpSession session, String windowId, boolean isDetached) {
066: if (isDetached)
067: return (NavigationState) session
068: .getAttribute(PortalConstants.DETACHED_NAV_STATE_ATTRIBUTE
069: + windowId);
070: else
071: return (NavigationState) session
072: .getAttribute(PortalConstants.NAV_STATE_ATTRIBUTE_PREFIX
073: + windowId);
074: }
075:
076: public static void setNavigationState(HttpSession session,
077: String windowId, boolean isDetached, NavigationState ns) {
078: if (isDetached)
079: session.setAttribute(
080: PortalConstants.DETACHED_NAV_STATE_ATTRIBUTE
081: + windowId, ns);
082: else
083: session.setAttribute(
084: PortalConstants.NAV_STATE_ATTRIBUTE_PREFIX
085: + windowId, ns);
086: }
087:
088: public static void removeNavigationState(HttpSession session,
089: String windowId, boolean isDetached) {
090: if (isDetached)
091: session
092: .removeAttribute(PortalConstants.DETACHED_NAV_STATE_ATTRIBUTE
093: + windowId);
094: else
095: session
096: .removeAttribute(PortalConstants.NAV_STATE_ATTRIBUTE_PREFIX
097: + windowId);
098: }
099:
100: public void setPortletMode(String pMode) {
101: this .portletModeStr = pMode;
102: this .portletMode = new PortletMode(pMode);
103: }
104:
105: private void readObject(ObjectInputStream in) throws IOException,
106: ClassNotFoundException {
107: in.defaultReadObject();
108: this .portletMode = new PortletMode(this .portletModeStr);
109: }
110:
111: public void print(PrintStream w) {
112: w.println("Portlet Window Mode = " + this .portletModeStr);
113: w.println("Parameters");
114: Iterator<Map.Entry<String, String[]>> iter = this .paramMap
115: .entrySet().iterator();
116: while (iter.hasNext()) {
117: Map.Entry<String, String[]> entry = iter.next();
118: w.print(entry.getKey());
119: w.print("=");
120: String[] values = entry.getValue();
121: for (int i = 0; i < values.length; i++) {
122: w.print(values[i]);
123: w.print(" ");
124: }
125: }
126: }
127:
128: public static void main(String[] args) {
129: try {
130: NavigationState ns = new NavigationState();
131: ns.setPortletMode("edit");
132: ns.paramMap.put("param1",
133: new String[] { "value1", "value2" });
134: ns.print(System.out);
135: System.out.println("*******************");
136: ByteArrayOutputStream os = new ByteArrayOutputStream();
137: ObjectOutputStream oos = new ObjectOutputStream(os);
138: oos.writeObject(ns);
139: oos.flush();
140: oos.reset();
141: ByteArrayInputStream is = new ByteArrayInputStream(os
142: .toByteArray());
143: ObjectInputStream ois = new ObjectInputStream(is);
144: NavigationState ns2 = (NavigationState) ois.readObject();
145: ns2.print(System.out);
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: }
150: }
|