001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/lock/CmsLockFilter.java,v $
003: * Date : $Date: 2008-02-27 12:05:46 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.lock;
033:
034: import org.opencms.file.CmsUser;
035: import org.opencms.util.CmsUUID;
036:
037: import java.util.Collections;
038: import java.util.HashSet;
039: import java.util.Set;
040:
041: /**
042: * A filter to retrieve the locks.<p>
043: *
044: * @author Michael Moossen
045: *
046: * @version $Revision: 1.5 $
047: *
048: * @since 6.5.4
049: */
050: public final class CmsLockFilter implements Cloneable {
051:
052: /** To filter all locks. */
053: public static final CmsLockFilter FILTER_ALL = new CmsLockFilter(
054: true).filterIncludeChildren();
055:
056: /** To filter all inherited locks. */
057: public static final CmsLockFilter FILTER_INHERITED = new CmsLockFilter(
058: true);
059:
060: /** To filter all non inherited locks. */
061: public static final CmsLockFilter FILTER_NON_INHERITED = new CmsLockFilter(
062: false);
063:
064: /** If set the filter restricts the result excluding locks owned by the given user. */
065: private CmsUUID m_notOwnedByUserId = null;
066:
067: /** If set the filter extends the result to non inherited locks. */
068: private boolean m_includeChildren = false;
069:
070: /** If set the filter restricts the result including only locks owned by the given user. */
071: private CmsUUID m_ownedByUserId = null;
072:
073: /** If set the filter extends the result to inherited locks. */
074: private boolean m_includeParents = false;
075:
076: /** If set the filter restricts the result to the given project. */
077: private CmsUUID m_projectId = null;
078:
079: /** If set the filter also matches shared exclusive locks. */
080: private boolean m_sharedExclusive = false;
081:
082: /** The types to filter. */
083: private Set m_types = new HashSet();
084:
085: /** If set the filter restricts the result excluding locks not lockable by the given user. */
086: private CmsUser m_notLockableByUser = null;
087:
088: /** If set the filter restricts the result including only locks lockable by the given user. */
089: private CmsUser m_lockableByUser = null;
090:
091: /**
092: * Private constructor.<p>
093: *
094: * @param inherited if the this lock filter should checks inherited locks or not
095: */
096: private CmsLockFilter(boolean inherited) {
097:
098: m_includeChildren = !inherited;
099: m_includeParents = inherited;
100: }
101:
102: /**
103: * @see java.lang.Object#clone()
104: */
105: public Object clone() {
106:
107: CmsLockFilter filter = new CmsLockFilter(false);
108: filter.m_includeChildren = m_includeChildren;
109: filter.m_includeParents = m_includeParents;
110: filter.m_types = new HashSet(m_types);
111: filter.m_ownedByUserId = m_ownedByUserId;
112: filter.m_notOwnedByUserId = m_notOwnedByUserId;
113: filter.m_projectId = m_projectId;
114: filter.m_notLockableByUser = m_notLockableByUser;
115: filter.m_lockableByUser = m_lockableByUser;
116: return filter;
117: }
118:
119: /**
120: * Returns an extended filter with the given user restriction.<p>
121: *
122: * @param userId the user id to filter
123: *
124: * @return an extended filter with the given user restriction
125: */
126: public CmsLockFilter filterNotOwnedByUserId(CmsUUID userId) {
127:
128: CmsLockFilter filter = (CmsLockFilter) this .clone();
129: filter.m_notOwnedByUserId = userId;
130: return filter;
131: }
132:
133: /**
134: * Returns an extended filter with the given user restriction.<p>
135: *
136: * @param user the user to filter
137: *
138: * @return an extended filter with the given user restriction
139: */
140: public CmsLockFilter filterNotLockableByUser(CmsUser user) {
141:
142: CmsLockFilter filter = (CmsLockFilter) this .clone();
143: filter.m_notLockableByUser = user;
144: return filter;
145: }
146:
147: /**
148: * Returns an extended filter with the given user restriction.<p>
149: *
150: * @param user the user to filter
151: *
152: * @return an extended filter with the given user restriction
153: */
154: public CmsLockFilter filterLockableByUser(CmsUser user) {
155:
156: CmsLockFilter filter = (CmsLockFilter) this .clone();
157: filter.m_lockableByUser = user;
158: return filter;
159: }
160:
161: /**
162: * Returns an extended filter that will extend the result to the given path and all its children.<p>
163: *
164: * @return an extended filter to search the subresources of the given path
165: */
166: public CmsLockFilter filterIncludeChildren() {
167:
168: CmsLockFilter filter = (CmsLockFilter) this .clone();
169: filter.m_includeChildren = true;
170: return filter;
171: }
172:
173: /**
174: * Returns an extended filter with the given user restriction.<p>
175: *
176: * @param userId the user id to filter
177: *
178: * @return an extended filter with the given user restriction
179: */
180: public CmsLockFilter filterOwnedByUserId(CmsUUID userId) {
181:
182: CmsLockFilter filter = (CmsLockFilter) this .clone();
183: filter.m_ownedByUserId = userId;
184: return filter;
185: }
186:
187: /**
188: * Returns an extended filter that will extend the result to the given path and all its parents.<p>
189: *
190: * @return an extended filter to search the subresources of the given path
191: */
192: public CmsLockFilter filterIncludeParents() {
193:
194: CmsLockFilter filter = (CmsLockFilter) this .clone();
195: filter.m_includeParents = true;
196: return filter;
197: }
198:
199: /**
200: * Returns an extended filter with the given project restriction.<p>
201: *
202: * @param projectId the project to filter the locks with
203: *
204: * @return an extended filter with the given project restriction
205: */
206: public CmsLockFilter filterProject(CmsUUID projectId) {
207:
208: CmsLockFilter filter = (CmsLockFilter) this .clone();
209: filter.m_projectId = projectId;
210: return filter;
211: }
212:
213: /**
214: * Returns an extended filter that also matches shared exclusive locks (siblings).<p>
215: *
216: * @return an extended filter that also matches shared exclusive locks
217: */
218: public CmsLockFilter filterSharedExclusive() {
219:
220: CmsLockFilter filter = (CmsLockFilter) this .clone();
221: filter.m_sharedExclusive = true;
222: return filter;
223: }
224:
225: /**
226: * Returns an extended filter with the given type restriction.<p>
227: *
228: * @param type the lock type to filter
229: *
230: * @return an extended filter with the given type restriction
231: */
232: public CmsLockFilter filterType(CmsLockType type) {
233:
234: CmsLockFilter filter = (CmsLockFilter) this .clone();
235: filter.m_types.add(type);
236: return filter;
237: }
238:
239: /**
240: * Returns the user that has to own the locks.<p>
241: *
242: * @return the user that has to own the locks
243: */
244: public CmsUUID getOwnedByUserId() {
245:
246: return m_ownedByUserId;
247: }
248:
249: /**
250: * Returns the user that has not to own the locks.<p>
251: *
252: * @return the user that has not to own the locks
253: */
254: public CmsUUID getNotOwnedByUserId() {
255:
256: return m_notOwnedByUserId;
257: }
258:
259: /**
260: * Returns the user that can overwrite the locks.<p>
261: *
262: * @return the user that can overwrite the locks
263: */
264: public CmsUser getLockableByUserId() {
265:
266: return m_lockableByUser;
267: }
268:
269: /**
270: * Returns the user that can not overwrite the locks.<p>
271: *
272: * @return the user that can not overwrite the locks
273: */
274: public CmsUser getNotLockableByUserId() {
275:
276: return m_notLockableByUser;
277: }
278:
279: /**
280: * Returns the project restriction.<p>
281: *
282: * @return the project restriction
283: */
284: public CmsUUID getProjectId() {
285:
286: return m_projectId;
287: }
288:
289: /**
290: * Returns the types to filter.<p>
291: *
292: * @return the types to filter
293: */
294: public Set getTypes() {
295:
296: return Collections.unmodifiableSet(m_types);
297: }
298:
299: /**
300: * Returns the include children flag.<p>
301: *
302: * @return if set the filter extends the result to the given path and all its children
303: */
304: public boolean isIncludeChildren() {
305:
306: return m_includeChildren;
307: }
308:
309: /**
310: * Returns the include parents flag.<p>
311: *
312: * @return if set the filter extends the result to the given path and all its parents
313: */
314: public boolean isIncludeParent() {
315:
316: return m_includeParents;
317: }
318:
319: /**
320: * Returns the <code>true</code> if this filter also matches shared exclusive locks.<p>
321: *
322: * @return the <code>true</code> if this filter also matches shared exclusive locks
323: */
324: public boolean isSharedExclusive() {
325:
326: return m_sharedExclusive;
327: }
328:
329: /**
330: * Matches the given lock against this filter and the given path.<p>
331: *
332: * @param rootPath the path to match the lock against
333: * @param lock the lock to match
334: *
335: * @return <code>true</code> if the given lock matches
336: */
337: public boolean match(String rootPath, CmsLock lock) {
338:
339: boolean match = false;
340: if (m_includeChildren) {
341: // safe since rootPath always ends with slash if a folder
342: match = lock.getResourceName().startsWith(rootPath);
343: }
344: if (!match && m_includeParents) {
345: // since parents can only be folders, check it only for folders
346: if (lock.getResourceName().endsWith("/")) {
347: match = rootPath.startsWith(lock.getResourceName());
348: }
349: }
350: if (match && (m_projectId != null) && !m_projectId.isNullUUID()
351: && (lock.getProjectId() != null)) {
352: match = lock.getProjectId().equals(m_projectId);
353: }
354: if (match && (m_ownedByUserId != null)
355: && !m_ownedByUserId.isNullUUID()) {
356: match = lock.getUserId().equals(m_ownedByUserId);
357: }
358: if (match && (m_notOwnedByUserId != null)
359: && !m_notOwnedByUserId.isNullUUID()) {
360: match = !lock.getUserId().equals(m_notOwnedByUserId);
361: }
362: if (match && (m_lockableByUser != null)) {
363: match = lock.isLockableBy(m_lockableByUser);
364: }
365: if (match && (m_notLockableByUser != null)) {
366: match = !lock.isLockableBy(m_notLockableByUser);
367: }
368: if (match && !m_types.isEmpty()) {
369: match = m_types.contains(lock.getType());
370: match = match || (m_includeParents && lock.isInherited());
371: }
372: // check the related lock if available
373: if (!match && !lock.getRelatedLock().isNullLock()) {
374: match = match(rootPath, lock.getRelatedLock());
375: }
376: return match;
377: }
378:
379: /**
380: * @see java.lang.Object#toString()
381: */
382: public String toString() {
383:
384: StringBuffer str = new StringBuffer(128);
385: str.append("[");
386: str.append("children").append("=").append(m_includeChildren)
387: .append(", ");
388: str.append("parents").append("=").append(m_includeParents)
389: .append(", ");
390: str.append("types").append("=").append(m_types).append(", ");
391: str.append("includedUser").append("=").append(m_ownedByUserId)
392: .append(", ");
393: str.append("excludedUser").append("=").append(
394: m_notOwnedByUserId).append(", ");
395: str.append("project").append("=").append(m_projectId).append(
396: ", ");
397: str.append("lockableBy").append("=").append(m_lockableByUser)
398: .append(", ");
399: str.append("notLockableBy").append("=").append(
400: m_notLockableByUser).append(", ");
401: str.append("includeShared").append("=").append(
402: m_sharedExclusive);
403: str.append("]");
404: return str.toString();
405: }
406: }
|