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;
029
030import java.util.ArrayList;
031import java.util.Collection;
032import java.util.HashSet;
033import java.util.List;
034import java.util.Set;
035
036/**
037 * Configuration that is common for all facets. Used as base class for special facet configurations, e.g. for the field facet configuration.
038 */
039public class CmsSearchConfigurationFacet implements I_CmsSearchConfigurationFacet {
040
041    /** The minimal number of hits required to add an entry to a facet. */
042    protected Integer m_minCount;
043    /** A name used to identify the facet when showing it in the search form. */
044    protected String m_name;
045    /** A label that can be displayed in the form, e.g., at top of the facet. */
046    protected String m_label;
047    /** The sorting of facet entries. */
048    protected List<String> m_preselection;
049    /** A flag, indicating if facet filter queries should be concatenated by AND. */
050    protected boolean m_isAndFacet;
051    /** A flag, indicating if checked entries from other facets should influence the facet or not. */
052    protected boolean m_ignoreFacetFilters;
053    /** Tags of filter-queries that should not be applied to the facet. */
054    protected Set<String> m_ignoreTags;
055    /** Tags of filter-queries that should not be applied to the facet, explicitly configured. */
056    protected Set<String> m_explicitExcludeTags;
057
058    /** The constructor setting all configuration options.
059     * @param minCount The minimal number of hits required to add an entry to a facet.
060     * @param label A label that can be displayed in the form, e.g., at top of the facet.
061     * @param name An optional name for the facet
062     * @param isAndFacet If set to true, the facets filters for results containing all checked entries. Otherwise it filters for results containing at least one checked entry.
063     * @param preselection A list with entries that should be preselected in the facet, when the search page is called the first time.
064     * @param ignoreFiltersFromFacets A flag, indicating if filters from other facets should be ignored or not.
065     * @param excludeTags The tags (keys) of (filter) queries to be not taken into account for the facet. If "ignoreFiltersFromFacets" is true, the according tags for facets and queries will be added.
066     */
067    public CmsSearchConfigurationFacet(
068        final Integer minCount,
069        final String label,
070        final String name,
071        final Boolean isAndFacet,
072        final List<String> preselection,
073        final Boolean ignoreFiltersFromFacets,
074        final Collection<String> excludeTags) {
075
076        m_minCount = minCount;
077        m_label = label == null ? name : label;
078        if (isAndFacet != null) {
079            m_isAndFacet = isAndFacet.booleanValue();
080        }
081        m_name = name;
082        m_preselection = preselection == null ? new ArrayList<String>() : preselection;
083        m_ignoreFacetFilters = ignoreFiltersFromFacets == null ? false : ignoreFiltersFromFacets.booleanValue();
084        m_ignoreTags = new HashSet<>();
085        if (!m_isAndFacet) {
086            m_ignoreTags.add(getName());
087        }
088        if (null != excludeTags) {
089            m_ignoreTags.addAll(excludeTags);
090        }
091    }
092
093    /**
094     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getIgnoreAllFacetFilters()
095     */
096    public boolean getIgnoreAllFacetFilters() {
097
098        return m_ignoreFacetFilters;
099    }
100
101    /**
102     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getIgnoreMaxParamKey()
103     */
104    public String getIgnoreMaxParamKey() {
105
106        return getParamKey() + "_ignoremax";
107    }
108
109    /**
110     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getIgnoreTags()
111     */
112    public String getIgnoreTags() {
113
114        return String.join(",", m_ignoreTags);
115    }
116
117    /**
118     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getIsAndFacet()
119     */
120    public boolean getIsAndFacet() {
121
122        return m_isAndFacet;
123    }
124
125    /**
126     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getLabel()
127     */
128    @Override
129    public String getLabel() {
130
131        return m_label;
132    }
133
134    /**
135     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getMinCount()
136     */
137    @Override
138    public Integer getMinCount() {
139
140        return m_minCount;
141    }
142
143    /**
144     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getName()
145     */
146    @Override
147    public String getName() {
148
149        return m_name;
150    }
151
152    /**
153     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getParamKey()
154     */
155    @Override
156    public String getParamKey() {
157
158        return "facet_" + getName();
159    }
160
161    /**
162     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#getPreSelection()
163     */
164    public List<String> getPreSelection() {
165
166        return m_preselection;
167    }
168
169    /**
170     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacet#propagateAllFacetNames(java.util.Collection)
171     */
172    public void propagateAllFacetNames(Collection<String> names) {
173
174        if (m_ignoreFacetFilters) {
175            m_ignoreTags.addAll(names);
176            m_ignoreTags.add("q"); //tag for the query
177        }
178    }
179
180}