1 23 package org.jahia.engines.search; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Enumeration ; 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 55 public class Search_Engine implements JahiaEngine { 56 57 58 public static final String ENGINE_NAME = "search"; 59 60 private static final String TEMPLATE_JSP = "/jsp/jahia/engines/search/searchresult.jsp"; 61 private static final String SEARCH_JSP_NAME = "searchresult.jsp"; 62 private static Search_Engine instance = null; 63 private EngineToolBox toolBox; 64 65 public static final String SEARCH_ALL_LANG = "all_lang"; 66 public static final String 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 75 private Search_Engine () { 76 logger.debug ( 77 "***** Starting " + Search_Engine.class.getName () + " engine *****"); 78 toolBox = EngineToolBox.getInstance (); 79 } 80 81 82 85 public static synchronized Search_Engine getInstance () { 86 if (instance == null) { 87 instance = new Search_Engine (); 88 } 89 return instance; 90 } 91 92 93 96 public boolean authoriseRender (ParamBean jParams) { 97 return true; } 99 100 101 104 public String renderLink (ParamBean jParams, Object theObj) 105 throws JahiaException { 106 String 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 116 public boolean needsJahiaData (ParamBean jParams) { 117 return true; 118 } 119 120 121 127 public void handleActions (ParamBean jParams, JahiaData jData) 128 throws JahiaException { 129 HashMap engineMap = (HashMap ) jParams.getRequest ().getAttribute ("engineMap"); 132 if (engineMap == null) 133 engineMap = new HashMap (); 134 135 processScreen (jParams, jData, engineMap); 136 137 toolBox.displayScreen (jParams, engineMap); 139 140 } 141 142 147 public final String getName () { 148 return ENGINE_NAME; 149 } 150 151 152 157 public void processScreen (ParamBean jParams, JahiaData jData, HashMap engineMap) 158 throws JahiaException { 159 String theTemplate = new String (TEMPLATE_JSP); 163 String 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 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 (1)); 187 } 188 189 String searchString = (String ) 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 searchFrom = ((Integer ) 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 (1); 208 } 209 210 Integer searchShow = ((Integer ) 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 219 if (theScreen.equals ("prev")) { 221 if (engineMap.get ("searchResultFrom") != null) 222 engineMap.put ("searchResultFrom", 223 new Integer ( 224 ((searchFrom.intValue () - searchShow.intValue () < 1) ? 225 1 : (searchFrom.intValue () - searchShow.intValue ())))); 226 searchFrom = new Integer (searchFrom.intValue () - searchShow.intValue ()); 227 } 228 229 if (theScreen.equals ("next")) { 231 if (engineMap.get ("searchResultFrom") != null) 232 engineMap.put ("searchResultFrom", 233 new Integer (searchFrom.intValue () + searchShow.intValue ())); 234 searchFrom = new Integer (searchFrom.intValue () + searchShow.intValue ()); 235 } 236 237 String 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 engineMap.put (ENGINE_OUTPUT_FILE_PARAM, theTemplate); 253 254 engineMap.put (RENDER_TYPE_PARAM, new Integer (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 (jParams.settings ().getBuildNumber ())); 258 engineMap.put ("javascriptUrl", jParams.settings ().getJsHttpPath ()); 259 260 jParams.getRequest ().setAttribute ("engineMap", engineMap); 261 262 ArrayList languageCodes = new ArrayList (); 263 String [] 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 siteIds = new ArrayList (); 275 String [] 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 ((String )siteIdsVal[i])); 280 } 281 } else { 282 Enumeration sites = ServicesRegistry.getInstance().getJahiaSitesService().getSites(); 283 JahiaSite site = null; 284 while ( sites.hasMoreElements() ){ 285 site = (JahiaSite)sites.nextElement(); 286 siteIds.add(new Integer (site.getID())); 287 } 288 } 289 } else { 290 siteIds.add (new Integer (jParams.getJahiaID())); 291 } 292 int[] sids = new int[siteIds.size()]; 293 Integer I = null; 294 for ( int i=0; i<siteIds.size(); i++ ){ 295 I = (Integer )siteIds.get(i); 296 sids[i]=I.intValue(); 297 } 298 299 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 |