1 12 package org.eclipse.help.internal.webapp.data; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.http.Cookie ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.eclipse.help.HelpSystem; 26 import org.eclipse.help.IHelpResource; 27 import org.eclipse.help.IToc; 28 import org.eclipse.help.ITopic; 29 import org.eclipse.help.internal.HelpPlugin; 30 import org.eclipse.help.internal.base.BaseHelpSystem; 31 import org.eclipse.help.internal.base.HelpBasePlugin; 32 import org.eclipse.help.internal.search.ISearchQuery; 33 import org.eclipse.help.internal.search.QueryTooComplexException; 34 import org.eclipse.help.internal.search.SearchHit; 35 import org.eclipse.help.internal.search.SearchProgressMonitor; 36 import org.eclipse.help.internal.search.SearchQuery; 37 import org.eclipse.help.internal.search.SearchResults; 38 import org.eclipse.help.internal.webapp.HelpWebappPlugin; 39 import org.eclipse.help.internal.webapp.servlet.WebappWorkingSetManager; 40 import org.eclipse.help.internal.workingset.AdaptableToc; 41 import org.eclipse.help.internal.workingset.WorkingSet; 42 import org.eclipse.help.search.ISearchEngineResult; 43 import org.eclipse.help.search.ISearchEngineResult2; 44 45 48 public class SearchData extends ActivitiesData { 49 private WebappWorkingSetManager wsmgr; 50 51 private String topicHref; 53 54 private String selectedTopicId = ""; 56 private String searchWord; 57 58 private String workingSetName; 59 60 SearchHit[] hits; 62 63 private int indexCompletion = 100; 65 66 private QueryTooComplexException queryException = null; 68 69 75 public SearchData(ServletContext context, HttpServletRequest request, 76 HttpServletResponse response) { 77 super(context, request, response); 78 wsmgr = new WebappWorkingSetManager(request, response, getLocale()); 79 this.topicHref = request.getParameter("topic"); if (topicHref != null && topicHref.length() == 0) 81 topicHref = null; 82 83 searchWord = request.getParameter("searchWord"); 85 if (isSearchRequest() && !isScopeRequest()) { 87 loadSearchResults(); 88 if (queryException != null) { 89 return; 90 } 91 if (!isProgressRequest()) { 92 for (int i = 0; i < hits.length; i++) { 93 if (hits[i].getHref().equals(topicHref)) { 95 selectedTopicId = "a" + i; break; 97 } 98 } 99 } 100 } 101 } 102 103 108 public boolean isSearchRequest() { 109 return (request.getParameter("searchWord") != null); } 111 112 115 public boolean isProgressRequest() { 116 return (hits == null && indexCompletion != 100); 117 } 118 119 122 public boolean isScopeRequest() { 123 return (request.getParameter("workingSet") != null); } 125 126 public String getCategoryLabel(int i) { 127 IHelpResource cat = hits[i].getCategory(); 128 if (cat != null) { 129 return cat.getLabel(); 130 } 131 return null; 132 } 133 134 public String getCategoryHref(int i) { 135 IHelpResource cat = hits[i].getCategory(); 136 if (cat != null) { 137 String tocHref = cat.getHref(); 138 IToc[] tocs = HelpSystem.getTocs(); 139 for (int j=0;j<tocs.length;++j) { 140 if (tocHref.equals(tocs[j].getHref())) { 141 ITopic topic = tocs[j].getTopic(null); 142 String topicHref = topic.getHref(); 143 if (topicHref != null) { 144 return UrlUtil.getHelpURL(topicHref); 145 } 146 return "../nav/" + j; } 148 } 149 } 150 return null; 151 } 152 153 158 public int getResultsCount() { 159 return hits.length; 160 } 161 162 public String getSelectedTopicId() { 163 return selectedTopicId; 164 } 165 166 public String getTopicHref(int i) { 167 return UrlUtil.getHelpURL(hits[i].getHref()); 168 } 169 170 public String getTopicLabel(int i) { 171 return UrlUtil.htmlEncode(hits[i].getLabel()); 172 } 173 174 public String getTopicTocLabel(int i) { 175 if (hits[i].getToc() != null) 176 return UrlUtil.htmlEncode(hits[i].getToc().getLabel()); 177 return ""; } 179 180 public String getTopicDescription(int i) { 181 String description = hits[i].getDescription(); 182 if (description != null) { 183 return UrlUtil.htmlEncode(description); 184 } 185 return ""; } 187 188 192 public boolean isEnabled(int i) { 193 String href = hits[i].getHref(); 194 return HelpBasePlugin.getActivitySupport().isEnabledTopic(href, 195 getLocale()); 196 } 197 198 206 public boolean isPotentialHit(int i) { 207 return ((getMode() != MODE_INFOCENTER) && hits[i].isPotentialHit()); 208 } 209 210 public boolean isShowCategories() { 211 Cookie [] cookies = request.getCookies(); 212 if (cookies != null) { 213 for (int i=0;i<cookies.length;++i) { 214 if ("showCategories".equals(cookies[i].getName())) { return String.valueOf(true).equals(cookies[i].getValue()); 216 } 217 } 218 } 219 return false; 221 } 222 223 public boolean isShowDescriptions() { 224 Cookie [] cookies = request.getCookies(); 225 if (cookies != null) { 226 for (int i=0;i<cookies.length;++i) { 227 if ("showDescriptions".equals(cookies[i].getName())) { return String.valueOf(true).equals(cookies[i].getValue()); 229 } 230 } 231 } 232 return true; 234 } 235 236 239 public String getIndexedPercentage() { 240 return String.valueOf(indexCompletion); 241 } 242 243 246 public String getSearchWord() { 247 if (searchWord == null) 248 return ""; return searchWord; 250 } 251 252 255 public String [] getSelectedTocs() { 256 String [] books = request.getParameterValues("scope"); if (books == null) { 258 TocData tocData = new TocData(context, request, response); 260 books = new String [tocData.getTocCount()]; 261 for (int i = 0; i < books.length; i++) 262 books[i] = tocData.getTocHref(i); 263 } 264 return books; 265 } 266 267 270 public boolean isTocSelected(int toc) { 271 TocData tocData = new TocData(context, request, response); 272 String href = tocData.getTocHref(toc); 273 String [] books = request.getParameterValues("scope"); if (books == null) 275 return false; 276 for (int i = 0; i < books.length; i++) { 277 if (books[i].equals(href)) { 278 return true; 279 } 280 } 281 return false; 282 } 283 284 290 public String getScope() { 291 if (workingSetName != null) 292 return workingSetName; 293 294 if (isScopeRequest()) { 295 workingSetName = request.getParameter("workingSet"); } else if (isSearchRequest()) { 297 workingSetName = request.getParameter("scope"); if (workingSetName == null) 300 workingSetName = request.getParameter("workingSet"); } else if (getWorkingSets() == null || getWorkingSets().length == 0) { 302 workingSetName = ServletResources.getString("All", request); } else { 304 workingSetName = wsmgr.getCurrentWorkingSet(); 305 } 306 307 if (workingSetName == null || workingSetName.length() == 0 308 || getMode() == RequestData.MODE_INFOCENTER 309 && wsmgr.getWorkingSet(workingSetName) == null) 310 workingSetName = ServletResources.getString("All", request); return workingSetName; 312 } 313 314 318 public void saveScope() { 319 String workingSet = request.getParameter("scope"); String lastWS = wsmgr.getCurrentWorkingSet(); 322 if (workingSet != null && !workingSet.equals(lastWS)) { 323 wsmgr.setCurrentWorkingSet(workingSet); 324 } else if (workingSet == null && lastWS != null && lastWS.length() > 0) { 325 wsmgr.setCurrentWorkingSet(""); } 327 } 328 329 333 private void loadSearchResults() { 334 try { 335 SearchProgressMonitor pm = SearchProgressMonitor 336 .getProgressMonitor(getLocale()); 337 if (pm.isDone()) { 338 this.indexCompletion = 100; 339 SearchResults results = createHitCollector(); 340 BaseHelpSystem.getSearchManager().search(createSearchQuery(), 341 results, pm); 342 hits = results.getSearchHits(); 343 if (hits == null) { 344 HelpWebappPlugin 345 .logWarning("No search results returned. Help index is in use."); } 347 else { 348 if (isShowCategories()) { 349 Arrays.sort(hits, new SearchResultComparator()); 350 } 351 } 352 return; 353 } 354 indexCompletion = pm.getPercentage(); 356 if (indexCompletion >= 100) { 357 indexCompletion = 100 - 1; 359 } 360 return; 361 } catch (QueryTooComplexException qe) { 362 queryException = qe; 363 } catch (Exception e) { 364 this.indexCompletion = 0; 365 } 366 367 } 368 369 private ISearchQuery createSearchQuery() { 370 String fieldSearchStr = request.getParameter("fieldSearch"); boolean fieldSearch = fieldSearchStr != null ? new Boolean ( 372 fieldSearchStr).booleanValue() : false; 373 return new SearchQuery(searchWord == null ? "" : searchWord, fieldSearch, new ArrayList (), getLocale()); 375 } 376 377 private SearchResults createHitCollector() { 378 WorkingSet[] workingSets; 379 if (request.getParameterValues("scopedSearch") == null) { workingSets = getWorkingSets(); 382 } else { 383 workingSets = createTempWorkingSets(); 385 } 386 387 int maxHits = 500; 388 String maxHitsStr = request.getParameter("maxHits"); if (maxHitsStr != null) { 390 try { 391 int clientmaxHits = Integer.parseInt(maxHitsStr); 392 if (0 < clientmaxHits && clientmaxHits < 500) { 393 maxHits = clientmaxHits; 394 } 395 } catch (NumberFormatException nfe) { 396 } 397 } 398 return new SearchResultFilter(workingSets, maxHits, getLocale()); 399 } 400 401 404 private WorkingSet[] getWorkingSets() { 405 String [] scopes = request.getParameterValues("scope"); if (scopes == null) { 407 return null; 408 } 409 ArrayList workingSetCol = new ArrayList (scopes.length); 411 for (int s = 0; s < scopes.length; s++) { 412 WorkingSet ws = wsmgr.getWorkingSet(scopes[s]); 413 if (ws != null) { 414 workingSetCol.add(ws); 415 } 416 } 417 if (workingSetCol.size() == 0) { 418 return null; 419 } 420 return (WorkingSet[]) workingSetCol 421 .toArray(new WorkingSet[workingSetCol.size()]); 422 } 423 424 427 private WorkingSet[] createTempWorkingSets() { 428 String [] scopes = request.getParameterValues("scope"); if (scopes == null) { 430 return new WorkingSet[0]; 433 } 434 if (scopes.length == HelpPlugin.getTocManager().getTocs(getLocale()).length) { 435 return null; 437 } 438 ArrayList tocs = new ArrayList (scopes.length); 440 for (int s = 0; s < scopes.length; s++) { 441 AdaptableToc toc = wsmgr.getAdaptableToc(scopes[s]); 442 if (toc != null) { 443 tocs.add(toc); 444 } 445 } 446 AdaptableToc[] adaptableTocs = (AdaptableToc[]) tocs 447 .toArray(new AdaptableToc[tocs.size()]); 448 WorkingSet[] workingSets = new WorkingSet[1]; 449 workingSets[0] = wsmgr.createWorkingSet("temp", adaptableTocs); return workingSets; 451 } 452 453 public String getQueryExceptionMessage() { 454 if (queryException == null) { 455 return null; 456 } 457 return ServletResources.getString("searchTooComplex", request); } 459 460 464 private static class SearchResultFilter extends SearchResults { 465 public SearchResultFilter(WorkingSet[] workingSets, int maxHits, String locale) { 466 super(workingSets, maxHits, locale); 467 } 468 public void addHits(List hits, String highlightTerms) { 469 List filtered = new ArrayList (); 470 Iterator iter = hits.iterator(); 471 while (iter.hasNext()) { 472 Object obj = iter.next(); 473 if (!(obj instanceof ISearchEngineResult2 && ((ISearchEngineResult2)obj).canOpen())) { 474 filtered.add(obj); 475 } 476 } 477 super.addHits(filtered, highlightTerms); 478 } 479 } 480 481 private static class SearchResultComparator implements Comparator { 482 public int category(Object element) { 483 if (element instanceof ISearchEngineResult) { 484 ISearchEngineResult r = (ISearchEngineResult)element; 485 IHelpResource c = r.getCategory(); 486 if (c!=null) { 487 String label = c.getLabel(); 488 if (label.length()==0) 489 return 10; 490 return 5; 491 } 492 } 493 return 0; 494 } 495 496 public int compare(Object e1, Object e2) { 497 int cat1 = category(e1); 498 int cat2 = category(e2); 499 if (cat1 != cat2) { 500 return cat1 - cat2; 501 } 502 ISearchEngineResult r1 = (ISearchEngineResult)e1; 503 ISearchEngineResult r2 = (ISearchEngineResult)e2; 504 IHelpResource c1 = r1.getCategory(); 505 IHelpResource c2 = r2.getCategory(); 506 if (c1 != null && c2 != null) { 507 int cat = c1.getLabel().compareToIgnoreCase(c2.getLabel()); 508 if (cat != 0) { 509 return cat; 510 } 511 } 512 float rank1 = ((ISearchEngineResult)e1).getScore(); 513 float rank2 = ((ISearchEngineResult)e2).getScore(); 514 if (rank1 - rank2 > 0) { 515 return -1; 516 } 517 else if (rank1 == rank2) { 518 return 0; 519 } 520 else { 521 return 1; 522 } 523 } 524 } 525 } 526 | Popular Tags |