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.controller;
029
030import org.opencms.file.CmsObject;
031import org.opencms.jsp.search.config.I_CmsSearchConfigurationFacetField;
032import org.opencms.search.solr.CmsSolrQuery;
033
034import java.util.Collection;
035import java.util.Iterator;
036import java.util.LinkedHashMap;
037import java.util.Map;
038
039/** Search controller as aggregation of all single field facet controllers. */
040public class CmsSearchControllerFacetsField implements I_CmsSearchControllerFacetsField {
041
042    /** Controllers of the single field facets with the facet's name as key. */
043    Map<String, I_CmsSearchControllerFacetField> m_fieldFacets;
044
045    /** Constructor taking the list of field facet controllers that are aggregated.
046     * @param configs The controllers for single field facets.
047     */
048    public CmsSearchControllerFacetsField(final Map<String, I_CmsSearchConfigurationFacetField> configs) {
049
050        m_fieldFacets = new LinkedHashMap<String, I_CmsSearchControllerFacetField>();
051        for (final String name : configs.keySet()) {
052            m_fieldFacets.put(name, new CmsSearchControllerFacetField(configs.get(name)));
053        }
054    }
055
056    /**
057     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
058     */
059    @Override
060    public void addParametersForCurrentState(final Map<String, String[]> parameters) {
061
062        for (final I_CmsSearchControllerFacetField controller : m_fieldFacets.values()) {
063            controller.addParametersForCurrentState(parameters);
064        }
065    }
066
067    /**
068     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery, CmsObject)
069     */
070    @Override
071    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
072
073        if (!m_fieldFacets.isEmpty()) {
074            query.set("facet", "true");
075            final Iterator<I_CmsSearchControllerFacetField> it = m_fieldFacets.values().iterator();
076            it.next().addQueryParts(query, cms);
077            while (it.hasNext()) {
078                it.next().addQueryParts(query, cms);
079            }
080        }
081    }
082
083    /**
084     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerFacetsField#getFieldFacetController()
085     */
086    @Override
087    public Map<String, I_CmsSearchControllerFacetField> getFieldFacetController() {
088
089        return m_fieldFacets;
090    }
091
092    /**
093     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerFacetsField#getFieldFacetControllers()
094     */
095    @Override
096    public Collection<I_CmsSearchControllerFacetField> getFieldFacetControllers() {
097
098        return m_fieldFacets.values();
099    }
100
101    /**
102     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
103     */
104    @Override
105    public void updateForQueryChange() {
106
107        for (final I_CmsSearchControllerFacetField controller : m_fieldFacets.values()) {
108            controller.updateForQueryChange();
109        }
110
111    }
112
113    /**
114     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
115     */
116    @Override
117    public void updateFromRequestParameters(final Map<String, String[]> parameters, boolean isReloaded) {
118
119        for (final I_CmsSearchControllerFacetField controller : m_fieldFacets.values()) {
120            controller.updateFromRequestParameters(parameters, isReloaded);
121        }
122
123    }
124
125}