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