1 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 ; 44 import java.util.HashMap ; 45 import java.util.Map ; 46 47 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 { 61 Request request = appleRequest.getCocoonRequest(); 62 63 Locale 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) { endProcessing = form.process(new FormContext(request, locale)); 71 } else { 72 String query = request.getParameter("query"); 74 75 if (query != null && query.length() > 0) { 76 form.getChild("query").setValue(query); 77 endProcessing = true; 78 } 79 80 form.getChild("limit").setValue(new Integer (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 (siteConf.getBranchId())); 87 form.getChild("languageId").setValue(new Long (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 viewData = new HashMap (); 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 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 getQuery(Form form) throws RepositoryException { 108 String ftQuery = (String )form.getChild("query").getValue(); 109 int limit = ((Integer )form.getChild("limit").getValue()).intValue(); 110 String scope = (String )form.getChild("scope").getValue(); 111 112 long branchId = ((Long )form.getChild("branchId").getValue()).longValue(); 113 long languageId = ((Long )form.getChild("languageId").getValue()).longValue(); 114 115 StringBuffer query = new StringBuffer (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 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 (-1), new I18nMessage("select-any")); 150 151 for (int i = 0; i < branches.length; i++) { 152 selectionList.addItem(new Long (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 (-1), new I18nMessage("select-any")); 161 162 for (int i = 0; i < languages.length; i++) { 163 selectionList.addItem(new Long (languages[i].getId()), new StringMessage(languages[i].getName())); 164 } 165 166 return selectionList; 167 } 168 } 169 | Popular Tags |