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.lock;
029
030import org.opencms.util.A_CmsModeIntEnumeration;
031
032/**
033 * Indicates the different possible lock types.<p>
034 *
035 * @since 7.0.0
036 */
037public final class CmsLockType extends A_CmsModeIntEnumeration {
038
039    /**
040     * A lock that allows the user to edit the resource's structure record,
041     * it's resource record, and its content record.<p>
042     *
043     * This lock is assigned to files that are locked via the context menu.
044     */
045    public static final CmsLockType EXCLUSIVE = new CmsLockType(4);
046
047    /** A lock that is inherited from a locked parent folder. */
048    public static final CmsLockType INHERITED = new CmsLockType(3);
049
050    /** A lock that indicates that the resource is waiting to be published in the publish queue. */
051    public static final CmsLockType PUBLISH = new CmsLockType(7);
052
053    /**
054     * A lock that allows the user to edit the resource's structure record only,
055     * but not it's resource record nor content record.<p>
056     *
057     * This lock is assigned to files if a sibling of the resource record has
058     * already an exclusive lock.
059     */
060    public static final CmsLockType SHARED_EXCLUSIVE = new CmsLockType(2);
061
062    /**
063     * A lock that allows the user to edit the resource's structure record only,
064     * but not it's resource record nor content record.<p>
065     *
066     * This lock is assigned to resources that already have a shared exclusive lock,
067     * and then inherit a lock because one of it's parent folders gets locked.
068     */
069    public static final CmsLockType SHARED_INHERITED = new CmsLockType(1);
070
071    /**
072     * A temporary exclusive lock that allows the user to edit the resource's structure record,
073     * it's resource record, and its content record.<p>
074     *
075     * This lock is identical to the {@link #EXCLUSIVE} lock, but it is automatically removed after
076     * a user is logged out.<p>
077     */
078    public static final CmsLockType TEMPORARY = new CmsLockType(6);
079
080    /** Type of the NULL lock obtained by {@link CmsLock#getNullLock()}. */
081    public static final CmsLockType UNLOCKED = new CmsLockType(0);
082
083    /** Type of the NULL system lock. */
084    protected static final CmsLockType SYSTEM_UNLOCKED = new CmsLockType(8);
085
086    /** serializable version id. */
087    private static final long serialVersionUID = 5333767594124738789L;
088
089    /**
090     * Creates a new lock type with the given name.<p>
091     *
092     * @param type the type id to use
093     */
094    private CmsLockType(int type) {
095
096        super(type);
097    }
098
099    /**
100     * Returns the lock type for the given type value.<p>
101     *
102     * This is used only for serialization and should not be accessed for other purposes.<p>
103     *
104     * @param type the type value to get the lock type for
105     *
106     * @return the lock type for the given type value
107     */
108    public static CmsLockType valueOf(int type) {
109
110        switch (type) {
111            case 1:
112                return SHARED_INHERITED;
113            case 2:
114                return SHARED_EXCLUSIVE;
115            case 3:
116                return INHERITED;
117            case 4:
118                return EXCLUSIVE;
119            case 6:
120                return TEMPORARY;
121            case 7:
122                return PUBLISH;
123            case 8:
124                return SYSTEM_UNLOCKED;
125            default:
126                return UNLOCKED;
127        }
128    }
129
130    /**
131     * Returns <code>true</code> if this is an directly inherited lock.<p>
132     *
133     * @return <code>true</code> if this is an directly inherited lock
134     */
135    public boolean isDirectlyInherited() {
136
137        return (this == CmsLockType.INHERITED);
138    }
139
140    /**
141     * Returns <code>true</code> if this is an exclusive (or temporary exclusive) lock.<p>
142     *
143     * @return <code>true</code> if this is an exclusive (or temporary exclusive) lock
144     */
145    public boolean isExclusive() {
146
147        return (this == CmsLockType.EXCLUSIVE) || (this == CmsLockType.TEMPORARY);
148    }
149
150    /**
151     * Returns <code>true</code> if this is an inherited lock, which may either be directly or shared inherited.<p>
152     *
153     * @return <code>true</code> if this is an inherited lock, which may either be directly or shared inherited
154     */
155    public boolean isInherited() {
156
157        return (isDirectlyInherited() || isSharedInherited());
158    }
159
160    /**
161     * Returns <code>true</code> if this is a persistent lock that should be saved when the systems shuts down.<p>
162     *
163     * @return <code>true</code> if this is a persistent lock that should be saved when the systems shuts down
164     */
165    public boolean isPersistent() {
166
167        return (this == CmsLockType.EXCLUSIVE) || isPublish();
168    }
169
170    /**
171     * Returns <code>true</code> if this is a publish lock.<p>
172     *
173     * @return <code>true</code> if this is a publish lock
174     */
175    public boolean isPublish() {
176
177        return (this == CmsLockType.PUBLISH);
178    }
179
180    /**
181     * Returns <code>true</code> if this is a shared lock.<p>
182     *
183     * @return <code>true</code> if this is a shared lock
184     */
185    public boolean isShared() {
186
187        return (isSharedExclusive() || isSharedInherited());
188    }
189
190    /**
191     * Returns <code>true</code> if this is an shared exclusive lock.<p>
192     *
193     * @return <code>true</code> if this is an shared exclusive lock
194     */
195    public boolean isSharedExclusive() {
196
197        return (this == CmsLockType.SHARED_EXCLUSIVE);
198    }
199
200    /**
201     * Returns <code>true</code> if this is an shared inherited lock.<p>
202     *
203     * @return <code>true</code> if this is an shared inherited lock
204     */
205    public boolean isSharedInherited() {
206
207        return (this == CmsLockType.SHARED_INHERITED);
208    }
209
210    /**
211     * Returns <code>true</code> if this is a system (2nd level) lock.<p>
212     *
213     * @return <code>true</code> if this is a system (2nd level) lock
214     */
215    public boolean isSystem() {
216
217        return (isPublish() || (this == CmsLockType.SYSTEM_UNLOCKED));
218    }
219
220    /**
221     * Returns <code>true</code> if this is a temporary lock.<p>
222     *
223     * @return <code>true</code> if this is a temporary lock
224     */
225    public boolean isTemporary() {
226
227        return (this == CmsLockType.TEMPORARY);
228    }
229
230    /**
231     * Returns <code>true</code> if this lock is in fact unlocked.<p>
232     *
233     * Only if this is <code>true</code>, the result lock is equal to the <code>NULL</code> lock,
234     * which can be obtained by {@link CmsLock#getNullLock()}.<p>
235     *
236     * @return <code>true</code> if this lock is in fact unlocked
237     */
238    public boolean isUnlocked() {
239
240        return ((this == CmsLockType.UNLOCKED) || (this == CmsLockType.SYSTEM_UNLOCKED));
241    }
242
243    /**
244     * @see java.lang.Object#toString()
245     */
246    @Override
247    public String toString() {
248
249        switch (getMode()) {
250            case 1:
251                return "shared inherited";
252            case 2:
253                return "shared exclusive";
254            case 3:
255                return "inherited";
256            case 4:
257                return "exclusive";
258            case 6:
259                return "temporary exclusive";
260            case 7:
261                return "publish";
262            case 8:
263                return "system unlocked";
264            default:
265                return "unlocked";
266        }
267    }
268}