001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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_CmsSearchConfigurationDidYouMean;
032import org.opencms.jsp.search.state.CmsSearchStateDidYouMean;
033import org.opencms.jsp.search.state.I_CmsSearchStateDidYouMean;
034import org.opencms.search.solr.CmsSolrQuery;
035
036import java.util.Map;
037
038import org.apache.solr.client.solrj.util.ClientUtils;
039
040/** Controller for the "Did you mean ...?" feature. */
041public class CmsSearchControllerDidYouMean implements I_CmsSearchControllerDidYouMean {
042
043    /** Stores the configuration. */
044    private final I_CmsSearchConfigurationDidYouMean m_config;
045    /** Stores the configuration. */
046    private final I_CmsSearchStateDidYouMean m_state;
047
048    /** Constructor, taking the configuration.
049     * @param config the configuration.
050     */
051    public CmsSearchControllerDidYouMean(I_CmsSearchConfigurationDidYouMean config) {
052
053        m_config = config;
054        m_state = new CmsSearchStateDidYouMean();
055    }
056
057    /**
058     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
059     */
060    public void addParametersForCurrentState(Map<String, String[]> parameters) {
061
062        // nothing to do
063
064    }
065
066    /**
067     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery, CmsObject)
068     */
069    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
070
071        query.set("spellcheck", "true");
072        String queryString = m_state.getQuery();
073        if (m_config.getEscapeQueryChars()) {
074            queryString = ClientUtils.escapeQueryChars(queryString);
075        }
076        query.set("spellcheck.q", queryString);
077        if (m_config.getCollate()) {
078            query.set("spellcheck.collate", "true");
079        } else {
080            query.set("spellcheck.collate", "false");
081        }
082        query.set("spellcheck.extendedResults", "true");
083        query.set("spellcheck.count", Integer.valueOf(m_config.getCount()).toString());
084    }
085
086    /**
087     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerDidYouMean#getConfig()
088     */
089    public I_CmsSearchConfigurationDidYouMean getConfig() {
090
091        return m_config;
092    }
093
094    /**
095     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerDidYouMean#getState()
096     */
097    public I_CmsSearchStateDidYouMean getState() {
098
099        return m_state;
100    }
101
102    /**
103     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
104     */
105    public void updateForQueryChange() {
106
107        // nothing to do
108
109    }
110
111    /**
112     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
113     */
114    public void updateFromRequestParameters(Map<String, String[]> parameters, boolean isReloaded) {
115
116        if (parameters.containsKey(m_config.getQueryParam())) {
117            final String[] queryStrings = parameters.get(m_config.getQueryParam());
118            if (queryStrings.length > 0) {
119                String queryString = queryStrings[0];
120                m_state.setQuery(queryString);
121                return;
122            }
123        }
124        m_state.setQuery("");
125    }
126
127}