001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp.search.config.parser.simplesearch;
029
030import org.opencms.jsp.search.config.parser.simplesearch.CmsConfigurationBean.CombinationMode;
031
032import java.util.Collections;
033import java.util.List;
034
035/** Wrapper for a combined category and folder restriction. */
036public class CmsCategoryFolderRestrictionBean {
037
038    /** The categories to restrict the search to. */
039    private List<String> m_categories;
040
041    /** The folders to restrict the search to. */
042    private List<String> m_folders;
043
044    /** The category combination mode, i.e., "AND" or "OR". */
045    private CombinationMode m_categoryMode;
046
047    /**
048     * Constructor for the wrapper.
049     * @param categories the categories to filter
050     * @param folders the folders to filter
051     * @param categoryMode the combination mode for categories
052     */
053    public CmsCategoryFolderRestrictionBean(List<String> categories, List<String> folders, CombinationMode categoryMode) {
054
055        m_categories = categories == null ? Collections.<String> emptyList() : categories;
056        m_folders = folders == null ? Collections.<String> emptyList() : folders;
057        m_categoryMode = categoryMode == null ? CombinationMode.OR : categoryMode;
058    }
059
060    /**
061     * Outputs the restriction as Solr filter query.
062     *
063     * @see java.lang.Object#toString()
064     */
065    @Override
066    public String toString() {
067
068        if (m_categories.isEmpty() && m_folders.isEmpty()) {
069            return "";
070        }
071        String result = "(";
072        if (!m_categories.isEmpty()) {
073            result += "category_exact:(";
074            if (m_categories.size() > 1) {
075                result += m_categories.stream().reduce(
076                    (cat1, cat2) -> "\"" + cat1 + "\" " + m_categoryMode + " \"" + cat2 + "\"").get();
077            } else {
078                result += "\"" + m_categories.get(0) + "\"";
079            }
080            result += ")";
081        }
082        if (!m_folders.isEmpty()) {
083            if (!m_categories.isEmpty()) {
084                result += " AND ";
085            }
086            result += "parent-folders:(";
087            if (m_folders.size() > 1) {
088                result += m_folders.stream().reduce((f1, f2) -> "\"" + f1 + "\" OR \"" + f2 + "\"").get();
089            } else {
090                result += "\"" + m_folders.get(0) + "\"";
091            }
092            result += ")";
093        }
094        result += ")";
095        return result;
096    }
097}