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