1 31 32 package org.opencms.search; 33 34 import org.opencms.main.CmsException; 35 import org.opencms.main.CmsIllegalArgumentException; 36 import org.opencms.main.CmsLog; 37 import org.opencms.main.OpenCms; 38 import org.opencms.search.documents.I_CmsDocumentFactory; 39 import org.opencms.util.CmsStringUtil; 40 41 import java.util.ArrayList ; 42 import java.util.Iterator ; 43 import java.util.LinkedList ; 44 import java.util.List ; 45 46 import org.apache.commons.collections.ListUtils; 47 import org.apache.commons.logging.Log; 48 import org.apache.lucene.search.Sort; 49 import org.apache.lucene.search.SortField; 50 51 66 public class CmsSearchParameters { 67 68 69 public static final Sort SORT_DATE_CREATED = new Sort(new SortField[] { 70 new SortField(I_CmsDocumentFactory.DOC_DATE_CREATED, SortField.STRING, true), 71 SortField.FIELD_SCORE}); 72 73 74 public static final Sort SORT_DATE_LASTMODIFIED = new Sort(new SortField[] { 75 new SortField(I_CmsDocumentFactory.DOC_DATE_LASTMODIFIED, SortField.STRING, true), 76 SortField.FIELD_SCORE}); 77 78 79 public static final Sort SORT_DEFAULT = null; 80 81 82 public static final String [] SORT_NAMES = { 83 "SORT_DEFAULT", 84 "SORT_DATE_CREATED", 85 "SORT_DATE_LASTMODIFIED", 86 "SORT_TITLE"}; 87 88 89 public static final Sort SORT_TITLE = new Sort(new SortField[] { 90 new SortField(I_CmsDocumentFactory.DOC_TITLE_KEY), 91 SortField.FIELD_SCORE}); 92 93 94 private static final Log LOG = CmsLog.getLog(CmsSearchParameters.class); 95 96 97 protected CmsSearchIndex m_index; 98 99 100 protected int m_page; 101 102 103 protected int m_queryLength; 104 105 106 private boolean m_calculateCategories; 107 108 109 private List m_categories; 110 111 112 private List m_fields; 113 114 115 private String m_query; 116 117 118 private List m_roots; 119 120 121 private Sort m_sort; 122 123 131 public CmsSearchParameters() { 132 133 this(""); 134 } 135 136 148 public CmsSearchParameters(String query) { 149 150 this(query, null, null, null, false, null); 151 152 } 153 154 165 public CmsSearchParameters( 166 String query, 167 List fields, 168 List roots, 169 List categories, 170 boolean calculateCategories, 171 Sort sort) { 172 173 super(); 174 m_query = (query == null) ? "" : query; 175 if (fields == null) { 176 fields = new ArrayList (2); 177 fields.add(CmsSearchIndex.DOC_META_FIELDS[0]); 178 fields.add(CmsSearchIndex.DOC_META_FIELDS[1]); 179 } 180 m_fields = fields; 181 if (roots == null) { 182 roots = new ArrayList (); 183 } 184 m_roots = roots; 185 m_categories = (categories == null) ? new LinkedList () : categories; 186 m_calculateCategories = calculateCategories; 187 m_sort = sort; 189 m_page = 1; 190 m_queryLength = -1; 191 } 192 193 198 public boolean getCalculateCategories() { 199 200 return m_calculateCategories; 201 } 202 203 208 public List getCategories() { 209 210 return m_categories; 211 } 212 213 218 public List getFields() { 219 220 return m_fields; 221 } 222 223 228 public String getIndex() { 229 230 return m_index.getName(); 231 } 232 233 238 public String getQuery() { 239 240 return m_query; 241 } 242 243 248 public int getQueryLength() { 249 250 return m_queryLength; 251 } 252 253 260 public List getRoots() { 261 262 return m_roots; 263 } 264 265 270 public String getSearchCategories() { 271 272 return toSeparatedString(getCategories(), ','); 273 } 274 275 282 public boolean getSearchFieldContent() { 283 284 return m_fields.contains(I_CmsDocumentFactory.DOC_CONTENT); 285 } 286 287 294 public boolean getSearchFieldDescription() { 295 296 return m_fields.contains(I_CmsDocumentFactory.DOC_DESCRIPTION); 297 } 298 299 306 public boolean getSearchFieldKeywords() { 307 308 return m_fields.contains(I_CmsDocumentFactory.DOC_KEYWORDS); 309 } 310 311 318 public boolean getSearchFieldMeta() { 319 320 return m_fields.contains(I_CmsDocumentFactory.DOC_META); 321 } 322 323 330 public boolean getSearchFieldTitle() { 331 332 return m_fields.contains(I_CmsDocumentFactory.DOC_TITLE_INDEXED); 333 } 334 335 341 public CmsSearchIndex getSearchIndex() { 342 343 return m_index; 344 } 345 346 351 public int getSearchPage() { 352 353 return m_page; 354 } 355 356 366 367 public String getSearchRoots() { 368 369 return toSeparatedString(m_roots, ','); 370 } 371 372 377 public Sort getSort() { 378 379 return m_sort; 380 } 381 382 389 public String getSortName() { 390 391 if (m_sort == SORT_DATE_CREATED) { 392 return SORT_NAMES[1]; 393 } 394 if (m_sort == SORT_DATE_LASTMODIFIED) { 395 return SORT_NAMES[2]; 396 } 397 if (m_sort == SORT_TITLE) { 398 return SORT_NAMES[3]; 399 } 400 return SORT_NAMES[0]; 401 } 402 403 408 public boolean isCalculateCategories() { 409 410 return m_calculateCategories; 411 } 412 413 433 public CmsSearchParameters restrict(CmsSearchParameters restriction) { 434 435 StringBuffer query = new StringBuffer (256); 437 if (getQuery() != null) { 438 boolean closure = !getQuery().startsWith("+("); 440 if (closure) { 441 query.append("+("); 442 } 443 query.append(getQuery()); 444 if (closure) { 445 query.append(")"); 446 } 447 } 448 if (restriction.getQuery() != null) { 449 if (query.indexOf(restriction.getQuery()) < 0) { 452 query.append(" +("); 453 query.append(restriction.getQuery()); 454 query.append(")"); 455 } 456 } 457 458 List fields = null; 460 if ((m_fields != null) && (m_fields.size() > 0)) { 461 if ((restriction.getFields() != null) && (restriction.getFields().size() > 0)) { 462 fields = ListUtils.intersection(m_fields, restriction.getFields()); 463 } else { 464 fields = m_fields; 465 } 466 } else { 467 fields = restriction.getFields(); 468 } 469 470 List roots = null; 472 if ((m_roots != null) && (m_roots.size() > 0)) { 473 if ((restriction.getRoots() != null) && (restriction.getRoots().size() > 0)) { 474 roots = ListUtils.intersection(m_roots, restriction.getRoots()); 475 } else { 478 roots = m_roots; 479 } 480 } else { 481 roots = restriction.getRoots(); 482 } 483 484 List categories = null; 486 if ((m_categories != null) && (m_categories.size() > 0)) { 487 if ((restriction.getCategories() != null) && (restriction.getCategories().size() > 0)) { 488 categories = ListUtils.intersection(m_categories, restriction.getCategories()); 489 } else { 490 categories = m_categories; 491 } 492 } else { 493 categories = restriction.getCategories(); 494 } 495 496 CmsSearchParameters result = new CmsSearchParameters( 498 query.toString(), 499 fields, 500 roots, 501 categories, 502 m_calculateCategories, 503 m_sort); 504 result.setIndex(getIndex()); 505 return result; 506 } 507 508 513 public void setCalculateCategories(boolean flag) { 514 515 m_calculateCategories = flag; 516 } 517 518 523 public void setCategories(List categories) { 524 525 m_categories = categories; 526 } 527 528 533 public void setFields(List fields) { 534 535 m_fields = fields; 536 } 537 538 544 public void setIndex(String indexName) { 545 546 CmsSearchIndex index; 547 if (CmsStringUtil.isNotEmpty(indexName)) { 548 try { 549 index = OpenCms.getSearchManager().getIndex(indexName); 550 if (index == null) { 551 throw new CmsException(Messages.get().container(Messages.ERR_INDEX_NOT_FOUND_1, indexName)); 552 } 553 setSearchIndex(index); 554 } catch (Exception exc) { 555 if (LOG.isErrorEnabled()) { 556 LOG.error(Messages.get().getBundle().key(Messages.LOG_INDEX_ACCESS_FAILED_1, indexName), exc); 557 } 558 } 559 } 560 } 561 562 575 public void setQuery(String query) { 576 577 579 if (query.trim().length() < getQueryLength()) { 581 throw new CmsIllegalArgumentException(Messages.get().container( 582 Messages.ERR_QUERY_TOO_SHORT_1, 583 new Integer (getQueryLength()))); 584 } 585 m_query = query; 586 } 587 588 593 public void setQueryLength(int length) { 594 595 m_queryLength = length; 596 } 597 598 603 public void setRoots(List roots) { 604 605 m_roots = roots; 606 } 607 608 613 public void setSearchCategories(String categories) { 614 615 setCategories(CmsStringUtil.splitAsList(categories, ',')); 616 } 617 618 626 public void setSearchFieldContent(boolean flag) { 627 628 if (flag) { 629 if (!m_fields.contains(I_CmsDocumentFactory.DOC_CONTENT)) { 630 m_fields.add(I_CmsDocumentFactory.DOC_CONTENT); 631 } 632 } else { 633 m_fields.remove(I_CmsDocumentFactory.DOC_CONTENT); 634 } 635 } 636 637 645 public void setSearchFieldDescription(boolean flag) { 646 647 if (flag) { 648 if (!m_fields.contains(I_CmsDocumentFactory.DOC_DESCRIPTION)) { 649 m_fields.add(I_CmsDocumentFactory.DOC_DESCRIPTION); 650 } 651 } else { 652 m_fields.remove(I_CmsDocumentFactory.DOC_DESCRIPTION); 653 } 654 } 655 656 664 public void setSearchFieldKeywords(boolean flag) { 665 666 if (flag) { 667 if (!m_fields.contains(I_CmsDocumentFactory.DOC_KEYWORDS)) { 668 m_fields.add(I_CmsDocumentFactory.DOC_KEYWORDS); 669 } 670 } else { 671 m_fields.remove(I_CmsDocumentFactory.DOC_KEYWORDS); 672 } 673 } 674 675 683 public void setSearchFieldMeta(boolean flag) { 684 685 if (flag) { 686 if (!m_fields.contains(I_CmsDocumentFactory.DOC_META)) { 687 m_fields.add(I_CmsDocumentFactory.DOC_META); 688 } 689 } else { 690 m_fields.remove(I_CmsDocumentFactory.DOC_META); 691 } 692 } 693 694 702 public void setSearchFieldTitle(boolean flag) { 703 704 if (flag) { 705 if (!m_fields.contains(I_CmsDocumentFactory.DOC_TITLE_INDEXED)) { 706 m_fields.add(I_CmsDocumentFactory.DOC_TITLE_INDEXED); 707 } 708 } else { 709 m_fields.remove(I_CmsDocumentFactory.DOC_TITLE_INDEXED); 710 } 711 } 712 713 720 public void setSearchIndex(CmsSearchIndex index) throws CmsIllegalArgumentException { 721 722 if (index == null) { 723 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INDEX_NULL_0)); 724 } 725 m_index = index; 726 } 727 728 733 public void setSearchPage(int page) { 734 735 m_page = page; 736 } 737 738 743 public void setSearchRoots(String rootNameList) { 744 745 m_roots = CmsStringUtil.splitAsList(rootNameList, ','); 746 } 747 748 753 public void setSort(Sort sortOrder) { 754 755 m_sort = sortOrder; 756 } 757 758 768 public void setSortName(String sortName) { 769 770 if (sortName.equals(SORT_NAMES[1])) { 771 m_sort = SORT_DATE_CREATED; 772 } else if (sortName.equals(SORT_NAMES[2])) { 773 m_sort = SORT_DATE_LASTMODIFIED; 774 } else if (sortName.equals(SORT_NAMES[3])) { 775 m_sort = SORT_TITLE; 776 } else { 777 m_sort = SORT_DEFAULT; 778 } 779 } 780 781 784 public String toString() { 785 786 StringBuffer result = new StringBuffer (); 787 result.append("query:["); 788 result.append(m_query); 789 result.append("] "); 790 if ((m_fields != null) && (m_fields.size() > 0)) { 791 result.append("fields:["); 792 for (int i = 0; i < m_fields.size(); i++) { 793 result.append(m_fields.get(i)); 794 if (i + 1 < m_fields.size()) { 795 result.append(", "); 796 } 797 } 798 result.append("] "); 799 } 800 if ((m_roots != null) && (m_roots.size() > 0)) { 801 result.append("roots:["); 802 for (int i = 0; i < m_roots.size(); i++) { 803 result.append(m_roots.get(i)); 804 if (i + 1 < m_roots.size()) { 805 result.append(", "); 806 } 807 } 808 result.append("] "); 809 } 810 if ((m_categories != null) && (m_categories.size() > 0)) { 811 result.append("categories:["); 812 for (int i = 0; i < m_categories.size(); i++) { 813 result.append(m_categories.get(i)); 814 if (i + 1 < m_categories.size()) { 815 result.append(", "); 816 } 817 } 818 result.append("] "); 819 } 820 if (m_calculateCategories) { 821 result.append("calculate-categories "); 822 } 823 result.append("sort:["); 824 if (m_sort == CmsSearchParameters.SORT_DEFAULT) { 825 result.append("default"); 826 } else if (m_sort == CmsSearchParameters.SORT_TITLE) { 827 result.append("title"); 828 } else if (m_sort == CmsSearchParameters.SORT_DATE_CREATED) { 829 result.append("date-created"); 830 } else if (m_sort == CmsSearchParameters.SORT_DATE_LASTMODIFIED) { 831 result.append("date-lastmodified"); 832 } else { 833 result.append("unknown"); 834 } 835 result.append("]"); 836 return result.toString(); 837 } 838 839 private String toSeparatedString(List stringList, char c) { 840 841 StringBuffer result = new StringBuffer (); 842 Iterator it = stringList.iterator(); 843 while (it.hasNext()) { 844 result.append(it.next()); 845 if (it.hasNext()) { 846 result.append(c); 847 } 848 } 849 return result.toString(); 850 } 851 } | Popular Tags |