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 GmbH & Co. KG, 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.log;
029
030import org.opencms.util.CmsUUID;
031
032import java.util.Collections;
033import java.util.Date;
034import java.util.HashSet;
035import java.util.Set;
036
037/**
038 * A filter to retrieve log entries.<p>
039 *
040 * @since 8.0.0
041 */
042public final class CmsLogFilter implements Cloneable {
043
044    /** To filter all. */
045    public static final CmsLogFilter ALL = new CmsLogFilter();
046
047    /** The starting date to filter. */
048    private long m_dateFrom;
049
050    /** The end date to filter. */
051    private long m_dateTo = Long.MAX_VALUE;
052
053    /** The types to exclude. */
054    private Set<CmsLogEntryType> m_excludeTypes = new HashSet<CmsLogEntryType>();
055
056    /** The types to include. */
057    private Set<CmsLogEntryType> m_includeTypes = new HashSet<CmsLogEntryType>();
058
059    /** The structure id of the resource to filter. */
060    private CmsUUID m_structureId;
061
062    /** The user id to filter. */
063    private CmsUUID m_userId;
064
065    /**
066     * Private constructor.<p>
067     */
068    private CmsLogFilter() {
069
070        // empty
071    }
072
073    /**
074     * @see java.lang.Object#clone()
075     */
076    @Override
077    public Object clone() {
078
079        CmsLogFilter filter = new CmsLogFilter();
080        filter.m_structureId = m_structureId;
081        filter.m_includeTypes = new HashSet<CmsLogEntryType>(m_includeTypes);
082        filter.m_excludeTypes = new HashSet<CmsLogEntryType>(m_excludeTypes);
083        filter.m_userId = m_userId;
084        filter.m_dateFrom = m_dateFrom;
085        filter.m_dateTo = m_dateTo;
086        return filter;
087    }
088
089    /**
090     * Returns an extended filter with the given type restriction.<p>
091     *
092     * @param type the relation type to exclude
093     *
094     * @return an extended filter with the given type restriction
095     */
096    public CmsLogFilter excludeType(CmsLogEntryType type) {
097
098        CmsLogFilter filter = (CmsLogFilter)clone();
099        filter.m_excludeTypes.add(type);
100        return filter;
101    }
102
103    /**
104     * Returns an extended filter with the starting date restriction.<p>
105     *
106     * @param from the starting date to filter
107     *
108     * @return an extended filter with the starting date restriction
109     */
110    public CmsLogFilter filterFrom(long from) {
111
112        CmsLogFilter filter = (CmsLogFilter)clone();
113        filter.m_dateFrom = from;
114        return filter;
115    }
116
117    /**
118     * Returns an extended filter with the given resource restriction.<p>
119     *
120     * @param structureId the structure id to filter
121     *
122     * @return an extended filter with the given resource restriction
123     */
124    public CmsLogFilter filterResource(CmsUUID structureId) {
125
126        CmsLogFilter filter = (CmsLogFilter)clone();
127        filter.m_structureId = structureId;
128        return filter;
129    }
130
131    /**
132     * Returns an extended filter with the end date restriction.<p>
133     *
134     * @param to the end date to filter
135     *
136     * @return an extended filter with the end date restriction
137     */
138    public CmsLogFilter filterTo(long to) {
139
140        CmsLogFilter filter = (CmsLogFilter)clone();
141        filter.m_dateTo = to;
142        return filter;
143    }
144
145    /**
146     * Returns an extended filter with the given user ID restriction.<p>
147     *
148     * @param userId the user ID to filter
149     *
150     * @return an extended filter with the given user ID restriction
151     */
152    public CmsLogFilter filterUser(CmsUUID userId) {
153
154        CmsLogFilter filter = (CmsLogFilter)clone();
155        filter.m_userId = userId;
156        return filter;
157    }
158
159    /**
160     * Returns the starting date restriction.<p>
161     *
162     * @return the starting date restriction
163     */
164    public long getDateFrom() {
165
166        return m_dateFrom;
167    }
168
169    /**
170     * Returns the end date restriction.<p>
171     *
172     * @return the end date restriction
173     */
174    public long getDateTo() {
175
176        return m_dateTo;
177    }
178
179    /**
180     * Returns the types to exclude.<p>
181     *
182     * @return the types to exclude
183     */
184    public Set<CmsLogEntryType> getExcludeTypes() {
185
186        return Collections.unmodifiableSet(m_excludeTypes);
187    }
188
189    /**
190     * Returns the types to include.<p>
191     *
192     * @return the types to include
193     */
194    public Set<CmsLogEntryType> getIncludeTypes() {
195
196        return Collections.unmodifiableSet(m_includeTypes);
197    }
198
199    /**
200     * Returns the structure Id of the resource to filter.<p>
201     *
202     * @return the structure Id of the resource to filter
203     */
204    public CmsUUID getStructureId() {
205
206        return m_structureId;
207    }
208
209    /**
210     * Returns the user ID restriction.<p>
211     *
212     * @return the user ID restriction
213     */
214    public CmsUUID getUserId() {
215
216        return m_userId;
217    }
218
219    /**
220     * Returns an extended filter with the given type restriction.<p>
221     *
222     * @param type the relation type to include
223     *
224     * @return an extended filter with the given type restriction
225     */
226    public CmsLogFilter includeType(CmsLogEntryType type) {
227
228        CmsLogFilter filter = (CmsLogFilter)clone();
229        filter.m_includeTypes.add(type);
230        return filter;
231    }
232
233    /**
234     * Returns <code>true</code> if the given log entry type matches this filter.<p>
235     *
236     * @param type the log entry type to test
237     *
238     * @return <code>true</code> if the given log entry type matches this filter
239     */
240    public boolean matchType(CmsLogEntryType type) {
241
242        if (m_excludeTypes.contains(type)) {
243            return false;
244        }
245        if (m_includeTypes.isEmpty()) {
246            return true;
247        }
248        return m_includeTypes.contains(type);
249    }
250
251    /**
252     * @see java.lang.Object#toString()
253     */
254    @Override
255    public String toString() {
256
257        StringBuffer str = new StringBuffer(128);
258        str.append("[");
259        str.append("resource").append("=").append(m_structureId).append(", ");
260        str.append("user").append("=").append(m_userId).append(", ");
261        str.append("from").append("=").append(new Date(m_dateFrom)).append(", ");
262        str.append("to").append("=").append(new Date(m_dateTo)).append(", ");
263        str.append("includeTypes").append("=").append(m_includeTypes);
264        str.append("excludeTypes").append("=").append(m_excludeTypes);
265        str.append("]");
266        return str.toString();
267    }
268}