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.db.urlname;
029
030import org.opencms.util.CmsStringUtil;
031import org.opencms.util.CmsUUID;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * A class which contains filter criteria for reading or deleting URL name mapping entries.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsUrlNameMappingFilter {
042
043    /** Base filter which matches all URL name mapping entries. */
044    public static final CmsUrlNameMappingFilter ALL = new CmsUrlNameMappingFilter();
045
046    /** The locale which should be matched. */
047    private String m_locale;
048
049    /** The name which should be matched. */
050    private String m_name;
051
052    /** The name pattern which should be matched. */
053    private String m_namePattern;
054
055    /** The structure id which should not be matched. */
056    private CmsUUID m_rejectStructureId;
057
058    /** The states which should be matched. */
059    private int[] m_states;
060
061    /** The structure id which should be matched. */
062    private CmsUUID m_structureId;
063
064    /**
065     * The default constructor.<p>
066     */
067    protected CmsUrlNameMappingFilter() {
068
069        // do nothing
070    }
071
072    /**
073     * The copy constructor.<p>
074     *
075     * @param filter the filter to copy
076     */
077    protected CmsUrlNameMappingFilter(CmsUrlNameMappingFilter filter) {
078
079        m_name = filter.m_name;
080        m_structureId = filter.m_structureId;
081        m_rejectStructureId = filter.m_rejectStructureId;
082        m_states = filter.m_states;
083        m_namePattern = filter.m_namePattern;
084        m_locale = filter.m_locale;
085    }
086
087    /**
088     * Returns a new url name mapping filter based on the current one which also has to match a given locale.<p>
089     *
090     * @param locale the locale to match
091     *
092     * @return the new filter
093     */
094    public CmsUrlNameMappingFilter filterLocale(String locale) {
095
096        if (locale == null) {
097            throw new IllegalArgumentException();
098        }
099        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
100        result.m_locale = locale;
101        return result;
102    }
103
104    /**
105     * Creates a new filter from the current filter which also has to match a given name.<p>
106     *
107     * @param name the name to match
108     *
109     * @return a new filter
110     */
111    public CmsUrlNameMappingFilter filterName(String name) {
112
113        if (name == null) {
114            throw new IllegalArgumentException();
115        }
116        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
117        result.m_name = name;
118        return result;
119    }
120
121    /**
122     * Creates a new filter from the current filter which also has to match a given name pattern.<p>
123     *
124     * @param namePattern the name pattern which should be matched
125     *
126     * @return a new filter
127     */
128    public CmsUrlNameMappingFilter filterNamePattern(String namePattern) {
129
130        if (namePattern == null) {
131            throw new IllegalArgumentException();
132        }
133        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
134        result.m_namePattern = namePattern;
135        return result;
136    }
137
138    /**
139     * Creates a new filter from the current filter which also must not match a given structure id.<p>
140     *
141     * @param id the structure id to not match
142     *
143     * @return a new filter
144     */
145    public CmsUrlNameMappingFilter filterRejectStructureId(CmsUUID id) {
146
147        if (id == null) {
148            throw new IllegalArgumentException();
149        }
150        if (m_structureId != null) {
151            throw new IllegalStateException();
152        }
153        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
154        result.m_rejectStructureId = id;
155        return result;
156    }
157
158    /**
159     * Creates a new filter from the current filter which also has to match a given state.<p>
160     *
161     * @param states the states to match
162     *
163     * @return the new filter
164     */
165    public CmsUrlNameMappingFilter filterStates(int... states) {
166
167        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
168        result.m_states = states;
169        return result;
170
171    }
172
173    /**
174     * Creates a new filter from the current filter which also has to match a given structure id.<p>
175     *
176     * @param structureId the structure id to match
177     *
178     * @return the new filter
179     */
180    public CmsUrlNameMappingFilter filterStructureId(CmsUUID structureId) {
181
182        if (structureId == null) {
183            throw new IllegalArgumentException();
184        }
185        CmsUrlNameMappingFilter result = new CmsUrlNameMappingFilter(this);
186        result.m_structureId = structureId;
187        return result;
188    }
189
190    /**
191     * Returns the locale which should be matched by the filter.<p>
192     *
193     * @return the locale
194     */
195    public String getLocale() {
196
197        return m_locale;
198    }
199
200    /**
201     * Returns the name which should be matched by the filter.<p>
202     *
203     * @return the name which should be matched by the filter
204     */
205    public String getName() {
206
207        return m_name;
208
209    }
210
211    /**
212     * Returns the name pattern which should be matched by the filter.<p>
213     *
214     * @return the name pattern which should be matched by the filter
215     */
216    public String getNamePattern() {
217
218        return m_namePattern;
219    }
220
221    /**
222     * Returns the structure id which should not be matched by the filter.<p>
223     *
224     * @return a structure id
225     */
226    public CmsUUID getRejectStructureId() {
227
228        return m_rejectStructureId;
229    }
230
231    /**
232     * Returns the state which should be matched by the filter.<p>
233     *
234     * @return the state which should be matched by the filter
235     */
236    public int[] getStates() {
237
238        return m_states;
239    }
240
241    /**
242     * Returns the structure id which should be matched by the filter.<p>
243     *
244     * @return the structure id which should be matched by the filter
245     */
246    public CmsUUID getStructureId() {
247
248        return m_structureId;
249    }
250
251    /**
252     * Checks whether this is a filter which only filters by structure id.<p>
253     *
254     * @return true if this is a filter which only filters by structure id
255     */
256    public boolean isIdFilter() {
257
258        return (m_structureId != null)
259            && (m_name == null)
260            && (m_namePattern == null)
261            && (m_states == null)
262            && (m_rejectStructureId == null)
263            && (m_locale == null);
264    }
265
266    /**
267     * Checks whether this is a filter which only filters by name.<p>
268     *
269     * @return true if this is a filter which only filters by name
270     */
271    public boolean isNameFilter() {
272
273        return (m_structureId == null)
274            && (m_name != null)
275            && (m_namePattern == null)
276            && (m_states == null)
277            && (m_rejectStructureId == null)
278            && (m_locale == null);
279    }
280
281    /**
282     * @see java.lang.Object#toString()
283     */
284    @Override
285    public String toString() {
286
287        List<String> resultParts = new ArrayList<String>();
288        resultParts.add("[CmsUrlNameMappingFilter:");
289        if (m_name != null) {
290            resultParts.add("name='" + m_name + "'");
291        }
292        if (m_structureId != null) {
293            resultParts.add("id=" + m_structureId);
294        }
295        if (m_states != null) {
296            resultParts.add("states=" + m_states);
297        }
298        if (m_namePattern != null) {
299            resultParts.add("pattern='" + m_namePattern + "'");
300        }
301        if (m_rejectStructureId != null) {
302            resultParts.add("rejectId=" + m_rejectStructureId);
303        }
304        resultParts.add("]");
305        return CmsStringUtil.listAsString(resultParts, " ");
306    }
307
308}