1 16 package org.apache.cocoon.portal.coplets.basket; 17 18 import java.io.IOException ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 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 ; 41 42 47 public class BasketGenerator 48 extends ServiceableGenerator { 49 50 51 protected String showCopletId; 52 53 54 protected String showLayoutId; 55 56 57 protected String type; 58 59 60 protected String typeLocation; 61 62 63 protected boolean adminMode; 64 65 66 protected BasketManager basketManager; 67 68 71 public void service(ServiceManager manager) throws ServiceException { 72 super.service(manager); 73 this.basketManager = (BasketManager)this.manager.lookup(BasketManager.ROLE); 74 } 75 76 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 90 public void setup(SourceResolver resolver, 91 Map objectModel, 92 String src, 93 Parameters par) 94 throws ProcessingException, SAXException , IOException { 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 107 public void generate() 108 throws IOException , SAXException , 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 ("Unable to lookup portal service.", se); 178 } finally { 179 this.manager.release(service); 180 } 181 } 182 this.xmlConsumer.endDocument(); 183 } 184 185 188 protected void generateAdminMode() 189 throws SAXException { 190 List 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 ("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 { 246 StoreInfo info = new StoreInfo(); 247 if ( store != null ) { 248 for(int i=0; i<store.size();i++) { 249 Object 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 attributes = (Map )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 attributes) 300 throws SAXException { 301 if ( attributes != null ) { 302 AttributesImpl a = new AttributesImpl(); 303 final Iterator i = attributes.entrySet().iterator(); 304 while ( i.hasNext() ) { 305 final Map.Entry current = (Map.Entry )i.next(); 306 final String key = current.getKey().toString(); 307 if ( "coplet-attributes".equals(key) ) { 308 this.toSAX((Map )current.getValue()); 309 } else { 310 final Object value = current.getValue(); 311 final String 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 { 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 |