KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > search > Search_Engine


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// Search_Engine
14
// DJ 20.02.2001
15
//
16
// getInstance()
17
// authoriseRender()
18
// renderLink()
19
// needsJahiaData()
20
// handleActions()
21
//
22

23 package org.jahia.engines.search;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 import org.jahia.data.JahiaData;
30 import org.jahia.data.search.JahiaSearchResult;
31 import org.jahia.engines.EngineToolBox;
32 import org.jahia.engines.JahiaEngine;
33 import org.jahia.exceptions.JahiaException;
34 import org.jahia.params.ParamBean;
35 import org.jahia.registries.ServicesRegistry;
36 import org.jahia.utils.JahiaTools;
37 import org.jahia.services.search.*;
38 import org.jahia.services.sites.JahiaSite;
39
40
41 /**
42  * This engine is called when the user wants to do a search on the jahia site. It displays
43  * sorted results using a JSP output which leaves every aspect of the search results design on
44  * the JSP coder.
45  * <p/>
46  * This engine doesn't implement any search algorithm, it calls the JahiaSearchService for that.
47  * Instead, this engine is here for interaction between the user and the service.
48  * <p/>
49  * The JSP template uses searchResult attribute to get the search results, and searchString to
50  * see what the user searched for.
51  *
52  * @author David Jilli
53  * @see org.jahia.services.search.JahiaSearchService
54  */

55 public class Search_Engine implements JahiaEngine {
56
57     /** The engine's name. */
58     public static final String JavaDoc ENGINE_NAME = "search";
59
60     private static final String JavaDoc TEMPLATE_JSP = "/jsp/jahia/engines/search/searchresult.jsp";
61     private static final String JavaDoc SEARCH_JSP_NAME = "searchresult.jsp";
62     private static Search_Engine instance = null;
63     private EngineToolBox toolBox;
64
65     public static final String JavaDoc SEARCH_ALL_LANG = "all_lang";
66     public static final String JavaDoc SEARCH_ALL_SITE = "all_sites";
67
68     private static final org.apache.log4j.Logger logger =
69             org.apache.log4j.Logger.getLogger (Search_Engine.class);
70
71
72     /**
73      * constructor
74      */

75     private Search_Engine () {
76         logger.debug (
77                 "***** Starting " + Search_Engine.class.getName () + " engine *****");
78         toolBox = EngineToolBox.getInstance ();
79     }
80
81
82     /**
83      * returns a single instance of the object
84      */

85     public static synchronized Search_Engine getInstance () {
86         if (instance == null) {
87             instance = new Search_Engine ();
88         }
89         return instance;
90     }
91
92
93     /**
94      * authorises engine render
95      */

96     public boolean authoriseRender (ParamBean jParams) {
97         return true; // always allowed to render search
98
}
99
100
101     /**
102      * renders link to pop-up window
103      */

104     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
105             throws JahiaException {
106         String JavaDoc theUrl = jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING);
107         if (theObj != null)
108             theUrl = theUrl + theObj;
109         return jParams.getResponse ().encodeURL (theUrl);
110     }
111
112
113     /**
114      * specifies if the engine needs the JahiaData object
115      */

116     public boolean needsJahiaData (ParamBean jParams) {
117         return true;
118     }
119
120
121     /**
122      * handles the engine actions
123      *
124      * @param jParams a ParamBean object
125      * @param jData a JahiaData object (not mandatory)
126      */

127     public void handleActions (ParamBean jParams, JahiaData jData)
128             throws JahiaException {
129         // initalizes the hashmap
130
// HashMap engineMap = new HashMap();
131
HashMap JavaDoc engineMap = (HashMap JavaDoc) jParams.getRequest ().getAttribute ("engineMap");
132         if (engineMap == null)
133             engineMap = new HashMap JavaDoc ();
134
135         processScreen (jParams, jData, engineMap);
136
137         // displays the screen
138
toolBox.displayScreen (jParams, engineMap);
139
140     }
141
142     /**
143      * Retrieve the engine name.
144      *
145      * @return the engine name.
146      */

147     public final String JavaDoc getName () {
148         return ENGINE_NAME;
149     }
150
151
152     /**
153      * prepares the screen requested by the user
154      *
155      * @param jParams a ParamBean object
156      */

