KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > frontend > ManageBooksApple


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.books.frontend;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.WikiHelper;
20 import org.outerj.daisy.frontend.PageContext;
21 import org.outerj.daisy.frontend.SkinConfHelper;
22 import org.outerj.daisy.repository.Repository;
23 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
24 import org.apache.cocoon.components.flow.apples.AppleRequest;
25 import org.apache.cocoon.components.flow.apples.AppleResponse;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.xml.AttributesImpl;
28 import org.apache.cocoon.xml.SaxBuffer;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.outerx.daisy.x10.SearchResultDocument;
33 import org.xml.sax.ContentHandler JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36 import java.util.*;
37
38 public class ManageBooksApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
39     private ServiceManager serviceManager;
40
41     public void service(ServiceManager serviceManager) throws ServiceException {
42         this.serviceManager = serviceManager;
43     }
44
45     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
46         Request request = appleRequest.getCocoonRequest();
47         Repository repository = WikiHelper.getRepository(request, serviceManager);
48         Locale locale = WikiHelper.getLocale(appleRequest.getCocoonRequest());
49         SearchResultDocument searchResultDocument = repository.getQueryManager().performQuery("select name, branch, language, $BookPath where documentType = 'BookDefinition' order by name", locale);
50
51         BookGroup rootGroup = buildHierarchicalBookIndex(searchResultDocument.getSearchResult());
52         SaxBuffer hierarchicalBookIndex = new SaxBuffer();
53         rootGroup.generateSaxFragment(hierarchicalBookIndex);
54
55         String JavaDoc mountPoint = WikiHelper.getMountPoint(request);
56         PageContext pageContext = new PageContext(mountPoint, repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext());
57         Map viewData = new HashMap();
58         viewData.put("pageContext", pageContext);
59         viewData.put("hierarchicalBookIndex", hierarchicalBookIndex);
60
61         appleResponse.sendPage("BookManagementPipe", viewData);
62     }
63
64     private BookGroup buildHierarchicalBookIndex(SearchResultDocument.SearchResult searchResult) {
65         BookGroup rootGroup = new BookGroup("root");
66         SearchResultDocument.SearchResult.Rows.Row[] rows = searchResult.getRows().getRowArray();
67         for (int i = 0; i < rows.length; i++) {
68             String JavaDoc[] values = rows[i].getValueArray();
69             BookNode bookNode = new BookNode(values[0], values[1], values[2], rows[i].getDocumentId(), rows[i].getBranchId(), rows[i].getLanguageId());
70             String JavaDoc path = values[3];
71             BookGroup group = rootGroup.getGroup(path);
72             group.addChild(bookNode);
73         }
74         return rootGroup;
75     }
76
77     static class BookNode implements BookGroup.BookGroupChild {
78         private final String JavaDoc name;
79         private final String JavaDoc branch;
80         private final String JavaDoc language;
81         private final long documentId;
82         private final long branchId;
83         private final long languageId;
84
85         public BookNode(String JavaDoc name, String JavaDoc branch, String JavaDoc language, long documentId, long branchId, long languageId) {
86             this.name = name;
87             this.branch = branch;
88             this.language = language;
89             this.documentId = documentId;
90             this.branchId = branchId;
91             this.languageId = languageId;
92         }
93
94         public int compareTo(Object JavaDoc o) {
95             BookNode otherBookNode = (BookNode)o;
96             return name.compareTo(otherBookNode.name);
97         }
98
99         public void generateSaxFragment(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
100             AttributesImpl bookAttrs = new AttributesImpl();
101             bookAttrs.addCDATAAttribute("name", name);
102             bookAttrs.addCDATAAttribute("branch", branch);
103             bookAttrs.addCDATAAttribute("language", language);
104             bookAttrs.addCDATAAttribute("documentId", String.valueOf(documentId));
105             bookAttrs.addCDATAAttribute("branchId", String.valueOf(branchId));
106             bookAttrs.addCDATAAttribute("languageId", String.valueOf(languageId));
107             contentHandler.startElement("", "book", "book", bookAttrs);
108             contentHandler.endElement("", "book", "book");
109         }
110     }
111 }
112
Popular Tags