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.coplets.basket;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.parameters.ParameterException;
021: import org.apache.avalon.framework.parameters.Parameters;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.cocoon.portal.PortalService;
025: import org.apache.cocoon.portal.coplet.CopletInstanceData;
026: import org.apache.cocoon.portal.coplets.basket.events.AddItemEvent;
027: import org.apache.cocoon.portal.event.Event;
028: import org.apache.cocoon.portal.layout.Layout;
029: import org.apache.cocoon.portal.layout.impl.CopletLayout;
030: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
031: import org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractAspect;
032: import org.apache.cocoon.xml.XMLUtils;
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * This renderer adds a link to add this coplet to the basket.
038: * It checks the coplet data for the attributes
039: * basket-content and basket-link (boolean values) to stream
040: * out the elements.
041: *
042: * @version CVS $Id: AddToBasketAspect.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public final class AddToBasketAspect extends AbstractAspect implements
045: Disposable {
046:
047: /** The basket manager */
048: protected BasketManager basketManager;
049:
050: /* (non-Javadoc)
051: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
052: */
053: public void service(ServiceManager manager) throws ServiceException {
054: super .service(manager);
055: this .basketManager = (BasketManager) this .manager
056: .lookup(BasketManager.ROLE);
057: }
058:
059: /* (non-Javadoc)
060: * @see org.apache.avalon.framework.activity.Disposable#dispose()
061: */
062: public void dispose() {
063: if (this .manager != null) {
064: this .manager.release(this .basketManager);
065: this .basketManager = null;
066: this .manager = null;
067: }
068: }
069:
070: /* (non-Javadoc)
071: * @see org.apache.cocoon.portal.layout.renderer.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
072: */
073: public void toSAX(RendererAspectContext context, Layout layout,
074: PortalService service, ContentHandler contenthandler)
075: throws SAXException {
076: final CopletInstanceData cid = ((CopletLayout) layout)
077: .getCopletInstanceData();
078: final ContentStore store;
079: final String elementName;
080: if (context.getAspectConfiguration().equals(Boolean.TRUE)) {
081: store = this .basketManager.getBasket();
082: elementName = "basket-add-content";
083: } else {
084: store = this .basketManager.getBriefcase();
085: elementName = "briefcase-add-content";
086: }
087:
088: Boolean b = (Boolean) cid.getCopletData().getAttribute(
089: "basket-content");
090: if (b != null && b.equals(Boolean.TRUE)) {
091: Object item = new ContentItem(cid, true);
092: Event event = new AddItemEvent(store, item);
093: XMLUtils.createElement(contenthandler, elementName, service
094: .getComponentManager().getLinkService().getLinkURI(
095: event));
096: }
097: b = (Boolean) cid.getCopletData().getAttribute("basket-link");
098: if (b != null && b.equals(Boolean.TRUE)) {
099: Object item = new ContentItem(cid, false);
100: Event event = new AddItemEvent(store, item);
101: XMLUtils.createElement(contenthandler, elementName, service
102: .getComponentManager().getLinkService().getLinkURI(
103: event));
104: }
105:
106: context.invokeNext(layout, service, contenthandler);
107: }
108:
109: /* (non-Javadoc)
110: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
111: */
112: public Object prepareConfiguration(Parameters configuration)
113: throws ParameterException {
114: if (configuration.getParameter("use-store", "basket")
115: .equalsIgnoreCase("basket")) {
116: return Boolean.TRUE;
117: }
118: return Boolean.FALSE;
119: }
120: }
|