157     public void processScreen (ParamBean jParams, JahiaData jData, HashMap JavaDoc engineMap)
158             throws JahiaException {
159         // check if a searchresult template exists in the template
160
// directory, else uses the standard template found in the
161
// jsp/engines directory
162
String JavaDoc theTemplate = new String JavaDoc (TEMPLATE_JSP);
163         String JavaDoc fileName;
164
165         if ((jParams != null) && (jParams.getPage () != null) &&
166                 (jParams.getPage ().getPageTemplate () != null)) {
167             fileName = jParams.getPage ().getPageTemplate ().getSourcePath ();
168             if (fileName.lastIndexOf ("/") != -1) {
169                 fileName = fileName.substring (0, fileName.lastIndexOf ("/") + 1) +
170                         SEARCH_JSP_NAME;
171                 logger.debug ("Trying to redirect search result to : " + fileName);
172                 theTemplate = fileName;
173             }
174         }
175
176         String JavaDoc theScreen = jParams.getRequest ().getParameter ("screen");
177         if (theScreen == null) {
178             theScreen = "execute";
179         }
180
181         if (jParams.getRequest ().getParameter ("from") != null) {
182             engineMap.put ("searchResultFrom",
183                     Integer.valueOf (jParams.getRequest ().getParameter ("from")));
184         }
185         if (engineMap.get ("searchResultFrom") == null) {
186             engineMap.put ("searchResultFrom", new Integer JavaDoc (1));
187         }
188
189         String JavaDoc searchString = (String JavaDoc) engineMap.get ("searchString");
190         if (searchString == null) {
191             searchString = jParams.getRequest ().getParameter ("search");
192         }
193
194         if (searchString == null) {
195             searchString = EMPTY_STRING;
196         } else {
197             searchString = searchString.trim ();
198         }
199
200         Integer JavaDoc searchFrom = ((Integer JavaDoc) engineMap.get ("searchResultFrom"));
201         if (searchFrom == null) {
202             if (jParams.getRequest ().getParameter ("from") != null) {
203                 searchFrom = Integer.valueOf (jParams.getRequest ().getParameter ("from"));
204             }
205         }
206         if (searchFrom == null) {
207             searchFrom = new Integer JavaDoc(1);
208         }
209
210         Integer JavaDoc searchShow = ((Integer JavaDoc) engineMap.get ("searchShow"));
211         if (searchShow == null) {
212             if (jParams.getRequest ().getParameter ("show") != null) {
213                 searchShow = Integer.valueOf (jParams.getRequest ().getParameter ("show"));
214             }
215         }
216
217         //logger.debug ("searchString : > " + (String)engineMap.get ("searchString"));
218

219         // previous screen
220
if (theScreen.equals ("prev")) {
221             if (engineMap.get ("searchResultFrom") != null)
222                 engineMap.put ("searchResultFrom",
223                         new Integer JavaDoc (
224                                 ((searchFrom.intValue () - searchShow.intValue () < 1) ?
225                         1 : (searchFrom.intValue () - searchShow.intValue ()))));
226             searchFrom = new Integer JavaDoc (searchFrom.intValue () - searchShow.intValue ());
227         }
228
229         // next screen
230
if (theScreen.equals ("next")) {
231             if (engineMap.get ("searchResultFrom") != null)
232                 engineMap.put ("searchResultFrom",
233                         new Integer JavaDoc (searchFrom.intValue () + searchShow.intValue ()));
234             searchFrom = new Integer JavaDoc (searchFrom.intValue () + searchShow.intValue ());
235         }
236
237         String JavaDoc showStr = EMPTY_STRING;
238         if (searchShow != null){
239             showStr = "&show=" + searchShow;
240         }
241         engineMap.put ("searchPrevUrl",
242                 renderLink (jParams,
243                         showStr + "&screen=prev&search=" + searchString + "&from=" +
244                 searchFrom.intValue ()));
245         engineMap.put ("searchNextUrl",
246                 renderLink (jParams,
247                         showStr + "&screen=next&search=" + searchString + "&from=" +
248                 searchFrom.intValue ()));
249
250
251         // execute the search (called the 1st time)
252
engineMap.put (ENGINE_OUTPUT_FILE_PARAM, theTemplate);
253
254         engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
255         engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME);
256         engineMap.put (ENGINE_URL_PARAM, jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING));
257         engineMap.put ("jahiaBuild", new Integer JavaDoc (jParams.settings ().getBuildNumber ()));
258         engineMap.put ("javascriptUrl", jParams.settings ().getJsHttpPath ());
259
260         jParams.getRequest ().setAttribute ("engineMap", engineMap);
261
262         ArrayList JavaDoc languageCodes = new ArrayList JavaDoc ();
263         String JavaDoc[] languageCodesVal = jParams.getRequest ().getParameterValues ("searchlang");
264         if (languageCodesVal != null) {
265             if (!JahiaTools.inValues (Search_Engine.SEARCH_ALL_LANG, languageCodesVal)) {
266                 for (int i = 0; i < languageCodesVal.length; i++) {
267                     languageCodes.add (languageCodesVal[i]);
268                 }
269             }
270         } else {
271             languageCodes.add (jParams.getLocale ().toString ());
272         }
273
274         ArrayList JavaDoc siteIds = new ArrayList JavaDoc ();
275         String JavaDoc[] siteIdsVal = jParams.getRequest ().getParameterValues ("searchsiteid");
276         if (siteIdsVal != null) {
277             if (!JahiaTools.inValues (Search_Engine.SEARCH_ALL_SITE, siteIdsVal)) {
278                 for (int i = 0; i < siteIdsVal.length; i++) {
279                     siteIds.add (new Integer JavaDoc((String JavaDoc)siteIdsVal[i]));
280                 }
281             } else {
282                 Enumeration JavaDoc sites = ServicesRegistry.getInstance().getJahiaSitesService().getSites();
283                 JahiaSite site = null;
284                 while ( sites.hasMoreElements() ){
285                     site = (JahiaSite)sites.nextElement();
286                     siteIds.add(new Integer JavaDoc(site.getID()));
287                 }
288             }
289         } else {
290             siteIds.add (new Integer JavaDoc(jParams.getJahiaID()));
291         }
292         int[] sids = new int[siteIds.size()];
293         Integer JavaDoc I = null;
294         for ( int i=0; i<siteIds.size(); i++ ){
295             I = (Integer JavaDoc)siteIds.get(i);
296             sids[i]=I.intValue();
297         }
298
299         //logger.debug ("Searchstring: "+searchString);
300
JahiaSearchResult searchResults = null;
301         if ( theScreen.equals("execute") || engineMap.get("searchResults") == null ){
302             PageSearcher searcher = new PageSearcher(sids,languageCodes);
303             searcher.setQuery(searchString);
304             searchResults = ServicesRegistry
305                 .getInstance().getJahiaSearchService().search(searcher,jParams);
306         }
307         if (searchResults == null) {
308             searchResults = new JahiaSearchResult(new
309                 JahiaSearchResultHandlerImpl());
310         }
311         engineMap.put("searchShow", searchShow);
312         engineMap.put("searchResults", searchResults);
313         engineMap.put("searchString", searchString);
314     }
315 }
316
Popular Tags