KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.environment.SourceResolver;
28 import org.apache.cocoon.generation.ServiceableGenerator;
29 import org.apache.cocoon.portal.LinkService;
30 import org.apache.cocoon.portal.PortalService;
31 import org.apache.cocoon.portal.coplets.basket.events.CleanBriefcaseEvent;
32 import org.apache.cocoon.portal.coplets.basket.events.RefreshBasketEvent;
33 import org.apache.cocoon.portal.coplets.basket.events.RemoveItemEvent;
34 import org.apache.cocoon.portal.coplets.basket.events.ShowBasketEvent;
35 import org.apache.cocoon.portal.coplets.basket.events.ShowItemEvent;
36 import org.apache.cocoon.portal.event.Event;
37 import org.apache.cocoon.portal.profile.ProfileManager;
38 import org.apache.cocoon.xml.AttributesImpl;
39 import org.apache.cocoon.xml.XMLUtils;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * This is a portlet that displays the contents of a basket
44  *
45  * @version CVS $Id: BasketGenerator.java 125056 2005-01-13 10:33:37Z cziegeler $
46  */

47 public class BasketGenerator
48 extends ServiceableGenerator {
49     
50     /** This is the coplet ID that is used to display the content */
51     protected String JavaDoc showCopletId;
52     
53     /** This is the layout ID that is used to display the content */
54     protected String JavaDoc showLayoutId;
55
56     /** The type of items to display */
57     protected String JavaDoc type;
58     
59     /** The location of the type information */
60     protected String JavaDoc typeLocation;
61     
62     /** admin mode? */
63     protected boolean adminMode;
64     
65     /** The basket manager */
66     protected BasketManager basketManager;
67     
68     /* (non-Javadoc)
69      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
70      */

71     public void service(ServiceManager manager) throws ServiceException {
72         super.service(manager);
73         this.basketManager = (BasketManager)this.manager.lookup(BasketManager.ROLE);
74     }
75
76     /* (non-Javadoc)
77      * @see org.apache.avalon.framework.activity.Disposable#dispose()
78      */

79     public void dispose() {
80         if ( this.manager != null ) {
81             this.manager.release(this.basketManager);
82             this.basketManager = null;
83         }
84         super.dispose();
85     }
86
87     /* (non-Javadoc)
88      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
89      */

90     public void setup(SourceResolver resolver,
91                       Map JavaDoc objectModel,
92                       String JavaDoc src,
93                       Parameters par)
94     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
95         super.setup(resolver, objectModel, src, par);
96
97         this.showCopletId = par.getParameter("show-coplet", null);
98         this.showLayoutId = par.getParameter("show-layout", null);
99         this.adminMode = par.getParameterAsBoolean("admin-mode", false);
100         this.type = par.getParameter("type", null);
101         this.typeLocation = par.getParameter("type-location", null);
102     }
103
104     /* (non-Javadoc)
105      * @see org.apache.cocoon.generation.Generator#generate()
106      */

107     public void generate()
108     throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
109         this.xmlConsumer.startDocument();
110         if ( this.adminMode ) {
111             this.generateAdminMode();
112         } else {
113             PortalService service = null;
114             try {
115                 service = (PortalService)this.manager.lookup(PortalService.ROLE);
116
117                 final UserConfiguration uc = UserConfiguration.get(this.objectModel, service);
118                 Basket basket = null;
119                 Briefcase briefcase = null;
120                 Folder folder = null;
121
122                 if ( uc.isBasketEnabled() ) {
123                     basket = this.basketManager.getBasket();
124                 }
125                 if ( uc.isBriefcaseEnabled() ) {
126                     briefcase = this.basketManager.getBriefcase();
127                 }
128                 if ( uc.isFolderEnabled() ) {
129                     folder = this.basketManager.getFolder();
130                 }
131     
132                 final LinkService linkService = service.getComponentManager().getLinkService();
133                 
134                 XMLUtils.startElement(this.xmlConsumer, "basket-content");
135     
136                 this.toSAX(uc);
137                 
138                 final ProfileManager profileManager = service.getComponentManager().getProfileManager();
139                     
140                     XMLUtils.startElement(this.xmlConsumer, "items");
141                 
142                 int itemCount = 0;
143                 long itemSize = 0;
144
145                 StoreInfo info;
146                 
147                 info = this.toSAX(basket, linkService, profileManager);
148                 itemCount += info.count;
149                 itemSize += info.maxSize;
150                 info = this.toSAX(briefcase, linkService, profileManager);
151                 itemCount += info.count;
152                 itemSize += info.maxSize;
153                 info = this.toSAX(folder, linkService, profileManager);
154                 itemCount += info.count;
155                 itemSize += info.maxSize;
156
157                 XMLUtils.endElement(this.xmlConsumer, "items");
158
159                 XMLUtils.startElement(this.xmlConsumer, "item-count");
160                 XMLUtils.data(this.xmlConsumer, String.valueOf(itemCount));
161                 XMLUtils.endElement(this.xmlConsumer, "item-count");
162
163                 XMLUtils.startElement(this.xmlConsumer, "item-size");
164                 double f = itemSize / 10.24;
165                 f = Math.floor(f);
166                 if ( f < 10.0 && f > 0.1) {
167                     f = 10.0;
168                 } else if ( f < 0.1 ) {
169                     f = 0.0;
170                 }
171                 f = f / 100.0;
172                 XMLUtils.data(this.xmlConsumer, String.valueOf(f));
173                 XMLUtils.endElement(this.xmlConsumer, "item-size");
174
175                 XMLUtils.endElement(this.xmlConsumer, "basket-content");
176             } catch (ServiceException se) {
177                 throw new SAXException JavaDoc("Unable to lookup portal service.", se);
178             } finally {
179                 this.manager.release(service);
180             }
181         }
182         this.xmlConsumer.endDocument();
183     }
184
185     /**
186      * Render admin mode
187      */

