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_CmsSearchConfigurationSortOption;
032import org.opencms.jsp.search.config.I_CmsSearchConfigurationSorting;
033import org.opencms.jsp.search.state.CmsSearchStateSorting;
034import org.opencms.jsp.search.state.I_CmsSearchStateSorting;
035import org.opencms.search.solr.CmsSolrQuery;
036
037import java.util.Map;
038
039/** Controller for sorting options. */
040public class CmsSearchControllerSorting implements I_CmsSearchControllerSorting {
041
042    /** The sorting configuration. */
043    private final I_CmsSearchConfigurationSorting m_config;
044    /** The state for sorting (chosen option). */
045    private final I_CmsSearchStateSorting m_state;
046
047    /** Constructor taking a sorting configuration.
048     * @param config The sorting configuration.
049     */
050    public CmsSearchControllerSorting(final I_CmsSearchConfigurationSorting config) {
051
052        m_config = config;
053        m_state = new CmsSearchStateSorting();
054        m_state.setSelectedOption(m_config.getDefaultSortOption());
055    }
056
057    /**
058     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
059     */
060    @Override
061    public void addParametersForCurrentState(final Map<String, String[]> parameters) {
062
063        if ((null != m_state.getSelected()) && (m_config.getDefaultSortOption() != m_state.getCheckSelected())) {
064            parameters.put(m_config.getSortParam(), new String[] {m_state.getSelected().getParamValue()});
065        }
066    }
067
068    /**
069     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery, CmsObject)
070     */
071    @Override
072    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
073
074        if (m_state.getSelected() != null) {
075            query.set("sort", m_state.getSelected().getSolrValue());
076        }
077    }
078
079    /**
080     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerSorting#getConfig()
081     */
082    @Override
083    public I_CmsSearchConfigurationSorting getConfig() {
084
085        return m_config;
086    }
087
088    /**
089     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerSorting#getState()
090     */
091    @Override
092    public I_CmsSearchStateSorting getState() {
093
094        return m_state;
095    }
096
097    /**
098     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
099     */
100    @Override
101    public void updateForQueryChange() {
102
103        // do nothing
104
105    }
106
107    /**
108     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
109     */
110    @Override
111    public void updateFromRequestParameters(final Map<String, String[]> parameters, boolean isReloaded) {
112
113        if (parameters.containsKey(m_config.getSortParam())) {
114            final String[] sortValues = parameters.get(m_config.getSortParam());
115            if (sortValues.length > 0) {
116                final String sortValue = sortValues[0];
117                for (final I_CmsSearchConfigurationSortOption sortOption : m_config.getSortOptions()) {
118                    if (sortOption.getParamValue().equals(sortValue)) {
119                        m_state.setSelectedOption(sortOption);
120                        return;
121                    }
122                }
123            }
124        }
125    }
126
127}