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.preconfiguredrestrictions;
029
030import org.opencms.jsp.search.config.parser.simplesearch.CmsConfigurationBean.CombinationMode;
031
032import java.util.Locale;
033
034/**
035 * A single restriction rule, telling for which field and type what kind of restriction should be enforced.
036 */
037public class CmsRestrictionRule {
038
039    /** Match type of the restriction, i.e., how the values should be treated. */
040    public static enum MatchType {
041        /** Quote the value to match */
042        EXACT,
043        /** Take the values as is. */
044        DEFAULT,
045        /** Adds * in the beginning of each word */
046        POSTFIX,
047        /** Adds * in the end of each word */
048        PREFIX,
049        /** Adds * in the beginning and end of each word */
050        INFIX
051    }
052
053    /** Placeholder that is replaced by the locale where we want to search in. */
054    private static String LOCALE_PLACEHOLDER = "#";
055
056    /** The type the restriction should hold for. */
057    private String m_type;
058    /** The solr field, the restriction should be placed on */
059    private String m_field;
060    /** The match type for the restriction. */
061    private MatchType m_match;
062    /** The combination mode for multiple values in different input fields, either AND or OR - match all or only any of the values. */
063    private CombinationMode m_combinationModeBetweenFields;
064    /** The combination mode for multiple values in one input field, either AND or OR - match all or only any of the values. */
065    private CombinationMode m_combinationModeInField;
066
067    /**
068     * Constructs a restriction rule for the provided field that has no type restriction and default combination mode and match type.
069     * @param field the field to put the restriction on.
070     */
071    public CmsRestrictionRule(String field) {
072
073        this(field, null, null, null, null);
074    }
075
076    /**
077     * Constructs restriction rule with the provided settings.
078     * @param field the field the restriction has to be enforced on.
079     * @param type the resource type the restriction should be enforced on.
080     * @param matchType the way how values should be treated.
081     * @param combinationModeBetweenFields the way how values in different fields should be combined.
082     * @param combinationModeInField the way how values in different fields in one field should be combined.
083     */
084    public CmsRestrictionRule(
085        String field,
086        String type,
087        MatchType matchType,
088        CombinationMode combinationModeBetweenFields,
089        CombinationMode combinationModeInField) {
090
091        m_field = field;
092        m_type = type;
093        m_match = null == matchType ? MatchType.DEFAULT : matchType;
094        m_combinationModeBetweenFields = null == combinationModeBetweenFields
095        ? CombinationMode.OR
096        : combinationModeBetweenFields;
097        m_combinationModeInField = (null == combinationModeInField)
098        ? m_combinationModeBetweenFields
099        : combinationModeInField;
100    }
101
102    /**
103     * Returns the way values of different input fields are combined (match any or all).
104     * @return the way values of different input fields are combined (match any or all).
105     */
106    public CombinationMode getCombinationModeBetweenFields() {
107
108        return m_combinationModeBetweenFields;
109    }
110
111    /**
112     * Returns the way values of different input fields are combined (match any or all).
113     * @return the way values of different input fields are combined (match any or all).
114     */
115    public CombinationMode getCombinationModeInField() {
116
117        return m_combinationModeInField;
118    }
119
120    /**
121     * Returns the name of the solr field, the restrictions should be enforced on for, dependent on the provided locale.
122     * @param l the search locale.
123     * @return the name of the solr field, the restrictions should be enforced on for, dependent on the provided locale.
124     */
125    public String getFieldForLocale(Locale l) {
126
127        return m_field.replace(LOCALE_PLACEHOLDER, l.toString());
128    }
129
130    /**
131     * Returns the match type for values, e.g., exact, as-is, prefix, ....
132     * @return the match type for values, e.g., exact, as-is, prefix, ....
133     */
134    public MatchType getMatchType() {
135
136        return m_match;
137    }
138
139    /**
140     * Returns the name of the solr field, the restrictions should be enforced on, but without resolving the locale placeholder.
141     * @return the name of the solr field, the restrictions should be enforced on, but without resolving the locale placeholder.
142     */
143    public String getRawField() {
144
145        return m_field;
146    }
147
148    /**
149     * Returns the name of the resource type, the restriction should be enforced for.
150     * @return the resource type, the restriction should be enforced for.
151     *      <code>null</code> is returned iff the restriction should hold for all resource types.
152     */
153    public String getType() {
154
155        return m_type;
156    }
157}