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_CmsSearchConfigurationGeoFilter;
032import org.opencms.jsp.search.state.CmsSearchStateGeoFilter;
033import org.opencms.jsp.search.state.I_CmsSearchStateGeoFilter;
034import org.opencms.main.CmsLog;
035import org.opencms.search.solr.CmsSolrQuery;
036import org.opencms.util.CmsGeoUtil;
037
038import java.util.Map;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * Search controller for the Geo filter.
044 */
045public class CmsSearchControllerGeoFilter implements I_CmsSearchControllerGeoFilter {
046
047    /** The logger instance for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsSearchControllerGeoFilter.class);
049
050    /** Configuration of the Geo filter. */
051    private final I_CmsSearchConfigurationGeoFilter m_config;
052
053    /** State of the Geo filter. */
054    private final I_CmsSearchStateGeoFilter m_state;
055
056    /**
057     * Constructor taking the managed configuration.
058     * @param config the configuration managed by the controller
059     */
060    public CmsSearchControllerGeoFilter(final I_CmsSearchConfigurationGeoFilter config) {
061
062        m_config = config;
063        m_state = new CmsSearchStateGeoFilter();
064    }
065
066    /**
067     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addParametersForCurrentState(java.util.Map)
068     */
069    @Override
070    public void addParametersForCurrentState(Map<String, String[]> parameters) {
071
072        if (m_state.hasGeoFilter()) {
073            parameters.put(m_config.getCoordinatesParam(), new String[] {m_state.getCoordinates()});
074            parameters.put(m_config.getRadiusParam(), new String[] {m_state.getRadius()});
075            parameters.put(m_config.getUnitsParam(), new String[] {m_state.getUnits()});
076        }
077    }
078
079    /**
080     * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(org.opencms.search.solr.CmsSolrQuery, org.opencms.file.CmsObject)
081     */
082    @Override
083    public void addQueryParts(CmsSolrQuery query, CmsObject cms) {
084
085        String fieldName = m_config.getFieldName();
086        if (m_state.hasGeoFilter()) {
087            String coordinates = m_state.getCoordinates();
088            String radius = m_state.getRadius();
089            String units = m_state.getUnits();
090            query.setGeoFilterQuery(fieldName, coordinates, radius, units);
091        }
092        if (m_config.hasGeoFilter()) {
093            String coordinates = m_config.getCoordinates();
094            String radius = m_config.getRadius();
095            String units = m_config.getUnits();
096            query.setGeoFilterQuery(fieldName, coordinates, radius, units);
097        }
098    }
099
100    /**
101     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerGeoFilter#getConfig()
102     */
103    @Override
104    public I_CmsSearchConfigurationGeoFilter getConfig() {
105
106        return m_config;
107    }
108
109    /**
110     * @see org.opencms.jsp.search.controller.I_CmsSearchControllerGeoFilter#getState()
111     */
112    @Override
113    public I_CmsSearchStateGeoFilter getState() {
114
115        return m_state;
116    }
117
118    /**
119     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateForQueryChange()
120     */
121    @Override
122    public void updateForQueryChange() {
123
124        // Nothing to do
125    }
126
127    /**
128     * @see org.opencms.jsp.search.controller.I_CmsSearchController#updateFromRequestParameters(java.util.Map, boolean)
129     */
130    @Override
131    public void updateFromRequestParameters(Map<String, String[]> parameters, boolean isRepeated) {
132
133        if (parameters.containsKey(m_config.getCoordinatesParam())) {
134            String[] coordinates = parameters.get(m_config.getCoordinatesParam());
135            if (((coordinates != null) && (coordinates.length > 0)) && CmsGeoUtil.validateCoordinates(coordinates[0])) {
136                m_state.setCoordinates(coordinates[0]);
137            } else {
138                m_state.setCoordinates(null);
139            }
140        }
141        if (parameters.containsKey(m_config.getRadiusParam())) {
142            String[] radius = parameters.get(m_config.getRadiusParam());
143            if (((radius != null) && (radius.length > 0)) && CmsGeoUtil.validateRadius(radius[0])) {
144                m_state.setRadius(radius[0]);
145            } else {
146                m_state.setRadius(null);
147            }
148        }
149        if (parameters.containsKey(m_config.getUnitsParam())) {
150            String[] units = parameters.get(m_config.getUnitsParam());
151            if ((units != null) && (units.length > 0) && CmsGeoUtil.validateUnits(units[0])) {
152                m_state.setUnits(units[0]);
153            } else {
154                m_state.setUnits(null);
155            }
156        }
157    }
158}