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.parser.simplesearch;
029
030import org.opencms.jsp.search.config.parser.simplesearch.daterestrictions.I_CmsDateRestriction;
031import org.opencms.jsp.search.config.parser.simplesearch.preconfiguredrestrictions.CmsRestrictionsBean;
032import org.opencms.main.CmsLog;
033import org.opencms.util.CmsUUID;
034import org.opencms.xml.types.CmsXmlDisplayFormatterValue;
035
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041import org.apache.commons.logging.Log;
042
043/**
044 * The list configuration data.<p>
045 */
046public class CmsConfigurationBean {
047
048    /**
049     * Enum representing how filter queries should be combined in a search.<p>
050     */
051    public static enum CombinationMode {
052        /** Combine with AND. */
053        AND,
054
055        /** Combine with OR. */
056        OR;
057    }
058
059    /** Parameter field key. */
060    public static final String N_TITLE = "Title";
061
062    /** Parameter field key. */
063    public static final String PARAM_TITLE = "Title";
064
065    /** Parameter field key. */
066    public static final String PARAM_FILTER_MULTI_DAY = "FilterMultiDay";
067
068    /** Parameter field key. */
069    public static final String PARAM_FILTER_QUERY = "FilterQuery";
070
071    /** Parameter field key. */
072    public static final String PARAM_SORT_ORDER = "SortOrder";
073
074    /** Parameter field key. */
075    public static final String PARAM_SHOW_EXPIRED = "ShowExpired";
076
077    /** Parameter field key. */
078    public static final String PARAM_MAX_RESULTS = "MaxResults";
079
080    /** The logger for this class. */
081    static final Log LOG = CmsLog.getLog(CmsConfigurationBean.class.getName());
082
083    /** Special parameter to configure the maximally returned results. */
084    private static final String ADDITIONAL_PARAM_MAX_RETURNED_RESULTS = "maxresults";
085
086    /** The additional content parameters. */
087    private Map<String, String> m_additionalParameters;
088
089    /** The resource blacklist. */
090    private List<CmsUUID> m_blacklist;
091
092    /** The categories. */
093    private List<String> m_categories;
094
095    /** The category mode. */
096    private CombinationMode m_categoryMode;
097
098    /** The date restriction. */
099    private I_CmsDateRestriction m_dateRestriction;
100
101    /** The display types. */
102    private List<String> m_dislayTypes;
103
104    /** The Geo filter */
105    private CmsGeoFilterBean m_geoFilter;
106
107    /** The folders. */
108    private List<String> m_folders;
109
110    /** Search parameters by configuration node name. */
111    private Map<String, String> m_parameterFields;
112
113    /** The preconfigured restrictions */
114    private CmsRestrictionsBean m_preconfiguredRestrictions;
115
116    /** Combined category and folder restrictions. */
117    private List<CmsCategoryFolderRestrictionBean> m_categoryFolderRestrictions = new ArrayList<>();
118
119    /**
120     * Constructor.<p>
121     */
122    public CmsConfigurationBean() {
123
124        m_parameterFields = new HashMap<String, String>();
125    }
126
127    /**
128     * Extracts the resource type name from a display type string.
129     * 
130     * @param displayType the display type
131     * @return the resource type name 
132     */
133    public static String getResourceTypeForDisplayType(String displayType) {
134
135        String type = displayType;
136        if (type.contains(CmsXmlDisplayFormatterValue.SEPARATOR)) {
137            type = type.substring(0, type.indexOf(CmsXmlDisplayFormatterValue.SEPARATOR));
138        }
139        return type;
140    }
141
142    /**
143     * Add a combined category-folder restriction.
144     * @param listCategoryFolderRestrictionBean the category-folder restriction to add.
145     */
146    public void addCategoryFolderFilter(CmsCategoryFolderRestrictionBean listCategoryFolderRestrictionBean) {
147
148        m_categoryFolderRestrictions.add(listCategoryFolderRestrictionBean);
149
150    }
151
152    /**
153     * Returns the additional content parameters.<p>
154     *
155     * @return the additional content parameters
156     */
157    public Map<String, String> getAdditionalParameters() {
158
159        return m_additionalParameters;
160    }
161
162    /**
163     * Returns the black list.<p>
164     *
165     * @return the black list
166     */
167    public List<CmsUUID> getBlacklist() {
168
169        return m_blacklist;
170    }
171
172    /**
173     * Returns the categories.<p>
174     *
175     * @return the categories
176     */
177    public List<String> getCategories() {
178
179        return m_categories;
180    }
181
182    /**
183     * Returns the combined category-folder restrictions.<p>
184     *
185     * @return the combined category-folder restrictions
186     */
187    public List<CmsCategoryFolderRestrictionBean> getCategoryFolderRestrictions() {
188
189        return m_categoryFolderRestrictions;
190    }
191
192    /**
193     * Gets the category mode.<p>
194     *
195     * @return the category mode
196     */
197    public CombinationMode getCategoryMode() {
198
199        return m_categoryMode;
200    }
201
202    /**
203     * Gets the date restriction.<p>
204     *
205     * @return the date restriction
206     */
207    public I_CmsDateRestriction getDateRestriction() {
208
209        return m_dateRestriction;
210    }
211
212    /**
213     * Returns the display types.<p>
214     *
215     * @return the display types
216     */
217    public List<String> getDisplayTypes() {
218
219        return m_dislayTypes;
220    }
221
222    /**
223     * Gets the filter query.<p>
224     *
225     * @return the filter query
226     */
227    public String getFilterQuery() {
228
229        return m_parameterFields.get(PARAM_FILTER_QUERY);
230    }
231
232    /**
233     * Returns the folders.<p>
234     *
235     * @return the folders
236     */
237    public List<String> getFolders() {
238
239        return m_folders;
240    }
241
242    /**
243     * Returns the Geo filter.<p>
244     *
245     * @return the Geo filter
246     */
247    public CmsGeoFilterBean getGeoFilter() {
248
249        return m_geoFilter;
250    }
251
252    /**
253     * Returns the number of results to return maximally, or <code>null</code> if not explicitly specified.
254     * @return the number of results to return maximally, or <code>null</code> if not explicitly specified.
255     */
256    public Integer getMaximallyReturnedResults() {
257
258        String resString = m_parameterFields.get(PARAM_MAX_RESULTS);
259        // Fallback, we first added the restriction as additional parameter. To make it more obvious, we integrated it as extra field.
260        // Only if the extra field is not set, we use the additional parameter to be backward compatible.
261        if (null == resString) {
262            m_additionalParameters.get(ADDITIONAL_PARAM_MAX_RETURNED_RESULTS);
263        }
264        if (null != resString) {
265            try {
266                return Integer.valueOf(resString);
267            } catch (NumberFormatException e) {
268                if (LOG.isErrorEnabled()) {
269                    LOG.error("Ignoring invalid maxresults param " + resString + " in list-config.");
270                }
271            }
272        }
273        return null;
274    }
275
276    /**
277     * Returns the parameter map.<p>
278     *
279     * @return the parameters
280     */
281    public Map<String, String> getParameters() {
282
283        return m_parameterFields;
284    }
285
286    /**
287     * Returns the parameter by name.<p>
288     *
289     * @param key the parameter name
290     *
291     * @return the parameter value
292     */
293    public String getParameterValue(String key) {
294
295        return m_parameterFields.get(key);
296    }
297
298    /**
299     * Returns the preconfigured restrictions.
300     * @return the preconfigured restrictions.
301     */
302    public CmsRestrictionsBean getPreconfiguredRestrictions() {
303
304        return m_preconfiguredRestrictions;
305    }
306
307    /**
308     * Gets the sort order.<p>
309     *
310     * @return the sort order
311     */
312    public String getSortOrder() {
313
314        return getParameterValue(PARAM_SORT_ORDER);
315    }
316
317    /**
318     * Returns the search types.<p>
319     *
320     * @return the search types
321     */
322    public List<String> getTypes() {
323
324        List<String> result = new ArrayList<String>();
325        if (m_dislayTypes != null) {
326            for (String displayType : m_dislayTypes) {
327                String type = getResourceTypeForDisplayType(displayType);
328                if (!result.contains(type)) {
329                    result.add(type);
330                }
331            }
332        }
333        return result;
334    }
335
336    /**
337     * Returns a flag, indicating if there are preconfigured restrictions.
338     *
339     * @return <code>true</code> iff there are preconfiugred restrictions, <code>false</code> otherwise.
340     */
341    public boolean hasPreconfiguredRestrictions() {
342
343        return (null != m_preconfiguredRestrictions) && m_preconfiguredRestrictions.hasRestrictions();
344    }
345
346    /**
347     * Returns a flag, indicating if there are preconfigured restrictions for the provided type.
348     *
349     * @param type the type to check the existence of preconfigured restrictions for.
350     *
351     * @return <code>true</code> iff there are preconfigured restrictions for the provided type, <code>false</code> otherwise.
352     */
353    public boolean hasTypeSpecificRestriction(String type) {
354
355        return (null != m_preconfiguredRestrictions) && m_preconfiguredRestrictions.hasRestrictionForType(type);
356    }
357
358    /**
359     * Returns the 'show expired' setting.<p>
360     *
361     * @return the 'show expired' setting
362     */
363    public boolean isShowExpired() {
364
365        return Boolean.parseBoolean(m_parameterFields.get(PARAM_SHOW_EXPIRED));
366
367    }
368
369    /**
370     * Sets the additional content parameters.<p>
371     *
372     * @param additionalParameters the additional content parameters to set
373     */
374    public void setAdditionalParameters(Map<String, String> additionalParameters) {
375
376        m_additionalParameters = additionalParameters;
377    }
378
379    /**
380     * Sets the blacklist.<p>
381     *
382     * @param blacklist the blacklist
383     */
384    public void setBlacklist(List<CmsUUID> blacklist) {
385
386        m_blacklist = blacklist;
387    }
388
389    /**
390     * Sets the categories.<p>
391     *
392     * @param categories the categories
393     */
394    public void setCategories(List<String> categories) {
395
396        m_categories = categories;
397    }
398
399    /**
400     * Sets the category mode.<p>
401     *
402     * @param categoryMode the category mode to set
403     */
404    public void setCategoryMode(CombinationMode categoryMode) {
405
406        m_categoryMode = categoryMode;
407    }
408
409    /**
410     * Sets the date restrictions.<p>
411     *
412     * @param restriction the date restrictions
413     */
414    public void setDateRestriction(I_CmsDateRestriction restriction) {
415
416        m_dateRestriction = restriction;
417    }
418
419    /**
420     * Sets the display types.<p>
421     *
422     * @param displayTypes the display types
423     */
424    public void setDisplayTypes(List<String> displayTypes) {
425
426        m_dislayTypes = displayTypes;
427    }
428
429    /**
430     * Sets the folders.<p>
431     *
432     * @param folders the folders
433     */
434    public void setFolders(List<String> folders) {
435
436        m_folders = folders;
437    }
438
439    /**
440     * Sets the Geo filter.<p>
441     *
442     * @param geoFilter the Geo filter
443     */
444    public void setGeoFilter(CmsGeoFilterBean geoFilter) {
445
446        m_geoFilter = geoFilter;
447    }
448
449    /**
450     * Sets the parameter by name.<p>
451     *
452     * @param name the parameter name
453     * @param value the parameter value
454     */
455    public void setParameterValue(String name, String value) {
456
457        m_parameterFields.put(name, value);
458
459    }
460
461    /**
462     * Set the preconfigured restrictions.
463     *
464     * @param restrictionBean the restrictions to set.
465     */
466    public void setPreconfiguredRestrictions(CmsRestrictionsBean restrictionBean) {
467
468        m_preconfiguredRestrictions = restrictionBean;
469    }
470}