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_CmsSearchConfigurationPagination;
032import org.opencms.jsp.search.state.CmsSearchStatePagination;
033import org.opencms.jsp.search.state.I_CmsSearchStatePagination;
034import org.opencms.search.solr.CmsSolrQuery;
035
036import java.util.Map;
037
038/** Controller for the pagination. */
039public class CmsSearchControllerPagination implements I_CmsSearchControllerPagination {
040
041    /** The default page to show. */
042    private static final int DEFAULT_PAGE = 1;
043    /** The configuration of the pagination. */
044    private final I_CmsSearchConfigurationPagination m_config;
045    /** The state of the pagination. */
046    private final I_CmsSearchStatePagination m_state;
047
048    /** Constructor taking a pagination configuration.
049     * @param config The pagination configuration.
050     */
051    public CmsSearchControllerPagination(final I_CmsSearchConfigurationPagination config) {
052
053        m_config = config;
054        m_state = new CmsSearchStatePagination();
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 (!(m_state.getCurrentPage() == DEFAULT_PAGE)) {
064            parameters.put(m_config.getPageParam(), new String[] {Integer.toString(m_state.getCurrentPage())});
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        query.setRows(Integer.valueOf(getCurrentPageSize()));
075        final int start = getCurrentPageStart();
076        query.setStart(Integer.valueOf(start));
077    }
078
079    /**
080     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerPagination#getConfig()
081     */
082    @Override
083    public I_CmsSearchConfigurationPagination getConfig() {
084
085        return m_config;
086    }
087
088    /**
089     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerPagination#getCurrentPageSize()
090     */
091    @Override
092    public int getCurrentPageSize() {
093
094        return getConfig().getSizeOfPage(getState().getCurrentPage());
095    }
096
097    /**
098     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerPagination#getCurrentPageStart()
099     */
100    public int getCurrentPageStart() {
101
102        return m_config.getStartOfPage(m_state.getCurrentPage());
103    }
104
105    /**
106     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerPagination#getState()
107     */
108    @Override
109    public I_CmsSearchStatePagination getState() {
110
111        return m_state;
112    }
113
114    /**
115     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
116     */
117    @Override
118    public void updateForQueryChange() {
119
120        m_state.setIgnorePage(true);
121
122    }
123
124    /**
125     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
126     */
127    @Override
128    public void updateFromRequestParameters(final Map<String, String[]> parameters, boolean isReloaded) {
129
130        if (!m_state.getIgnorePage() && parameters.containsKey(m_config.getPageParam())) {
131            final String[] page = parameters.get(m_config.getPageParam());
132            if (page.length > 0) {
133                try {
134                    m_state.setCurrentPage(Integer.valueOf(page[0]).intValue());
135                    return;
136                } catch (@SuppressWarnings("unused") final NumberFormatException e) {
137                    m_state.setCurrentPage(DEFAULT_PAGE);
138                }
139            }
140        }
141        m_state.setCurrentPage(DEFAULT_PAGE);
142    }
143}