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.List;
033
034/** Configuration for the query facet. */
035public class CmsSearchConfigurationFacetQuery extends CmsSearchConfigurationFacet
036implements I_CmsSearchConfigurationFacetQuery {
037
038    /** Representation of one query facet item. */
039    public static class CmsFacetQueryItem implements I_CmsFacetQueryItem {
040
041        /** The query string for the item. */
042        String m_query;
043        /** The label for the query item. */
044        String m_label;
045
046        /** Constructor for a facet item.
047         * @param query the query string for the item.
048         * @param label the label for the item, defaults to the query string.
049         */
050        public CmsFacetQueryItem(String query, String label) {
051
052            m_query = query;
053            m_label = label == null ? query : label;
054        }
055
056        /**
057         * @see java.lang.Object#equals(java.lang.Object)
058         */
059        @Override
060        public boolean equals(final Object queryItem) {
061
062            if (this.hashCode() != queryItem.hashCode()) {
063                return false;
064            }
065
066            if (queryItem instanceof CmsFacetQueryItem) {
067                CmsFacetQueryItem item = (CmsFacetQueryItem)queryItem;
068                boolean equalQueries = ((null == m_query) && (null == item.getQuery()))
069                    || ((null != item.getQuery()) && m_query.equals(item.getQuery()));
070                boolean equalLabels = false;
071                if (equalQueries) {
072                    equalLabels = ((null == m_label) && (null == item.getLabel()))
073                        || ((null != item.getLabel()) && m_label.equals(item.getLabel()));
074                }
075                return equalQueries && equalLabels;
076            }
077            return false;
078        }
079
080        /**
081         * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacetQuery.I_CmsFacetQueryItem#getLabel()
082         */
083        @Override
084        public String getLabel() {
085
086            return m_label;
087        }
088
089        /**
090         * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacetQuery.I_CmsFacetQueryItem#getQuery()
091         */
092        @Override
093        public String getQuery() {
094
095            return m_query;
096        }
097
098        /**
099         * @see java.lang.Object#hashCode()
100         */
101        @Override
102        public int hashCode() {
103
104            int hashCode = 0;
105            if (null != m_label) {
106                hashCode = m_label.hashCode();
107            }
108            if (null != m_query) {
109                hashCode += m_query.hashCode() / 2;
110            }
111            return hashCode;
112        }
113
114    }
115
116    /** List of queries for the facet. */
117    List<I_CmsSearchConfigurationFacetQuery.I_CmsFacetQueryItem> m_queries;
118
119    /** Constructor for the range facet configuration.
120     * @param queries the queries that can be selected for the facet
121     * @param label the label used to display the facet
122     * @param isAndFacet true if checked facet entries should all be matched, otherwise only one checked entry must match
123     * @param preselection list of entries that should be checked in advance
124     * @param ignoreFiltersFromAllFacets A flag, indicating if filters from all facets should be ignored or not.
125     * @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.
126     */
127    public CmsSearchConfigurationFacetQuery(
128        final List<I_CmsFacetQueryItem> queries,
129        final String label,
130        final Boolean isAndFacet,
131        final List<String> preselection,
132        final Boolean ignoreFiltersFromAllFacets,
133        final Collection<String> excludeTags) {
134
135        super(
136            null,
137            label,
138            I_CmsSearchConfigurationFacetQuery.NAME,
139            isAndFacet,
140            preselection,
141            ignoreFiltersFromAllFacets,
142            excludeTags);
143        m_queries = queries != null ? queries : new ArrayList<I_CmsFacetQueryItem>();
144    }
145
146    /**
147     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationFacetQuery#getQueryList()
148     */
149    @Override
150    public List<I_CmsFacetQueryItem> getQueryList() {
151
152        return m_queries;
153    }
154}