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_CmsSearchConfiguration;
032import org.opencms.search.solr.CmsSolrQuery;
033
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Iterator;
037import java.util.Map;
038
039/** The main controller that allows to access all single sub-controllers. */
040public class CmsSearchController implements I_CmsSearchControllerMain {
041
042    /** List of all controllers. */
043    Collection<I_CmsSearchController> m_controllers;
044    /** The controller for the common options. */
045    I_CmsSearchControllerCommon m_common;
046    /** The controller for the sort options. */
047    I_CmsSearchControllerSorting m_sorting;
048    /** The controller for the pagination options. */
049    I_CmsSearchControllerPagination m_pagination;
050    /** The controller for the field facets. */
051    I_CmsSearchControllerFacetsField m_fieldFacets;
052    /** The controller for the field facets. */
053    I_CmsSearchControllerFacetsRange m_rangeFacets;
054    /** The controller for the query facet. */
055    I_CmsSearchControllerFacetQuery m_queryFacet;
056    /** The controller for the highlighting. */
057    I_CmsSearchControllerHighlighting m_highlighting;
058    /** The controller for the "Did you mean ...?" feature. */
059    I_CmsSearchControllerDidYouMean m_didyoumean;
060
061    /** Constructor that sets up the controller with a given configuration.
062     * @param config The search configuration handled by the controller.
063     */
064    public CmsSearchController(final I_CmsSearchConfiguration config) {
065
066        m_controllers = new ArrayList<I_CmsSearchController>();
067
068        // create the separate controllers and add them to the list of
069        // controllers
070        m_common = new CmsSearchControllerCommon(config.getGeneralConfig());
071        m_controllers.add(m_common);
072
073        if (config.getPaginationConfig() != null) {
074            m_pagination = new CmsSearchControllerPagination(config.getPaginationConfig());
075            m_controllers.add(m_pagination);
076        }
077
078        if (config.getSortConfig() != null) {
079            m_sorting = new CmsSearchControllerSorting(config.getSortConfig());
080            m_controllers.add(m_sorting);
081        }
082
083        m_fieldFacets = new CmsSearchControllerFacetsField(config.getFieldFacetConfigs());
084        m_controllers.add(m_fieldFacets);
085
086        m_rangeFacets = new CmsSearchControllerFacetsRange(config.getRangeFacetConfigs());
087        m_controllers.add(m_rangeFacets);
088
089        if (config.getHighlighterConfig() != null) {
090            m_highlighting = new CmsSearchControllerHighlighting(config.getHighlighterConfig());
091            m_controllers.add(m_highlighting);
092        }
093        if (config.getDidYouMeanConfig() != null) {
094            m_didyoumean = new CmsSearchControllerDidYouMean(config.getDidYouMeanConfig());
095            m_controllers.add(m_didyoumean);
096        }
097        if (config.getQueryFacetConfig() != null) {
098            m_queryFacet = new CmsSearchControllerFacetQuery(config.getQueryFacetConfig());
099            m_controllers.add(m_queryFacet);
100        }
101    }
102
103    /**
104     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
105     */
106    @Override
107    public void addParametersForCurrentState(final Map<String, String[]> parameters) {
108
109        for (final I_CmsSearchController controller : m_controllers) {
110            controller.addParametersForCurrentState(parameters);
111        }
112
113    }
114
115    /**
116     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery, CmsObject)
117     */
118    @Override
119    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
120
121        final Iterator<I_CmsSearchController> it = m_controllers.iterator();
122        it.next().addQueryParts(query, cms);
123        while (it.hasNext()) {
124            it.next().addQueryParts(query, cms);
125        }
126        // fix for highlighting bug
127        if ((getHighlighting() != null) && !((query.getParams("df") != null) || (query.getParams("type") != null))) {
128            String df = getHighlighting().getConfig().getHightlightField().trim();
129            int index = df.indexOf(' ');
130            query.add("df", (index > 0 ? df.substring(0, index) : df));
131        }
132    }
133
134    /**
135     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getCommon()
136     */
137    @Override
138    public I_CmsSearchControllerCommon getCommon() {
139
140        return m_common;
141    }
142
143    /**
144     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getDidYouMean()
145     */
146    public I_CmsSearchControllerDidYouMean getDidYouMean() {
147
148        return m_didyoumean;
149    }
150
151    /**
152     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getFieldFacets()
153     */
154    @Override
155    public I_CmsSearchControllerFacetsField getFieldFacets() {
156
157        return m_fieldFacets;
158    }
159
160    /**
161     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getHighlighting()
162     */
163    @Override
164    public I_CmsSearchControllerHighlighting getHighlighting() {
165
166        return m_highlighting;
167    }
168
169    /**
170     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getPagination()
171     */
172    @Override
173    public I_CmsSearchControllerPagination getPagination() {
174
175        return m_pagination;
176    }
177
178    /**
179     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getQueryFacet()
180     */
181    @Override
182    public I_CmsSearchControllerFacetQuery getQueryFacet() {
183
184        return m_queryFacet;
185    }
186
187    /**
188     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getRangeFacets()
189     */
190    public I_CmsSearchControllerFacetsRange getRangeFacets() {
191
192        return m_rangeFacets;
193    }
194
195    /**
196     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerMain#getSorting()
197     */
198    @Override
199    public I_CmsSearchControllerSorting getSorting() {
200
201        return m_sorting;
202    }
203
204    /**
205     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
206     */
207    @Override
208    public void updateForQueryChange() {
209
210        for (final I_CmsSearchController controller : m_controllers) {
211            controller.updateForQueryChange();
212        }
213
214    }
215
216    /**
217     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
218     */
219    @Override
220    public void updateFromRequestParameters(final Map<String, String[]> parameters, final boolean unused) {
221
222        // Check if query has changed
223        final String lastQueryParam = m_common.getConfig().getLastQueryParam();
224        final String queryParam = m_common.getConfig().getQueryParam();
225        final String firstCallParam = m_common.getConfig().getReloadedParam();
226        if (!m_common.getConfig().getIgnoreQueryParam()
227            && parameters.containsKey(lastQueryParam)
228            && parameters.containsKey(queryParam)
229            && (parameters.get(lastQueryParam).length > 0)
230            && (parameters.get(queryParam).length > 0)
231            && !parameters.get(queryParam)[0].equals(parameters.get(lastQueryParam)[0])) {
232            updateForQueryChange();
233        }
234        boolean isReloaded = parameters.containsKey(firstCallParam);
235        for (final I_CmsSearchController controller : m_controllers) {
236            controller.updateFromRequestParameters(parameters, isReloaded);
237        }
238
239    }
240
241}