1 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 ; 34 import org.xml.sax.SAXException ; 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 { 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 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 [] 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 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 name; 79 private final String branch; 80 private final String language; 81 private final long documentId; 82 private final long branchId; 83 private final long languageId; 84 85 public BookNode(String name, String branch, String 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 o) { 95 BookNode otherBookNode = (BookNode)o; 96 return name.compareTo(otherBookNode.name); 97 } 98 99 public void generateSaxFragment(ContentHandler contentHandler) throws SAXException { 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 |