001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.layout.renderer.aspect.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.cocoon.environment.ObjectModelHelper;
025: import org.apache.cocoon.environment.Request;
026: import org.apache.cocoon.environment.Session;
027: import org.apache.cocoon.portal.PortalService;
028: import org.apache.cocoon.portal.acting.helpers.CopletEventDescription;
029: import org.apache.cocoon.portal.acting.helpers.LayoutEventDescription;
030: import org.apache.cocoon.portal.coplet.CopletInstanceData;
031: import org.apache.cocoon.portal.layout.Layout;
032: import org.apache.cocoon.portal.layout.impl.CopletLayout;
033: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
034: import org.xml.sax.ContentHandler;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * Save the current state of the layout into the session. Takes into account
039: * state already present as request attribute. Includes aspect data and parameters
040: * as well as aspect data and attributes of a coplet instance if layout is a
041: * coplet layout. This aspect does not add to the XML created by the renderer chain.
042: *
043: * <h2>Example XML</h2>
044: * <pre>
045: * <!-- output from following renderers -->
046: * </pre>
047: *
048: * <h2>Applicable to:</h2>
049: * <ul>
050: * <li>{@link org.apache.cocoon.portal.layout.Layout}</li>
051: * </ul>
052: *
053: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
054: *
055: * @version CVS $Id: HistoryAspect.java 433543 2006-08-22 06:22:54Z crossley $
056: */
057: public class HistoryAspect extends AbstractAspect {
058:
059: /**
060: * Add the values to the state
061: * @param id
062: * @param state
063: * @param values
064: * @param isCopletEvent tells if the event has to be a coplet event
065: */
066: protected void addValues(String id, List state, Map values,
067: String prefix, boolean isCopletEvent) {
068: final Iterator iter = values.entrySet().iterator();
069: while (iter.hasNext()) {
070: final Map.Entry entry = (Map.Entry) iter.next();
071: final String path = prefix + entry.getKey();
072: if (!isCopletEvent) {
073: LayoutEventDescription led = new LayoutEventDescription();
074: led.path = path;
075: led.layoutId = id;
076: led.data = entry.getValue();
077: state.add(led);
078: } else {
079: CopletEventDescription ced = new CopletEventDescription();
080: ced.path = path;
081: ced.copletId = id;
082: ced.data = entry.getValue();
083: state.add(ced);
084: }
085: }
086: }
087:
088: /* (non-Javadoc)
089: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
090: */
091: public void toSAX(RendererAspectContext context, Layout layout,
092: PortalService service, ContentHandler handler)
093: throws SAXException {
094: if (layout.getId() != null) {
095: final Request request = ObjectModelHelper
096: .getRequest(context.getObjectModel());
097: final Session session = request.getSession(false);
098: if (session != null) {
099: List history = (List) session
100: .getAttribute("portal-history");
101: if (history == null) {
102:
103: history = new ArrayList();
104: }
105: List state = (List) request
106: .getAttribute("portal-history");
107: if (state == null) {
108: state = new ArrayList();
109: request.setAttribute("portal-history", state);
110: history.add(state);
111: }
112:
113: this .addValues(layout.getId(), state, layout
114: .getAspectDatas(), "aspectDatas/", false);
115: this .addValues(layout.getId(), state, layout
116: .getParameters(), "parameters/", false);
117:
118: // are we a coplet layout
119: if (layout instanceof CopletLayout) {
120: CopletInstanceData cid = ((CopletLayout) layout)
121: .getCopletInstanceData();
122: this .addValues(cid.getId(), state, cid
123: .getAspectDatas(), "aspectDatas/", true);
124: this .addValues(cid.getId(), state, cid
125: .getAttributes(), "attributes/", true);
126: }
127: session.setAttribute("portal-history", history);
128: }
129: }
130: context.invokeNext(layout, service, handler);
131: }
132:
133: }
|