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.config;
029
030import java.util.List;
031
032/** Configuration for sorting in general. */
033public class CmsSearchConfigurationSorting implements I_CmsSearchConfigurationSorting {
034
035    /** Default request parameter holding the selected sort option. */
036    public static final String DEFAULT_SORT_PARAM = "sort";
037    /** The request parameter used to send the currently chosen search option. */
038    private final String m_sortParam;
039    /** The available sort options. */
040    private final List<I_CmsSearchConfigurationSortOption> m_options;
041
042    /** The default sort option. */
043    private final I_CmsSearchConfigurationSortOption m_defaultOption;
044
045    /** Constructor setting all options.
046     * @param sortParam The request parameter used to send the currently chosen search option.
047     * @param options The available sort options.
048     * @param defaultOption The default sort option.
049     */
050    public CmsSearchConfigurationSorting(
051        final String sortParam,
052        final List<I_CmsSearchConfigurationSortOption> options,
053        final I_CmsSearchConfigurationSortOption defaultOption) {
054
055        m_sortParam = sortParam == null ? DEFAULT_SORT_PARAM : sortParam;
056        m_options = options;
057        m_defaultOption = defaultOption;
058    }
059
060    /** Creates a sort configuration iff at least one of the parameters is not null and the options list is not empty.
061     * @param sortParam The request parameter used to send the currently chosen search option.
062     * @param options The available sort options.
063     * @param defaultOption The default sort option.
064     * @return the sort configuration or null, depending on the arguments.
065     */
066    public static CmsSearchConfigurationSorting create(
067        final String sortParam,
068        final List<I_CmsSearchConfigurationSortOption> options,
069        final I_CmsSearchConfigurationSortOption defaultOption) {
070
071        return (null != sortParam) || ((null != options) && !options.isEmpty()) || (null != defaultOption)
072        ? new CmsSearchConfigurationSorting(sortParam, options, defaultOption)
073        : null;
074    }
075
076    /**
077     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationSorting#getDefaultSortOption()
078     */
079    public I_CmsSearchConfigurationSortOption getDefaultSortOption() {
080
081        return m_defaultOption;
082    }
083
084    /**
085     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationSorting#getSortOptions()
086     */
087    @Override
088    public List<I_CmsSearchConfigurationSortOption> getSortOptions() {
089
090        return m_options;
091    }
092
093    /**
094     * @see org.opencms.jsp.search.config.I_CmsSearchConfigurationSorting#getSortParam()
095     */
096    @Override
097    public String getSortParam() {
098
099        return m_sortParam;
100    }
101
102}