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