001: /*
002: * (C) Copyright 2000 - 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.model;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.StringReader;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029:
030: import javax.xml.bind.JAXBContext;
031: import javax.xml.bind.Marshaller;
032: import javax.xml.bind.Unmarshaller;
033: import javax.xml.bind.annotation.XmlAttribute;
034: import javax.xml.bind.annotation.XmlElement;
035: import javax.xml.bind.annotation.XmlElements;
036: import javax.xml.bind.annotation.XmlRootElement;
037: import javax.xml.bind.annotation.XmlTransient;
038:
039: /**
040: *
041: *
042: * @author Padmanabh Dabke
043: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
044: */
045: @XmlRootElement(name="renderable-map")
046: public class RenderableMap extends BaseCompositeRenderable {
047:
048: @XmlRootElement(name="renderable")
049: public static class ComponentEntry {
050:
051: @XmlAttribute(name="name")
052: public String name = null;
053:
054: @XmlElements({@XmlElement(name="renderable-map",type=RenderableMap.class),@XmlElement(name="portlet-window",type=PortletWindow.class),@XmlElement(name="renderable-list",type=RenderableList.class),@XmlElement(name="menu",type=MenuLayout.class),@XmlElement(name="tree",type=TreeLayout.class),@XmlElement(name="tabbed-panel",type=TabLayout.class),@XmlElement(name="horizontal-menu",type=HorizontalMenuLayout.class),@XmlElement(name="solo",type=SoloLayout.class),@XmlElement(name="cascading-menu",type=CascadingMenuLayout.class)})
055: public Renderable renderable;
056:
057: public ComponentEntry() {
058: }
059:
060: public ComponentEntry(String name, Renderable r) {
061: this .name = name;
062: this .renderable = r;
063: }
064: }
065:
066: @XmlTransient
067: HashMap<String, Renderable> componentMap = new HashMap<String, Renderable>();
068:
069: @XmlElement(name="renderable")
070: private List<ComponentEntry> componentList = new ArrayList<ComponentEntry>();
071:
072: public RenderableMap() {
073: }
074:
075: public RenderableMap(PortalPage p) {
076: super (p);
077: }
078:
079: public Renderable getRenderable(String name) {
080: return componentMap.get(name);
081: }
082:
083: public void afterUnmarshal(Unmarshaller um, Object parent) {
084: if (this .componentList == null) {
085: this .componentList = new ArrayList<ComponentEntry>();
086: } else {
087: for (int i = 0; i < this .componentList.size(); i++) {
088: ComponentEntry element = this .componentList.get(i);
089: this .componentMap.put(element.name, element.renderable);
090: if (element.renderable.getTemplate() == null)
091: element.renderable.setTemplate(element.name
092: + "_portlet.jsp");
093: }
094: }
095: }
096:
097: public static void main(String[] args) {
098: try {
099:
100: RenderableMap r = new RenderableMap();
101: JAXBContext jc = JAXBContext
102: .newInstance("com.nabhinc.portal.model:com.nabhinc.condition");
103:
104: Marshaller m = jc.createMarshaller();
105: m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
106: ByteArrayOutputStream sos = new ByteArrayOutputStream();
107: m.marshal(r, sos);
108: String xml = sos.toString();
109: System.out.println(xml);
110: Unmarshaller um = jc.createUnmarshaller();
111: RenderableMap c = (RenderableMap) um
112: .unmarshal(new StringReader(xml));
113: ByteArrayOutputStream sos2 = new ByteArrayOutputStream();
114: m.marshal(c, sos2);
115: System.out.println(sos2.toString());
116: } catch (Exception e) {
117: e.printStackTrace();
118: }
119:
120: }
121:
122: public Iterator<Renderable> getComponents() {
123: return this .componentMap.values().iterator();
124: }
125:
126: public boolean removePortletWindow(PortletWindow pWindow) {
127: Iterator<Map.Entry<String, Renderable>> iter = this .componentMap
128: .entrySet().iterator();
129: while (iter.hasNext()) {
130: Map.Entry<String, Renderable> e = iter.next();
131: if (e.getValue() == pWindow) {
132: this .componentMap.remove(e.getKey());
133: return true;
134: } else {
135: if (e.getValue().removePortletWindow(pWindow))
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: public void addContent(Renderable r, String name) {
143: componentMap.put(name, r);
144: componentList.add(new ComponentEntry(name, r));
145: if (r.getTemplate() == null)
146: r.setTemplate(name + "_portlet.jsp");
147:
148: }
149:
150: public void addPortletWindow(PortletWindow pWindow) {
151: Collection<Renderable> members = this .componentMap.values();
152: if (members.size() == 0) {
153: // This case is really problematic. There is no way to figure out
154: // what the layout file is. so we can't figure out the right
155: // window template. For desktop mode, it doesn't matter, but if
156: // the user switches to the normal mode, we have a problem. For
157: // now we assume that this renderable is using two_columns.jsp
158: // and col1 uses template wide_portlet.jsp for rendering.
159: RenderableList rList = new RenderableList();
160: rList.setTemplate("wide_portlet.jsp");
161: this .setTemplate("two_columns.jsp");
162: addContent(rList, "col1");
163: rList.addContent(pWindow, null);
164: } else {
165: // Add it to the first member
166: CompositeRenderable firstR = (CompositeRenderable) members
167: .iterator().next();
168: firstR.addPortletWindow(pWindow);
169: }
170:
171: }
172:
173: }
|