KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplets > basket > AddToBasketAspect


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.coplets.basket;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.parameters.ParameterException;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.cocoon.portal.PortalService;
24 import org.apache.cocoon.portal.coplet.CopletInstanceData;
25 import org.apache.cocoon.portal.coplets.basket.events.AddItemEvent;
26 import org.apache.cocoon.portal.event.Event;
27 import org.apache.cocoon.portal.layout.Layout;
28 import org.apache.cocoon.portal.layout.impl.CopletLayout;
29 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
30 import org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractAspect;
31 import org.apache.cocoon.xml.XMLUtils;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * This renderer adds a link to add this coplet to the basket.
37  * It checks the coplet data for the attributes
38  * basket-content and basket-link (boolean values) to stream
39  * out the elements.
40  *
41  * @version CVS $Id: AddToBasketAspect.java 125056 2005-01-13 10:33:37Z cziegeler $
42  */

43 public final class AddToBasketAspect
44 extends AbstractAspect
45 implements Disposable {
46
47     /** The basket manager */
48     protected BasketManager basketManager;
49     
50     
51     /* (non-Javadoc)
52      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
53      */

54     public void service(ServiceManager manager) throws ServiceException {
55         super.service(manager);
56         this.basketManager = (BasketManager)this.manager.lookup(BasketManager.ROLE);
57     }
58     
59     
60     /* (non-Javadoc)
61      * @see org.apache.avalon.framework.activity.Disposable#dispose()
62      */

63     public void dispose() {
64         if ( this.manager != null ) {
65             this.manager.release(this.basketManager);
66             this.basketManager = null;
67             this.manager = null;
68         }
69     }
70     
71     /* (non-Javadoc)
72      * @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)
73      */

74     public void toSAX(RendererAspectContext context,
75                         Layout layout,
76                         PortalService service,
77                         ContentHandler JavaDoc contenthandler)
78     throws SAXException JavaDoc {
79         final CopletInstanceData cid = ((CopletLayout)layout).getCopletInstanceData();
80         final ContentStore store;
81         final String JavaDoc elementName;
82         if ( context.getAspectConfiguration().equals(Boolean.TRUE) ) {
83             store = this.basketManager.getBasket();
84             elementName = "basket-add-content";
85         } else {
86             store = this.basketManager.getBriefcase();
87             elementName = "briefcase-add-content";
88         }
89         
90         Boolean JavaDoc b = (Boolean JavaDoc)cid.getCopletData().getAttribute("basket-content");
91         if ( b != null && b.equals(Boolean.TRUE) ) {
92             Object JavaDoc item = new ContentItem(cid, true);
93             Event event = new AddItemEvent(store, item);
94             XMLUtils.createElement(contenthandler, elementName, service.getComponentManager().getLinkService().getLinkURI(event));
95         }
96         b = (Boolean JavaDoc)cid.getCopletData().getAttribute("basket-link");
97         if ( b != null && b.equals(Boolean.TRUE) ) {
98             Object JavaDoc item = new ContentItem(cid, false);
99             Event event = new AddItemEvent(store, item);
100             XMLUtils.createElement(contenthandler, elementName, service.getComponentManager().getLinkService().getLinkURI(event));
101         }
102         
103         context.invokeNext( layout, service, contenthandler );
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
108      */

109     public Object JavaDoc prepareConfiguration(Parameters configuration)
110     throws ParameterException {
111         if ( configuration.getParameter("use-store", "basket").equalsIgnoreCase("basket") ) {
112             return Boolean.TRUE;
113         }
114         return Boolean.FALSE;
115     }
116 }
117
Popular Tags