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 java.io.IOException;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.environment.SourceResolver;
029: import org.apache.cocoon.generation.ServiceableGenerator;
030: import org.apache.cocoon.portal.LinkService;
031: import org.apache.cocoon.portal.PortalService;
032: import org.apache.cocoon.portal.coplets.basket.events.CleanBriefcaseEvent;
033: import org.apache.cocoon.portal.coplets.basket.events.RefreshBasketEvent;
034: import org.apache.cocoon.portal.coplets.basket.events.RemoveItemEvent;
035: import org.apache.cocoon.portal.coplets.basket.events.ShowBasketEvent;
036: import org.apache.cocoon.portal.coplets.basket.events.ShowItemEvent;
037: import org.apache.cocoon.portal.event.Event;
038: import org.apache.cocoon.portal.profile.ProfileManager;
039: import org.apache.cocoon.xml.AttributesImpl;
040: import org.apache.cocoon.xml.XMLUtils;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * This is a portlet that displays the contents of a basket
045: *
046: * @version CVS $Id: BasketGenerator.java 433543 2006-08-22 06:22:54Z crossley $
047: */
048: public class BasketGenerator extends ServiceableGenerator {
049:
050: /** This is the coplet ID that is used to display the content */
051: protected String showCopletId;
052:
053: /** This is the layout ID that is used to display the content */
054: protected String showLayoutId;
055:
056: /** The type of items to display */
057: protected String type;
058:
059: /** The location of the type information */
060: protected String typeLocation;
061:
062: /** admin mode? */
063: protected boolean adminMode;
064:
065: /** The basket manager */
066: protected BasketManager basketManager;
067:
068: /* (non-Javadoc)
069: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
070: */
071: public void service(ServiceManager manager) throws ServiceException {
072: super .service(manager);
073: this .basketManager = (BasketManager) this .manager
074: .lookup(BasketManager.ROLE);
075: }
076:
077: /* (non-Javadoc)
078: * @see org.apache.avalon.framework.activity.Disposable#dispose()
079: */
080: public void dispose() {
081: if (this .manager != null) {
082: this .manager.release(this .basketManager);
083: this .basketManager = null;
084: }
085: super .dispose();
086: }
087:
088: /* (non-Javadoc)
089: * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
090: */
091: public void setup(SourceResolver resolver, Map objectModel,
092: String src, Parameters par) throws ProcessingException,
093: SAXException, IOException {
094: super .setup(resolver, objectModel, src, par);
095:
096: this .showCopletId = par.getParameter("show-coplet", null);
097: this .showLayoutId = par.getParameter("show-layout", null);
098: this .adminMode = par.getParameterAsBoolean("admin-mode", false);
099: this .type = par.getParameter("type", null);
100: this .typeLocation = par.getParameter("type-location", null);
101: }
102:
103: /* (non-Javadoc)
104: * @see org.apache.cocoon.generation.Generator#generate()
105: */
106: public void generate() throws IOException, SAXException,
107: ProcessingException {
108: this .xmlConsumer.startDocument();
109: if (this .adminMode) {
110: this .generateAdminMode();
111: } else {
112: PortalService service = null;
113: try {
114: service = (PortalService) this .manager
115: .lookup(PortalService.ROLE);
116:
117: final UserConfiguration uc = UserConfiguration.get(
118: this .objectModel, service);
119: Basket basket = null;
120: Briefcase briefcase = null;
121: Folder folder = null;
122:
123: if (uc.isBasketEnabled()) {
124: basket = this .basketManager.getBasket();
125: }
126: if (uc.isBriefcaseEnabled()) {
127: briefcase = this .basketManager.getBriefcase();
128: }
129: if (uc.isFolderEnabled()) {
130: folder = this .basketManager.getFolder();
131: }
132:
133: final LinkService linkService = service
134: .getComponentManager().getLinkService();
135:
136: XMLUtils.startElement(this .xmlConsumer,
137: "basket-content");
138:
139: this .toSAX(uc);
140:
141: final ProfileManager profileManager = service
142: .getComponentManager().getProfileManager();
143:
144: XMLUtils.startElement(this .xmlConsumer, "items");
145:
146: int itemCount = 0;
147: long itemSize = 0;
148:
149: StoreInfo info;
150:
151: info = this .toSAX(basket, linkService, profileManager);
152: itemCount += info.count;
153: itemSize += info.maxSize;
154: info = this .toSAX(briefcase, linkService,
155: profileManager);
156: itemCount += info.count;
157: itemSize += info.maxSize;
158: info = this .toSAX(folder, linkService, profileManager);
159: itemCount += info.count;
160: itemSize += info.maxSize;
161:
162: XMLUtils.endElement(this .xmlConsumer, "items");
163:
164: XMLUtils.startElement(this .xmlConsumer, "item-count");
165: XMLUtils.data(this .xmlConsumer, String
166: .valueOf(itemCount));
167: XMLUtils.endElement(this .xmlConsumer, "item-count");
168:
169: XMLUtils.startElement(this .xmlConsumer, "item-size");
170: double f = itemSize / 10.24;
171: f = Math.floor(f);
172: if (f < 10.0 && f > 0.1) {
173: f = 10.0;
174: } else if (f < 0.1) {
175: f = 0.0;
176: }
177: f = f / 100.0;
178: XMLUtils.data(this .xmlConsumer, String.valueOf(f));
179: XMLUtils.endElement(this .xmlConsumer, "item-size");
180:
181: XMLUtils.endElement(this .xmlConsumer, "basket-content");
182: } catch (ServiceException se) {
183: throw new SAXException(
184: "Unable to lookup portal service.", se);
185: } finally {
186: this .manager.release(service);
187: }
188: }
189: this .xmlConsumer.endDocument();
190: }
191:
192: /**
193: * Render admin mode
194: */
195: protected void generateAdminMode() throws SAXException {
196: List baskets = this .basketManager.getBriefcaseDescriptions();
197:
198: PortalService service = null;
199: try {
200: service = (PortalService) this .manager
201: .lookup(PortalService.ROLE);
202: LinkService linkService = service.getComponentManager()
203: .getLinkService();
204: XMLUtils.startElement(this .xmlConsumer, "basket-admin");
205: if (baskets.size() > 0) {
206: XMLUtils.startElement(this .xmlConsumer, "baskets");
207: for (int i = 0; i < baskets.size(); i++) {
208: ContentStoreDescription item = (ContentStoreDescription) baskets
209: .get(i);
210: XMLUtils.startElement(this .xmlConsumer, "basket");
211:
212: XMLUtils.startElement(this .xmlConsumer, "id");
213: XMLUtils.data(this .xmlConsumer, item.id);
214: XMLUtils.endElement(this .xmlConsumer, "id");
215:
216: XMLUtils.startElement(this .xmlConsumer, "size");
217: XMLUtils.data(this .xmlConsumer, String
218: .valueOf(item.size));
219: XMLUtils.endElement(this .xmlConsumer, "size");
220:
221: Event event = new CleanBriefcaseEvent(
222: (Briefcase) null);
223: XMLUtils.startElement(this .xmlConsumer,
224: "remove-url");
225: XMLUtils.data(this .xmlConsumer, linkService
226: .getLinkURI(event));
227: XMLUtils.endElement(this .xmlConsumer, "remove-url");
228:
229: event = new ShowBasketEvent(item.id);
230: XMLUtils.startElement(this .xmlConsumer, "show-url");
231: XMLUtils.data(this .xmlConsumer, linkService
232: .getLinkURI(event));
233: XMLUtils.endElement(this .xmlConsumer, "show-url");
234:
235: XMLUtils.endElement(this .xmlConsumer, "basket");
236: }
237: XMLUtils.endElement(this .xmlConsumer, "baskets");
238: }
239: Event e;
240: e = new RefreshBasketEvent();
241: XMLUtils.startElement(this .xmlConsumer, "refresh-url");
242: XMLUtils.data(this .xmlConsumer, linkService.getLinkURI(e));
243: XMLUtils.endElement(this .xmlConsumer, "refresh-url");
244:
245: e = new CleanBriefcaseEvent();
246: XMLUtils.startElement(this .xmlConsumer, "clean-url");
247: XMLUtils.data(this .xmlConsumer, linkService.getLinkURI(e));
248: XMLUtils.endElement(this .xmlConsumer, "clean-url");
249:
250: XMLUtils.endElement(this .xmlConsumer, "basket-admin");
251: } catch (ServiceException se) {
252: throw new SAXException("Unable to lookup portal service.",
253: se);
254: } finally {
255: this .manager.release(service);
256: }
257: }
258:
259: protected StoreInfo toSAX(ContentStore store,
260: LinkService linkService, ProfileManager profileManager)
261: throws SAXException {
262: StoreInfo info = new StoreInfo();
263: if (store != null) {
264: for (int i = 0; i < store.size(); i++) {
265: Object item = store.getItem(i);
266: if (item instanceof ContentItem) {
267: ContentItem ci = (ContentItem) item;
268:
269: boolean process = true;
270: if (this .type != null && this .type.length() > 0
271: && this .typeLocation != null) {
272: Map attributes = (Map) ci
273: .getAttribute("coplet-attributes");
274: if (attributes != null) {
275: if (!this .type.equals(attributes
276: .get(this .typeLocation))) {
277: process = false;
278: }
279: }
280: }
281: if (process) {
282: info.count++;
283: info.maxSize += ci.size();
284: XMLUtils.startElement(this .xmlConsumer, "item");
285:
286: XMLUtils.createElement(this .xmlConsumer,
287: "title", item.toString());
288:
289: XMLUtils
290: .startElement(this .xmlConsumer, "store");
291: if (store instanceof Briefcase) {
292: XMLUtils
293: .data(this .xmlConsumer, "briefcase");
294: } else if (store instanceof Folder) {
295: XMLUtils.data(this .xmlConsumer, "folder");
296: } else {
297: XMLUtils.data(this .xmlConsumer, "basket");
298: }
299: XMLUtils.endElement(this .xmlConsumer, "store");
300:
301: XMLUtils.createElement(this .xmlConsumer, "id",
302: String.valueOf(ci.getId()));
303: Event e = new ShowItemEvent(store, item,
304: profileManager.getPortalLayout(null,
305: this .showLayoutId),
306: this .showCopletId);
307: XMLUtils.createElement(this .xmlConsumer,
308: "show-url", linkService.getLinkURI(e));
309: if (ci.size() != -1) {
310: XMLUtils.createElement(this .xmlConsumer,
311: "size", String.valueOf(ci.size()));
312: }
313: XMLUtils.startElement(this .xmlConsumer,
314: "attributes");
315: this .toSAX(ci.attributes);
316: XMLUtils.endElement(this .xmlConsumer,
317: "attributes");
318: Event removeEvent = new RemoveItemEvent(store,
319: item);
320: XMLUtils.createElement(this .xmlConsumer,
321: "remove-url", linkService
322: .getLinkURI(removeEvent));
323:
324: XMLUtils.endElement(this .xmlConsumer, "item");
325: }
326: }
327: }
328: }
329: return info;
330: }
331:
332: protected void toSAX(Map attributes) throws SAXException {
333: if (attributes != null) {
334: AttributesImpl a = new AttributesImpl();
335: final Iterator i = attributes.entrySet().iterator();
336: while (i.hasNext()) {
337: final Map.Entry current = (Map.Entry) i.next();
338: final String key = current.getKey().toString();
339: if ("coplet-attributes".equals(key)) {
340: this .toSAX((Map) current.getValue());
341: } else {
342: final Object value = current.getValue();
343: final String valueText;
344: if (value != null) {
345: valueText = value.toString();
346: } else {
347: valueText = "";
348: }
349: a.addCDATAAttribute("name", key);
350: a.addCDATAAttribute("value", valueText);
351: XMLUtils.createElement(this .xmlConsumer,
352: "attribute", a);
353: a.clear();
354: }
355: }
356: }
357: }
358:
359: protected void toSAX(UserConfiguration uc) throws SAXException {
360: XMLUtils.startElement(this .xmlConsumer, "configuration");
361: AttributesImpl attr = new AttributesImpl();
362:
363: if (uc.isBasketEnabled()) {
364: XMLUtils.createElement(this .xmlConsumer, "basket", attr,
365: "enabled");
366: attr.clear();
367: }
368: if (uc.isBriefcaseEnabled()) {
369: XMLUtils.createElement(this .xmlConsumer, "briefcase",
370: "enabled");
371: attr.clear();
372: }
373: if (uc.isFolderEnabled()) {
374: XMLUtils.createElement(this .xmlConsumer, "folder",
375: "enabled");
376: attr.clear();
377: }
378:
379: XMLUtils.endElement(this .xmlConsumer, "configuration");
380: }
381:
382: public static final class StoreInfo {
383: int count;
384: long maxSize;
385: }
386: }
|