188     protected void generateAdminMode()
189     throws SAXException JavaDoc {
190         List JavaDoc baskets = this.basketManager.getBriefcaseDescriptions();
191
192         PortalService service = null;
193         try {
194             service = (PortalService)this.manager.lookup(PortalService.ROLE);
195             LinkService linkService = service.getComponentManager().getLinkService();
196             XMLUtils.startElement(this.xmlConsumer, "basket-admin");
197             if ( baskets.size() > 0 ) {
198                 XMLUtils.startElement(this.xmlConsumer, "baskets");
199                 for(int i=0; i<baskets.size();i++) {
200                     ContentStoreDescription item = (ContentStoreDescription)baskets.get(i);
201                     XMLUtils.startElement(this.xmlConsumer, "basket");
202
203                     XMLUtils.startElement(this.xmlConsumer, "id");
204                     XMLUtils.data(this.xmlConsumer, item.id);
205                     XMLUtils.endElement(this.xmlConsumer, "id");
206                     
207                     XMLUtils.startElement(this.xmlConsumer, "size");
208                     XMLUtils.data(this.xmlConsumer, String.valueOf(item.size));
209                     XMLUtils.endElement(this.xmlConsumer, "size");
210                     
211                     Event event = new CleanBriefcaseEvent((Briefcase)null);
212                     XMLUtils.startElement(this.xmlConsumer, "remove-url");
213                     XMLUtils.data(this.xmlConsumer, linkService.getLinkURI(event));
214                     XMLUtils.endElement(this.xmlConsumer, "remove-url");
215                     
216                     event = new ShowBasketEvent(item.id);
217                     XMLUtils.startElement(this.xmlConsumer, "show-url");
218                     XMLUtils.data(this.xmlConsumer, linkService.getLinkURI(event));
219                     XMLUtils.endElement(this.xmlConsumer, "show-url");
220
221                     XMLUtils.endElement(this.xmlConsumer, "basket");
222                 }
223                 XMLUtils.endElement(this.xmlConsumer, "baskets");
224             }
225             Event e;
226             e = new RefreshBasketEvent();
227             XMLUtils.startElement(this.xmlConsumer, "refresh-url");
228             XMLUtils.data(this.xmlConsumer, linkService.getLinkURI(e));
229             XMLUtils.endElement(this.xmlConsumer, "refresh-url");
230             
231             e = new CleanBriefcaseEvent();
232             XMLUtils.startElement(this.xmlConsumer, "clean-url");
233             XMLUtils.data(this.xmlConsumer, linkService.getLinkURI(e));
234             XMLUtils.endElement(this.xmlConsumer, "clean-url");
235             
236             XMLUtils.endElement(this.xmlConsumer, "basket-admin");
237         } catch (ServiceException se) {
238             throw new SAXException JavaDoc("Unable to lookup portal service.", se);
239         } finally {
240             this.manager.release(service);
241         }
242     }
243     
244     protected StoreInfo toSAX(ContentStore store, LinkService linkService, ProfileManager profileManager)
245     throws SAXException JavaDoc {
246         StoreInfo info = new StoreInfo();
247         if ( store != null ) {
248             for(int i=0; i<store.size();i++) {
249                 Object JavaDoc item = store.getItem(i);
250                 if ( item instanceof ContentItem ) {
251                     ContentItem ci = (ContentItem)item;
252                     
253                     boolean process = true;
254                     if ( this.type != null && this.type.length() > 0 && this.typeLocation != null ) {
255                         Map JavaDoc attributes = (Map JavaDoc)ci.getAttribute("coplet-attributes");
256                         if ( attributes != null ) {
257                             if ( !this.type.equals(attributes.get(this.typeLocation)) ) {
258                                 process = false;
259                             }
260                         }
261                     }
262                     if ( process ) {
263                         info.count++;
264                         info.maxSize += ci.size();
265                         XMLUtils.startElement(this.xmlConsumer, "item");
266                         
267                         XMLUtils.createElement(this.xmlConsumer, "title", item.toString());
268         
269                         XMLUtils.startElement(this.xmlConsumer, "store");
270                         if ( store instanceof Briefcase ) {
271                             XMLUtils.data(this.xmlConsumer, "briefcase");
272                         } else if ( store instanceof Folder ) {
273                             XMLUtils.data(this.xmlConsumer, "folder");
274                         } else {
275                             XMLUtils.data(this.xmlConsumer, "basket");
276                         }
277                         XMLUtils.endElement(this.xmlConsumer, "store");
278                     
279                         XMLUtils.createElement(this.xmlConsumer, "id", String.valueOf(ci.getId()));
280                         Event e = new ShowItemEvent(store, item, profileManager.getPortalLayout(null, this.showLayoutId), this.showCopletId);
281                         XMLUtils.createElement(this.xmlConsumer, "show-url", linkService.getLinkURI(e));
282                         if (ci.size() != -1 ) {
283                             XMLUtils.createElement(this.xmlConsumer, "size", String.valueOf(ci.size()));
284                         }
285                         XMLUtils.startElement(this.xmlConsumer, "attributes");
286                         this.toSAX(ci.attributes);
287                         XMLUtils.endElement(this.xmlConsumer, "attributes");
288                         Event removeEvent = new RemoveItemEvent(store, item);
289                         XMLUtils.createElement(this.xmlConsumer, "remove-url", linkService.getLinkURI(removeEvent));
290                         
291                         XMLUtils.endElement(this.xmlConsumer, "item");
292                     }
293                 }
294             }
295         }
296         return info;
297     }
298     
299     protected void toSAX(Map JavaDoc attributes)
300     throws SAXException JavaDoc {
301         if ( attributes != null ) {
302             AttributesImpl a = new AttributesImpl();
303             final Iterator JavaDoc i = attributes.entrySet().iterator();
304             while ( i.hasNext() ) {
305                 final Map.Entry JavaDoc current = (Map.Entry JavaDoc)i.next();
306                 final String JavaDoc key = current.getKey().toString();
307                 if ( "coplet-attributes".equals(key) ) {
308                     this.toSAX((Map JavaDoc)current.getValue());
309                 } else {
310                     final Object JavaDoc value = current.getValue();
311                     final String JavaDoc valueText;
312                     if ( value != null ) {
313                         valueText = value.toString();
314                     } else {
315                         valueText ="";
316                     }
317                     a.addCDATAAttribute("name", key);
318                     a.addCDATAAttribute("value", valueText);
319                     XMLUtils.createElement(this.xmlConsumer, "attribute", a);
320                     a.clear();
321                 }
322             }
323         }
324     }
325     
326     protected void toSAX(UserConfiguration uc)
327     throws SAXException JavaDoc {
328         XMLUtils.startElement(this.xmlConsumer, "configuration");
329         AttributesImpl attr = new AttributesImpl();
330         
331         if ( uc.isBasketEnabled() ) {
332             XMLUtils.createElement(this.xmlConsumer, "basket", attr, "enabled");
333             attr.clear();
334         }
335         if ( uc.isBriefcaseEnabled() ) {
336             XMLUtils.createElement(this.xmlConsumer, "briefcase", "enabled");
337             attr.clear();
338         }
339         if ( uc.isFolderEnabled() ) {
340             XMLUtils.createElement(this.xmlConsumer, "folder", "enabled");
341             attr.clear();
342         }
343         
344         XMLUtils.endElement(this.xmlConsumer, "configuration");
345     }
346     
347     public static final class StoreInfo {
348         int count;
349         long maxSize;
350     }
351 }
352
Popular Tags