⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssearchparameters.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * <code>{@link #isCalculateCategories()}</code>
     * and <code>{@link #getSort()}</code> of this parameters are used for the restricted parameters.<p>
     * 
     * @param restriction the parameters to restrict this parameters with
     * @return the restricted parameters
     */
    public CmsSearchParameters restrict(CmsSearchParameters restriction) {

        // append queries
        StringBuffer query = new StringBuffer(256);
        if (getQuery() != null) {
            // don't blow up unneccessary closures (if CmsSearch is reused and restricted several times)
            boolean closure = !getQuery().startsWith("+(");
            if (closure) {
                query.append("+(");
            }
            query.append(getQuery());
            if (closure) {
                query.append(")");
            }
        }
        if (restriction.getQuery() != null) {
            // don't let lucene max terms be exceeded in case someone reuses a CmsSearch and continuously restricts 
            // query with the same restrictions...
            if (query.indexOf(restriction.getQuery()) < 0) {
                query.append(" +(");
                query.append(restriction.getQuery());
                query.append(")");
            }
        }

        // restrict fields
        List fields = null;
        if ((m_fields != null) && (m_fields.size() > 0)) {
            if ((restriction.getFields() != null) && (restriction.getFields().size() > 0)) {
                fields = ListUtils.intersection(m_fields, restriction.getFields());
            } else {
                fields = m_fields;
            }
        } else {
            fields = restriction.getFields();
        }

        // restrict roots
        List roots = null;
        if ((m_roots != null) && (m_roots.size() > 0)) {
            if ((restriction.getRoots() != null) && (restriction.getRoots().size() > 0)) {
                roots = ListUtils.intersection(m_roots, restriction.getRoots());
                // TODO: This only works if there are equal paths in both parameter sets - for two distinct sets 
                //       all root restrictions are dropped with an empty list. 
            } else {
                roots = m_roots;
            }
        } else {
            roots = restriction.getRoots();
        }

        // restrict categories
        List categories = null;
        if ((m_categories != null) && (m_categories.size() > 0)) {
            if ((restriction.getCategories() != null) && (restriction.getCategories().size() > 0)) {
                categories = ListUtils.intersection(m_categories, restriction.getCategories());
            } else {
                categories = m_categories;
            }
        } else {
            categories = restriction.getCategories();
        }

        // create the new search parameters 
        CmsSearchParameters result = new CmsSearchParameters(
            query.toString(),
            fields,
            roots,
            categories,
            m_calculateCategories,
            m_sort);
        result.setIndex(getIndex());
        return result;
    }

    /**
     * Set wether category counts shall be calculated for the corresponding search results or not.<p> 
     * 
     * @param flag true if category counts shall be calculated for the corresponding search results or false if not
     */
    public void setCalculateCategories(boolean flag) {

        m_calculateCategories = flag;
    }

    /**
     * Set the list of categories (strings) to this parameters. <p> 
     * 
     * @param categories the list of categories (strings) of this parameters
     */
    public void setCategories(List categories) {

        m_categories = categories;
    }

    /**
     * Sets the list of strings of names of fields to search in. <p>
     * 
     * @param fields the list of strings of names of fields to search in to set
     */
    public void setFields(List fields) {

        m_fields = fields;
    }

    /**
     * Set the name of the index to search.<p>
     * 
     * 
     * @param indexName the name of the index
     */
    public void setIndex(String indexName) {

        CmsSearchIndex index;
        if (CmsStringUtil.isNotEmpty(indexName)) {
            try {
                index = OpenCms.getSearchManager().getIndex(indexName);
                if (index == null) {
                    throw new CmsException(Messages.get().container(Messages.ERR_INDEX_NOT_FOUND_1, indexName));
                }
                setSearchIndex(index);
            } catch (Exception exc) {
                if (LOG.isErrorEnabled()) {
                    LOG.error(Messages.get().getBundle().key(Messages.LOG_INDEX_ACCESS_FAILED_1, indexName), exc);
                }
            }
        }
    }

    /**
     * Sets the query to search for. <p> 
     * 
     * The decoding here is tailored for query strings that are 
     * additionally manually utf-8 encoded at client side (javascript) to get around an 
     * issue with special chars in applications that use non- utf-8 encoding 
     * (e.g. ISO-8859-1) OpenCms applications. It is not recommended to use this with 
     * frontends that don't encode manually as characters like sole "%" (without number suffix) 
     * will cause an Exception.<p> 
     * 
     * @param query the querye to search for to set
     *
     */
    public void setQuery(String query) {

        // query = CmsEncoder.decode(query);

        // for widget use the exception is thrown here to enforce the errmsg next to widget
        if (query.trim().length() < getQueryLength()) {
            throw new CmsIllegalArgumentException(Messages.get().container(
                Messages.ERR_QUERY_TOO_SHORT_1,
                new Integer(getQueryLength())));
        }
        m_query = query;
    }

    /**
     * Sets the minimum length of the search query.<p>
     * 
     * @param length the minimum search query length
     */
    public void setQueryLength(int length) {

        m_queryLength = length;
    }

    /**
     * Sets the list of strings of roots to search under for the search.<p>
     * 
     * @param roots  the list of strings of roots to search under for the search to set
     */
    public void setRoots(List roots) {

        m_roots = roots;
    }

    /**
     * Set the comma separated search root names to  restrict search to.<p>
     * 
     * @param categories the comma separated category names to  restrict search to
     */
    public void setSearchCategories(String categories) {

        setCategories(CmsStringUtil.splitAsList(categories, ','));
    }

    /**
     * Set wether the content field should be searched.<p>
     * 
     * This method is a widget support for <code>{@link org.opencms.widgets.CmsCheckboxWidget}</code>.<p>
     * 
     * @param flag true if the field <code>{@link org.opencms.search.documents.I_CmsDocumentFactory#DOC_CONTENT}</code> 
     *        shall be searched - false else
     */
    public void setSearchFieldContent(boolean flag) {

        if (flag) {
            if (!m_fields.contains(I_CmsDocumentFactory.DOC_CONTENT)) {
                m_fields.add(I_CmsDocumentFactory.DOC_CONTENT);
            }
        } else {
            m_fields.remove(I_CmsDocumentFactory.DOC_CONTENT);
        }
    }

    /**
     * Set wether the description field should be searched.<p>
     * 
     * This method is a widget support for <code>{@link org.opencms.widgets.CmsCheckboxWidget}</code>.<p>
     * 
     * @param flag true if the field <code>{@link org.opencms.search.documents.I_CmsDocumentFactory#DOC_DESCRIPTION}</code> 
     *        shall be searched - false else
     */
    public void setSearchFieldDescription(boolean flag) {

        if (flag) {
            if (!m_fields.contains(I_CmsDocumentFactory.DOC_DESCRIPTION)) {
                m_fields.add(I_CmsDocumentFactory.DOC_DESCRIPTION);
            }
        } else {
            m_fields.remove(I_CmsDocumentFactory.DOC_DESCRIPTION);
        }
    }

    /**
     * Set wether the title field should be searched.<p>
     * 
     * This method is a widget support for <code>{@link org.opencms.widgets.CmsCheckboxWidget}</code>.<p>
     * 
     * @param flag true if the field <code>{@link org.opencms.search.documents.I_CmsDocumentFactory#DOC_KEYWORDS}</code> 
     *        shall be searched - false else
     */
    public void setSearchFieldKeywords(boolean flag) {

        if (flag) {
            if (!m_fields.contains(I_CmsDocumentFactory.DOC_KEYWORDS)) {
                m_fields.add(I_CmsDocumentFactory.DOC_KEYWORDS);
            }
        } else {
            m_fields.remove(I_CmsDocumentFactory.DOC_KEYWORDS);
        }
    }

    /**
     * Set wether the meta field should be searched.<p>
     * 
     * This method is a widget support for <code>{@link org.opencms.widgets.CmsCheckboxWidget}</code>.<p>
     * 
     * @param flag true if the field <code>{@link org.opencms.search.documents.I_CmsDocumentFactory#DOC_META}</code> 
     *        shall be searched - false else
     */
    public void setSearchFieldMeta(boolean flag) {

        if (flag) {
            if (!m_fields.contains(I_CmsDocumentFactory.DOC_META)) {
                m_fields.add(I_CmsDocumentFactory.DOC_META);
            }
        } else {
            m_fields.remove(I_CmsDocumentFactory.DOC_META);
        }
    }

    /**
     * Set wether the title field should be searched.<p>
     * 
     * This method is a widget support for <code>{@link org.opencms.widgets.CmsCheckboxWidget}</code>.<p>
     * 
     * @param flag true if the field <code>{@link org.opencms.search.documents.I_CmsDocumentFactory#DOC_TITLE_INDEXED}</code> 
     *        shall be searched - false else
     */
    public void setSearchFieldTitle(boolean flag) {

        if (flag) {
            if (!m_fields.contains(I_CmsDocumentFactory.DOC_TITLE_INDEXED)) {
                m_fields.add(I_CmsDocumentFactory.DOC_TITLE_INDEXED);
            }
        } else {
            m_fields.remove(I_CmsDocumentFactory.DOC_TITLE_INDEXED);
        }
    }

    /**
     * Sets the search index to use for the search. <p>
     * 
     * @param index the search index to use for the search to set.
     * 
     * @throws CmsIllegalArgumentException if null is given as argument 
     */
    public void setSearchIndex(CmsSearchIndex index) throws CmsIllegalArgumentException {

        if (index == null) {
            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INDEX_NULL_0));
        }
        m_index = index;
    }

    /**
     * Set the search page to display. <p>
     * 
     * @param page the search page to display
     */
    public void setSearchPage(int page) {

        m_page = page;
    }

    /**
     * Set the comma separated search root names to  restrict search to.<p>
     * 
     * @param rootNameList the comma separated search root names to  restrict search to
     */
    public void setSearchRoots(String rootNameList) {

        m_roots = CmsStringUtil.splitAsList(rootNameList, ',');
    }

    /**
     * Set the instance that defines the sort order for search results. 
     * 
     * @param sortOrder the instance that defines the sort order for search results to set
     */
    public void setSort(Sort sortOrder) {

        m_sort = sortOrder;
    }

    /** 
     * Sets the internal member of type <code>{@link Sort}</code> according to 
     * the given sort name. <p>
     * 
     * For a list of valid sort names, please see <code>{@link #SORT_NAMES}</code>.<p>
     * 
     * @param sortName the name of the sort to use
     * 
     * @see #SORT_NAMES
     */
    public void setSortName(String sortName) {

        if (sortName.equals(SORT_NAMES[1])) {
            m_sort = SORT_DATE_CREATED;
        } else if (sortName.equals(SORT_NAMES[2])) {
            m_sort = SORT_DATE_LASTMODIFIED;
        } else if (sortName.equals(SORT_NAMES[3])) {
            m_sort = SORT_TITLE;
        } else {
            m_sort = SORT_DEFAULT;
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {

        StringBuffer result = new StringBuffer();
        result.append("query:[");
        result.append(m_query);
        result.append("] ");
        if ((m_fields != null) && (m_fields.size() > 0)) {
            result.append("fields:[");
            for (int i = 0; i < m_fields.size(); i++) {
                result.append(m_fields.get(i));
                if (i + 1 < m_fields.size()) {
                    result.append(", ");
                }
            }
            result.append("] ");
        }
        if ((m_roots != null) && (m_roots.size() > 0)) {
            result.append("roots:[");
            for (int i = 0; i < m_roots.size(); i++) {
                result.append(m_roots.get(i));
                if (i + 1 < m_roots.size()) {
                    result.append(", ");
                }
            }
            result.append("] ");
        }
        if ((m_categories != null) && (m_categories.size() > 0)) {
            result.append("categories:[");
            for (int i = 0; i < m_categories.size(); i++) {
                result.append(m_categories.get(i));
                if (i + 1 < m_categories.size()) {
                    result.append(", ");
                }
            }
            result.append("] ");
        }
        if (m_calculateCategories) {
            result.append("calculate-categories ");
        }
        result.append("sort:[");
        if (m_sort == CmsSearchParameters.SORT_DEFAULT) {
            result.append("default");
        } else if (m_sort == CmsSearchParameters.SORT_TITLE) {
            result.append("title");
        } else if (m_sort == CmsSearchParameters.SORT_DATE_CREATED) {
            result.append("date-created");
        } else if (m_sort == CmsSearchParameters.SORT_DATE_LASTMODIFIED) {
            result.append("date-lastmodified");
        } else {
            result.append("unknown");
        }
        result.append("]");
        return result.toString();
    }

    private String toSeparatedString(List stringList, char c) {

        StringBuffer result = new StringBuffer();
        Iterator it = stringList.iterator();
        while (it.hasNext()) {
            result.append(it.next());
            if (it.hasNext()) {
                result.append(c);
            }
        }
        return result.toString();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -