KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.cocoon.components.flow.apples.StatelessAppleController;
19 import org.apache.cocoon.components.flow.apples.AppleRequest;
20 import org.apache.cocoon.components.flow.apples.AppleResponse;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.forms.formmodel.Form;
23 import org.apache.cocoon.forms.formmodel.Field;
24 import org.apache.cocoon.forms.FormContext;
25 import org.apache.cocoon.forms.util.StringMessage;
26 import org.apache.cocoon.forms.util.I18nMessage;
27 import org.apache.cocoon.forms.datatype.StaticSelectionList;
28 import org.apache.cocoon.forms.datatype.SelectionList;
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.outerj.daisy.frontend.util.AbstractDaisyApple;
33 import org.outerj.daisy.frontend.util.XmlObjectXMLizable;
34 import org.outerj.daisy.frontend.util.FormHelper;
35 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
36 import org.outerj.daisy.repository.Repository;
37 import org.outerj.daisy.repository.RepositoryException;
38 import org.outerj.daisy.repository.variant.Branch;
39 import org.outerj.daisy.repository.variant.Language;
40 import org.outerj.daisy.repository.query.QueryHelper;
41 import org.outerx.daisy.x10.SearchResultDocument;
42
43 import java.util.Locale JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * Apple for performing a simple fulltext search. For the search box on every page.
49  */

50 public class FulltextSearchApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
51     private static final int DEFAULT_LIMIT = 50;
52     private ServiceManager serviceManager;
53     private Repository repository;
54     private SiteConf siteConf;
55
56     public void service(ServiceManager serviceManager) throws ServiceException {
57         this.serviceManager = serviceManager;
58     }
59
60     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
61         Request request = appleRequest.getCocoonRequest();
62
63         Locale JavaDoc locale = WikiHelper.getLocale(request);
64         siteConf = WikiHelper.getSiteConf(request);
65         repository = WikiHelper.getRepository(request, serviceManager);
66
67         Form form = FormHelper.createForm(serviceManager, "resources/form/fulltextsearch_definition.xml");
68         boolean endProcessing = false;
69         if (request.getParameter("scope") != null) { // check if form submission by testing presence of one of the fields
70
endProcessing = form.process(new FormContext(request, locale));
71         } else {
72             // maybe user entered the fulltext search by only specifying a query request parameter
73
String JavaDoc query = request.getParameter("query");
74
75             if (query != null && query.length() > 0) {
76                 form.getChild("query").setValue(query);
77                 endProcessing = true;
78             }
79
80             // set default values
81
form.getChild("limit").setValue(new Integer JavaDoc(DEFAULT_LIMIT));
82             form.getChild("scope").setValue("currentSiteCollection");
83             form.getChild("searchName").setValue(Boolean.TRUE);
84             form.getChild("searchContent").setValue(Boolean.TRUE);
85             form.getChild("searchFields").setValue(Boolean.TRUE);
86             form.getChild("branchId").setValue(new Long JavaDoc(siteConf.getBranchId()));
87             form.getChild("languageId").setValue(new Long JavaDoc(siteConf.getLanguageId()));
88         }
89
90         ((Field)form.getChild("branchId")).setSelectionList(createList(repository.getVariantManager().getAllBranches(false).getArray(), form));
91         ((Field)form.getChild("languageId")).setSelectionList(createList(repository.getVariantManager().getAllLanguages(false).getArray(), form));
92
93         Map JavaDoc viewData = new HashMap JavaDoc();
94         viewData.put("CocoonFormsInstance", form);
95         viewData.put("pageContext", new PageContext(getMountPoint(), siteConf, repository, getLayoutType(), getSkin(), getContext()));
96         viewData.put("locale", locale);
97
98         if (endProcessing) {
99             String JavaDoc query = getQuery(form);
100             SearchResultDocument searchResultDocument = repository.getQueryManager().performQuery(query, locale);
101             viewData.put("pageXml", new XmlObjectXMLizable(searchResultDocument));
102         }
103
104         appleResponse.sendPage("Form-fulltextsearch-Pipe", viewData);
105     }
106
107     private String JavaDoc getQuery(Form form) throws RepositoryException {
108         String JavaDoc ftQuery = (String JavaDoc)form.getChild("query").getValue();
109         int limit = ((Integer JavaDoc)form.getChild("limit").getValue()).intValue();
110         String JavaDoc scope = (String JavaDoc)form.getChild("scope").getValue();
111
112         long branchId = ((Long JavaDoc)form.getChild("branchId").getValue()).longValue();
113         long languageId = ((Long JavaDoc)form.getChild("languageId").getValue()).longValue();
114
115         StringBuffer JavaDoc query = new StringBuffer JavaDoc(300);
116         query.append("select name, summary, versionStateLastModified where FullText(").append(QueryHelper.formatString(ftQuery));
117         query.append(", ").append(form.getChild("searchName").getValue().equals(Boolean.TRUE) ? "1" : "0");
118         query.append(", ").append(form.getChild("searchContent").getValue().equals(Boolean.TRUE) ? "1" : "0");
119         query.append(", ").append(form.getChild("searchFields").getValue().equals(Boolean.TRUE) ? "1" : "0");
120         query.append(", ").append(branchId);
121         query.append(", ").append(languageId);
122         query.append(")");
123
124         boolean nonFullTextConditions = false;
125
126         if (scope.equals("currentSiteCollection")) {
127             query.append(" and InCollection(").append(QueryHelper.formatString(getCollectionName())).append(")");
128             nonFullTextConditions = true;
129         }
130
131         if (nonFullTextConditions) {
132             if (branchId != -1)
133                 query.append(" and branchId = ").append(branchId);
134             if (languageId != -1)
135                 query.append(" and languageId = ").append(languageId);
136         }
137
138         query.append(" limit ").append(limit);
139
140         return query.toString();
141     }
142
143     private String JavaDoc getCollectionName() throws RepositoryException {
144         return repository.getCollectionManager().getCollection(siteConf.getCollectionId(), false).getName();
145     }
146
147     private SelectionList createList(Branch[] branches, Form form) {
148         StaticSelectionList selectionList = new StaticSelectionList(((Field)form.getChild("branchId")).getDatatype());
149         selectionList.addItem(new Long JavaDoc(-1), new I18nMessage("select-any"));
150
151         for (int i = 0; i < branches.length; i++) {
152             selectionList.addItem(new Long JavaDoc(branches[i].getId()), new StringMessage(branches[i].getName()));
153         }
154
155         return selectionList;
156     }
157
158     private SelectionList createList(Language[] languages, Form form) {
159         StaticSelectionList selectionList = new StaticSelectionList(((Field)form.getChild("languageId")).getDatatype());
160         selectionList.addItem(new Long JavaDoc(-1), new I18nMessage("select-any"));
161
162         for (int i = 0; i < languages.length; i++) {
163             selectionList.addItem(new Long JavaDoc(languages[i].getId()), new StringMessage(languages[i].getName()));
164         }
165
166         return selectionList;
167     }
168 }
169
Popular Tags