1 31 32 package org.opencms.search; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.i18n.CmsEncoder; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.CmsIllegalArgumentException; 38 import org.opencms.main.CmsLog; 39 import org.opencms.main.OpenCms; 40 import org.opencms.util.CmsStringUtil; 41 42 import java.util.ArrayList ; 43 import java.util.Arrays ; 44 import java.util.Collections ; 45 import java.util.Iterator ; 46 import java.util.LinkedList ; 47 import java.util.List ; 48 import java.util.Map ; 49 import java.util.TreeMap ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.lucene.search.Sort; 53 54 77 public class CmsSearch implements Cloneable { 78 79 80 private static final Log LOG = CmsLog.getLog(CmsSearch.class); 81 82 83 protected Map m_categoriesFound; 84 85 86 protected transient CmsObject m_cms; 87 88 89 protected int m_displayPages; 90 91 92 protected Exception m_lastException; 93 94 95 protected int m_matchesPerPage; 96 97 98 protected String m_nextUrl; 99 100 101 protected int m_pageCount; 102 103 104 protected CmsSearchParameters m_parameterRestriction; 105 106 107 protected CmsSearchParameters m_parameters; 108 109 110 protected String m_prevUrl; 111 112 113 protected List m_result; 114 115 116 protected String m_searchParameters; 117 118 119 protected int m_searchResultCount; 120 121 124 public CmsSearch() { 125 126 super(); 127 128 m_parameters = new CmsSearchParameters(); 129 m_parameters.setSearchRoots(""); 130 m_parameters.setSearchPage(1); 131 m_searchResultCount = 0; 132 m_matchesPerPage = 10; 133 m_displayPages = 10; 134 m_parameters.setSort(CmsSearchParameters.SORT_DEFAULT); 135 List fields = new ArrayList (2); 136 fields.add(CmsSearchIndex.DOC_META_FIELDS[0]); 137 fields.add(CmsSearchIndex.DOC_META_FIELDS[1]); 138 m_parameters.setFields(fields); 139 } 140 141 151 public boolean getCalculateCategories() { 152 153 return m_parameters.getCalculateCategories(); 154 } 155 156 161 public String [] getCategories() { 162 163 List l = m_parameters.getCategories(); 164 return (String [])l.toArray(new String [l.size()]); 165 } 166 167 172 public int getDisplayPages() { 173 174 return m_displayPages; 175 } 176 177 182 public String getFields() { 183 184 if (m_parameters.getFields() == null) { 185 return ""; 186 } 187 StringBuffer result = new StringBuffer (); 188 Iterator it = m_parameters.getFields().iterator(); 189 while (it.hasNext()) { 190 result.append(it.next()); 191 result.append(" "); 192 } 193 return result.toString(); 194 } 195 196 201 public String getIndex() { 202 203 return m_parameters.getSearchIndex().getName(); 204 } 205 206 211 public Exception getLastException() { 212 213 return m_lastException; 214 } 215 216 221 public int getMatchesPerPage() { 222 223 return m_matchesPerPage; 224 } 225 226 231 public String getNextUrl() { 232 233 return m_nextUrl; 234 } 235 236 244 public Map getPageLinks() { 245 246 Map links = new TreeMap (); 247 if (m_pageCount <= 1) { 248 return links; 249 } 250 int startIndex, endIndex; 251 String link = m_cms.getRequestContext().getUri() + getSearchParameters() + "&searchPage="; 252 if (getDisplayPages() < 1) { 253 startIndex = 1; 255 endIndex = m_pageCount; 256 } else { 257 int currentPage = getSearchPage(); 259 int countBeforeCurrent = getDisplayPages() / 2; 260 int countAfterCurrent; 261 if ((currentPage - countBeforeCurrent) < 1) { 262 countBeforeCurrent = currentPage - 1; 264 } 265 countAfterCurrent = getDisplayPages() - countBeforeCurrent - 1; 267 startIndex = currentPage - countBeforeCurrent; 269 endIndex = currentPage + countAfterCurrent; 270 if (endIndex > m_pageCount) { 272 int delta = endIndex - m_pageCount; 273 startIndex -= delta; 275 if (startIndex < 1) { 277 startIndex = 1; 278 } 279 endIndex = m_pageCount; 280 } 281 } 282 283 for (int i = startIndex; i <= endIndex; i++) { 285 links.put(new Integer (i), (link + i)); 286 } 287 return links; 288 } 289 290 295 public CmsSearchParameters getParameters() { 296 297 if (m_parameterRestriction != null) { 298 m_parameters = m_parameters.restrict(m_parameterRestriction); 299 } 300 return m_parameters; 301 302 } 303 304 309 public String getPreviousUrl() { 310 311 return m_prevUrl; 312 } 313 314 319 public String getQuery() { 320 321 return m_parameters.getQuery(); 322 } 323 324 329 public int getQueryLength() { 330 331 return m_parameters.getQueryLength(); 332 } 333 334 339 public int getSearchPage() { 340 341 return m_parameters.getSearchPage(); 342 } 343 344 349 public String getSearchParameters() { 350 351 StringBuffer params = new StringBuffer (128); 353 params.append("?action=search&query="); 354 params.append(CmsEncoder.encodeParameter(m_parameters.getQuery())); 355 356 params.append("&matchesPerPage="); 357 params.append(getMatchesPerPage()); 358 params.append("&displayPages="); 359 params.append(getDisplayPages()); 360 params.append("&index="); 361 params.append(CmsEncoder.encodeParameter(m_parameters.getIndex())); 362 363 Sort sort = m_parameters.getSort(); 364 if (sort != CmsSearchParameters.SORT_DEFAULT) { 365 params.append("&sort="); 366 if (sort == CmsSearchParameters.SORT_TITLE) { 368 params.append("title"); 369 } else if (sort == CmsSearchParameters.SORT_DATE_CREATED) { 370 params.append("date-created"); 371 } else if (sort == CmsSearchParameters.SORT_DATE_LASTMODIFIED) { 372 params.append("date-lastmodified"); 373 } 374 } 375 376 if (m_parameters.getCategories() != null) { 377 params.append("&category="); 378 Iterator it = m_parameters.getCategories().iterator(); 379 while (it.hasNext()) { 380 params.append(it.next()); 381 if (it.hasNext()) { 382 params.append(','); 383 } 384 } 385 } 386 387 if (m_parameters.getRoots() != null) { 388 params.append("&searchRoots="); 389 Iterator it = m_parameters.getRoots().iterator(); 390 while (it.hasNext()) { 391 params.append(CmsEncoder.encode((String )it.next())); 392 if (it.hasNext()) { 393 params.append(','); 394 } 395 } 396 } 397 398 int todo = 0; 400 406 return params.toString(); 407 413 } 414 415 420 public List getSearchResult() { 421 422 if (m_cms != null 423 && m_result == null 424 && m_parameters.getIndex() != null 425 && CmsStringUtil.isNotEmpty(m_parameters.getQuery())) { 426 427 if ((getQueryLength() > 0) && (m_parameters.getQuery().trim().length() < getQueryLength())) { 428 429 m_lastException = new CmsSearchException(Messages.get().container( 430 Messages.ERR_QUERY_TOO_SHORT_1, 431 new Integer (getQueryLength()))); 432 433 return m_result; 434 } 435 436 try { 437 438 CmsSearchResultList result = m_parameters.getSearchIndex().search( 439 m_cms, 440 getParameters(), 441 m_matchesPerPage); 442 443 if (result.size() > 0) { 444 445 m_result = result; 446 m_searchResultCount = result.getHitCount(); 447 m_categoriesFound = result.getCategories(); 448 449 m_pageCount = m_searchResultCount / m_matchesPerPage; 451 if ((m_searchResultCount % m_matchesPerPage) != 0) { 452 m_pageCount++; 453 } 454 455 String url = m_cms.getRequestContext().getUri() + getSearchParameters() + "&searchPage="; 457 if (m_parameters.getSearchPage() > 1) { 458 m_prevUrl = url + (m_parameters.getSearchPage() - 1); 459 } 460 if (m_parameters.getSearchPage() < m_pageCount) { 461 m_nextUrl = url + (m_parameters.getSearchPage() + 1); 462 } 463 } else { 464 m_result = Collections.EMPTY_LIST; 465 m_searchResultCount = 0; 466 m_categoriesFound = null; 467 m_pageCount = 0; 468 m_prevUrl = null; 469 m_nextUrl = null; 470 } 471 } catch (Exception exc) { 472 473 if (LOG.isDebugEnabled()) { 474 LOG.debug(Messages.get().getBundle().key(Messages.LOG_SEARCHING_FAILED_0), exc); 475 } 476 477 m_result = null; 478 m_searchResultCount = 0; 479 m_pageCount = 0; 480 481 m_lastException = exc; 482 } 483 } 484 485 return m_result; 486 } 487 488 497 public Map getSearchResultCategories() { 498 499 return m_categoriesFound; 500 } 501 502 507 public int getSearchResultCount() { 508 509 return m_searchResultCount; 510 } 511 512 525 public String [] getSearchRoots() { 526 527 List l = m_parameters.getRoots(); 528 return (String [])l.toArray(new String [l.size()]); 529 } 530 531 536 public Sort getSortOrder() { 537 538 return m_parameters.getSort(); 539 } 540 541 546 public void init(CmsObject cms) { 547 548 m_cms = cms; 549 m_result = null; 550 m_lastException = null; 551 m_pageCount = 0; 552 m_nextUrl = null; 553 m_prevUrl = null; 554 } 555 556 567 public void setCalculateCategories(boolean calculateCategories) { 568 569 m_parameters.setCalculateCategories(calculateCategories); 570 } 571 572 581 public void setCategories(String [] categories) { 582 583 List setCategories = new LinkedList (); 584 if (categories != null) { 585 if (categories.length != 0) { 586 String cat; 588 for (int i = 0; i < categories.length; i++) { 589 cat = categories[i]; 590 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(cat)) { 591 cat = cat.trim().toLowerCase(); 594 setCategories.add(cat); 595 } 596 } 597 } 598 } 599 m_parameters.setCategories(setCategories); 600 resetLastResult(); 601 } 602 603 610 public void setDisplayPages(int value) { 611 612 m_displayPages = value; 613 } 614 615 626 public void setField(String [] fields) { 627 628 List l = new LinkedList (Arrays.asList(fields)); 629 m_parameters.setFields(l); 630 resetLastResult(); 631 } 632 633 640 public void setIndex(String indexName) { 641 642 resetLastResult(); 643 CmsSearchIndex index; 644 if (CmsStringUtil.isNotEmpty(indexName)) { 645 try { 646 index = OpenCms.getSearchManager().getIndex(indexName); 647 if (index == null) { 648 throw new CmsException(Messages.get().container(Messages.ERR_INDEX_NOT_FOUND_1, indexName)); 649 } 650 m_parameters.setSearchIndex(index); 651 } catch (Exception exc) { 652 if (LOG.isDebugEnabled()) { 653 LOG.debug(Messages.get().getBundle().key(Messages.LOG_INDEX_ACCESS_FAILED_1, indexName), exc); 654 } 655 m_lastException = exc; 656 } 657 } 658 } 659 660 665 public void setMatchesPerPage(int matches) { 666 667 m_matchesPerPage = matches; 668 resetLastResult(); 669 } 670 671 677 public void setParameters(CmsSearchParameters parameters) { 678 679 if (parameters != null) { 680 m_parameters = parameters; 681 } 682 } 683 684 692 public void setQuery(String query) { 693 694 try { 695 m_parameters.setQuery(CmsEncoder.decodeParameter(query)); 696 } catch (CmsIllegalArgumentException iae) { 697 m_lastException = iae; 698 } 699 resetLastResult(); 700 } 701 702 707 public void setQueryLength(int length) { 708 709 m_parameters.setQueryLength(length); 710 } 711 712 722 public void setResultRestriction(CmsSearchParameters restriction) { 723 724 resetLastResult(); 725 m_parameterRestriction = restriction; 726 } 727 728 736 public void setSearchPage(int page) { 737 738 m_parameters.setSearchPage(page); 739 resetLastResult(); 740 } 741 742 749 public void setSearchRoot(String searchRoot) { 750 751 setSearchRoots(CmsStringUtil.splitAsArray(searchRoot, ",")); 752 } 753 754 767 public void setSearchRoots(String [] searchRoots) { 768 769 List l = new LinkedList (Arrays.asList(searchRoots)); 770 m_parameters.setRoots(l); 771 resetLastResult(); 772 } 773 774 779 public void setSortOrder(Sort sortOrder) { 780 781 m_parameters.setSort(sortOrder); 782 resetLastResult(); 783 } 784 785 788 private void resetLastResult() { 789 790 m_result = null; 791 m_lastException = null; 792 m_categoriesFound = null; 793 m_parameterRestriction = null; 794 } 795 796 } | Popular Tags |