KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > BrowseDocumentsApple


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;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
20 import org.outerj.daisy.repository.Repository;
21 import org.outerj.daisy.repository.query.QueryManager;
22 import org.outerj.daisy.repository.query.QueryHelper;
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.ResourceNotFoundException;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.outerx.daisy.x10.SearchResultDocument;
32
33 import java.util.Locale JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 public class BrowseDocumentsApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
38     private ServiceManager serviceManager;
39
40     public void service(ServiceManager serviceManager) throws ServiceException {
41         this.serviceManager = serviceManager;
42     }
43
44     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
45         Request request = appleRequest.getCocoonRequest();
46         Locale JavaDoc locale = WikiHelper.getLocale(request);
47         SiteConf siteConf = WikiHelper.getSiteConf(request);
48         Repository repository = WikiHelper.getRepository(request, serviceManager);
49
50         String JavaDoc resource = appleRequest.getSitemapParameter("resource");
51         if (resource == null) {
52             long branchId = RequestUtil.getBranchId(request, siteConf.getBranchId(), repository);
53             long languageId = RequestUtil.getLanguageId(request, siteConf.getLanguageId(), repository);
54             Map JavaDoc viewData = new HashMap JavaDoc();
55             viewData.put("branchId", String.valueOf(branchId));
56             viewData.put("languageId", String.valueOf(languageId));
57             viewData.put("branch", repository.getVariantManager().getBranch(branchId, false).getName());
58             viewData.put("language", repository.getVariantManager().getLanguage(languageId, false).getName());
59             viewData.put("branches", repository.getVariantManager().getAllBranches(false).getArray());
60             viewData.put("languages", repository.getVariantManager().getAllLanguages(false).getArray());
61             viewData.put("versionMode", WikiHelper.getVersionMode(request));
62             viewData.put("siteConf", siteConf);
63             viewData.put("mountPoint", getMountPoint());
64             appleResponse.sendPage("Template-documentbrowser-Pipe", viewData);
65         } else if (resource.equals("documents")) {
66             WikiVersionMode versionMode = WikiVersionMode.fromString(request.getParameter("versionMode"));
67             String JavaDoc query = getQuery(request, siteConf, repository, versionMode);
68             QueryManager queryManager = repository.getQueryManager();
69             SearchResultDocument result = queryManager.performQuery(query, locale);
70
71             Map JavaDoc viewData = new HashMap JavaDoc();
72             viewData.put("searchResult", result.getSearchResult());
73             viewData.put("versionMode", versionMode);
74             viewData.put("siteConf", siteConf);
75             viewData.put("mountPoint", getMountPoint());
76             appleResponse.sendPage("Template-documentbrowser_docs-Pipe", viewData);
77         } else {
78             throw new ResourceNotFoundException("Unsupported resource: " + resource);
79         }
80     }
81
82     private String JavaDoc getQuery(Request request, SiteConf siteConf, Repository repository, WikiVersionMode versionMode) throws Exception JavaDoc {
83         String JavaDoc name = request.getParameter("name");
84         String JavaDoc fulltextQuery = request.getParameter("fulltextQuery");
85         String JavaDoc collections = request.getParameter("collections");
86         long branchId = RequestUtil.getLongParameter(request, "branchId");
87         long languageId = RequestUtil.getLongParameter(request, "languageId");
88         boolean attachmentDocType = request.getParameter("attachmentDocType") != null;
89         boolean imageDocType = request.getParameter("imageDocType") != null;
90
91         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
92         query.append("select name, branch, language where ");
93
94         if (fulltextQuery != null && fulltextQuery.length() > 0) {
95             query.append("FullText(").append(QueryHelper.formatString(fulltextQuery));
96             query.append(", 1, 1, 1, ").append(branchId).append(", ").append(languageId).append(")");
97         } else {
98             query.append("true");
99         }
100
101         boolean nonFullTextConditions = false;
102
103         if (name != null && name.length() > 0) {
104             query.append(" and name like ").append(QueryHelper.formatString(name));
105             nonFullTextConditions = true;
106         }
107
108         if ("current".equals(collections)) {
109             String JavaDoc collectionName = repository.getCollectionManager().getCollection(siteConf.getCollectionId(), false).getName();
110             query.append(" and InCollection(").append(QueryHelper.formatString(collectionName)).append(")");
111             nonFullTextConditions = true;
112         }
113
114         if (imageDocType || attachmentDocType) {
115             query.append(" and (");
116             boolean first = true;
117             if (imageDocType) {
118                 query.append("documentType!='Image'");
119                 first = false;
120             }
121             if (attachmentDocType) {
122                 if (!first)
123                     query.append(" and ");
124                 query.append("documentType!='Attachment'");
125             }
126             query.append(")");
127             nonFullTextConditions = true;
128         }
129
130         if (nonFullTextConditions) {
131             if (branchId != -1)
132                 query.append(" and branchId = ").append(branchId);
133             if (languageId != -1)
134                 query.append(" and languageId = ").append(languageId);
135         }
136
137         query.append(" order by name");
138
139         if (versionMode == WikiVersionMode.LAST)
140             query.append(" option search_last_version = 'true'");
141
142         return query.toString();
143     }
144 }
145
Popular Tags