KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > components > docbasket > DocumentBasketHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend.components.docbasket;
17
18 import org.apache.cocoon.environment.Request;
19 import org.apache.cocoon.environment.Session;
20 import org.outerj.daisy.frontend.WikiHelper;
21 import org.outerj.daisy.publisher.Publisher;
22 import org.outerj.daisy.repository.Repository;
23 import org.outerj.daisy.repository.RepositoryException;
24 import org.outerx.daisy.x10Publisher.PublisherRequestDocument;
25 import org.outerx.daisy.x10Publisher.ResolveDocumentIdsDocument;
26 import org.outerx.daisy.x10Publisher.ResolveDocumentIdsDocument.ResolveDocumentIds;
27 import org.xml.sax.helpers.DefaultHandler JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 public class DocumentBasketHelper {
32     public static final String JavaDoc DOCBASKET_SESSION_ATTR = "DaisyDocumentBasket";
33
34     /**
35      *
36      * @param create create the basket in the session if it does not exist. When
37      * false, no session will be created if it did not yet exist.
38      */

39     public static DocumentBasket getDocumentBasket(Request request, boolean create) {
40         Session session = request.getSession(create);
41         if (session == null)
42             return null;
43
44         DocumentBasket documentBasket = (DocumentBasket)session.getAttribute(DOCBASKET_SESSION_ATTR);
45         if (documentBasket == null) {
46             if (!create)
47                 return null;
48
49             documentBasket = new DocumentBasket();
50             session.setAttribute(DOCBASKET_SESSION_ATTR, documentBasket);
51         }
52         return documentBasket;
53     }
54
55     public static void updateDocumentNames(DocumentBasketEntry entry, Request request, Repository repository)
56             throws SAXException JavaDoc, RepositoryException {
57         updateDocumentNames(new DocumentBasketEntry[] { entry }, request, repository);
58     }
59
60     public static void updateDocumentNames(DocumentBasketEntry[] entries, Request request, Repository repository)
61             throws RepositoryException, SAXException JavaDoc {
62
63         if (entries.length == 0)
64             return;
65
66         PublisherRequestDocument pubReqDoc = PublisherRequestDocument.Factory.newInstance();
67         PublisherRequestDocument.PublisherRequest pubReq = pubReqDoc.addNewPublisherRequest();
68         ResolveDocumentIdsDocument.ResolveDocumentIds resolveDocIds = pubReq.addNewResolveDocumentIds();
69
70         ResolveDocumentIds.Document documents[] = new ResolveDocumentIds.Document[entries.length];
71
72         for (int i = 0; i < entries.length; i++) {
73             DocumentBasketEntry entry = entries[i];
74             ResolveDocumentIds.Document doc = ResolveDocumentIds.Document.Factory.newInstance();
75             doc.setId(entry.getDocumentId());
76             doc.setBranch(entry.getBranch());
77             doc.setLanguage(entry.getLanguage());
78
79             String JavaDoc version;
80             long versionId = entry.getVersionId();
81             if (versionId == -3) {
82                 version = WikiHelper.getVersionMode(request).toString();
83             } else if (versionId == -2) {
84                 version = "last";
85             } else if (versionId == -1) {
86                 version = "live";
87             } else {
88                 version = String.valueOf(versionId);
89             }
90
91             doc.setVersion(version);
92             documents[i] = doc;
93         }
94
95         resolveDocIds.setDocumentArray(documents);
96
97
98         Publisher publisher = (Publisher)repository.getExtension("Publisher");
99         publisher.processRequest(pubReqDoc, new DocNamesReceiver(entries));
100     }
101
102     static class DocNamesReceiver extends DefaultHandler JavaDoc {
103         private DocumentBasketEntry[] entries;
104         private int pos = 0;
105
106         public DocNamesReceiver(DocumentBasketEntry[] entries) {
107             this.entries = entries;
108         }
109
110         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
111             if (uri.equals("http://outerx.org/daisy/1.0#publisher") && localName.equals("document")) {
112                 entries[pos++].setDocumentName(attributes.getValue("name"));
113             }
114         }
115     }
116
117 }
118
Popular Tags