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_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
038/** Controller for the "Did you mean ...?" feature. */
039public class CmsSearchControllerDidYouMean implements I_CmsSearchControllerDidYouMean {
040
041    /** Stores the configuration. */
042    private final I_CmsSearchConfigurationDidYouMean m_config;
043    /** Stores the configuration. */
044    private final I_CmsSearchStateDidYouMean m_state;
045
046    /** Constructor, taking the configuration.
047     * @param config the configuration.
048     */
049    public CmsSearchControllerDidYouMean(I_CmsSearchConfigurationDidYouMean config) {
050
051        m_config = config;
052        m_state = new CmsSearchStateDidYouMean();
053    }
054
055    /**
056     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
057     */
058    public void addParametersForCurrentState(Map<String, String[]> parameters) {
059
060        // nothing to do
061
062    }
063
064    /**
065     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery, CmsObject)
066     */
067    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
068
069        query.set("spellcheck", "true");
070        String queryString = m_state.getQuery();
071        query.set("spellcheck.q", queryString);
072        if (m_config.getCollate()) {
073            query.set("spellcheck.collate", "true");
074        } else {
075            query.set("spellcheck.collate", "false");
076        }
077        query.set("spellcheck.extendedResults", "true");
078        query.set("spellcheck.count", Integer.valueOf(m_config.getCount()).toString());
079    }
080
081    /**
082     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerDidYouMean#getConfig()
083     */
084    public I_CmsSearchConfigurationDidYouMean getConfig() {
085
086        return m_config;
087    }
088
089    /**
090     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerDidYouMean#getState()
091     */
092    public I_CmsSearchStateDidYouMean getState() {
093
094        return m_state;
095    }
096
097    /**
098     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
099     */
100    public void updateForQueryChange() {
101
102        // nothing to do
103
104    }
105
106    /**
107     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
108     */
109    public void updateFromRequestParameters(Map<String, String[]> parameters, boolean isReloaded) {
110
111        if (parameters.containsKey(m_config.getQueryParam())) {
112            final String[] queryStrings = parameters.get(m_config.getQueryParam());
113            if (queryStrings.length > 0) {
114                m_state.setQuery(queryStrings[0]);
115                return;
116            }
117        }
118        m_state.setQuery("");
119    }
120
121}