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.repository;
029
030import java.util.Date;
031
032/**
033 * The class represents a lock to a {@link I_CmsRepositoryItem}.<p>
034 *
035 * @since 6.2.4
036 */
037public class CmsRepositoryLockInfo {
038
039    /** The lock scope "exclusive". */
040    public static final String SCOPE_EXCLUSIVE = "exclusive";
041
042    /** The default scope for locks. */
043    public static final String DEFAULT_SCOPE = SCOPE_EXCLUSIVE;
044
045    /** Infinite timeout for the lock. */
046    public static final int TIMEOUT_INFINITE_VALUE = -1;
047
048    /** The lock type "write". */
049    public static final String TYPE_WRITE = "write";
050
051    /** The default type for locks. */
052    public static final String DEFAULT_TYPE = TYPE_WRITE;
053
054    /** Default depth is infinite. */
055    public static final int DEPTH_INFINITY_VALUE = 3; // To limit tree browsing a bit
056
057    /** The lock scope "shared". */
058    public static final String SCOPE_SHARED = "shared";
059
060    /** The creation date of the lock. */
061    private Date m_creationDate = new Date();
062
063    /** The depth the lock is valid for (0 for the resource or "Infinity" for inheriting). */
064    private int m_depth;
065
066    /** The time when the lock expires. */
067    private long m_expiresAt = TIMEOUT_INFINITE_VALUE;
068
069    /** The owner of the lock. */
070    private String m_owner = "";
071
072    /** The path of the resource item the lock belongs to. */
073    private String m_path = "/";
074
075    /** The scope of the lock (shared or exclusive). */
076    private String m_scope = DEFAULT_SCOPE;
077
078    /** The type of the lock (write or read). */
079    private String m_type = DEFAULT_TYPE;
080
081    /** The login name of the current user. */
082    private String m_username = "";
083
084    /**
085     * Empty default constructor.<p>
086     */
087    public CmsRepositoryLockInfo() {
088
089        // empty default constructor
090    }
091
092    /**
093     * Returns the creationDate.<p>
094     *
095     * @return the creationDate
096     */
097    public Date getCreationDate() {
098
099        return m_creationDate;
100    }
101
102    /**
103     * Returns the depth.<p>
104     *
105     * @return the depth
106     */
107    public int getDepth() {
108
109        return m_depth;
110    }
111
112    /**
113     * Returns the expiresAt.<p>
114     *
115     * @return the expiresAt
116     */
117    public long getExpiresAt() {
118
119        return m_expiresAt;
120    }
121
122    /**
123     * Returns the owner.<p>
124     *
125     * @return the owner
126     */
127    public String getOwner() {
128
129        return m_owner;
130    }
131
132    /**
133     * Returns the path.<p>
134     *
135     * @return the path
136     */
137    public String getPath() {
138
139        return m_path;
140    }
141
142    /**
143     * Returns the scope.<p>
144     *
145     * @return the scope
146     */
147    public String getScope() {
148
149        return m_scope;
150    }
151
152    /**
153     * Returns the type.<p>
154     *
155     * @return the type
156     */
157    public String getType() {
158
159        return m_type;
160    }
161
162    /**
163     * Returns the username.<p>
164     *
165     * @return the username
166     */
167    public String getUsername() {
168
169        return m_username;
170    }
171
172    /**
173     * Return true if the lock has expired.
174     *
175     * @return true if the lock has expired
176     */
177    public boolean hasExpired() {
178
179        return (System.currentTimeMillis() > m_expiresAt);
180    }
181
182    /**
183     * Return true if the lock is exclusive.
184     *
185     * @return true if the lock is exclusive
186     */
187    public boolean isExclusive() {
188
189        return (m_scope.equals("exclusive"));
190
191    }
192
193    /**
194     * Sets the depth.<p>
195     *
196     * @param depth the depth to set
197     */
198    public void setDepth(int depth) {
199
200        m_depth = depth;
201    }
202
203    /**
204     * Sets the expiresAt.<p>
205     *
206     * @param expiresAt the expiresAt to set
207     */
208    public void setExpiresAt(long expiresAt) {
209
210        m_expiresAt = expiresAt;
211    }
212
213    /**
214     * Sets the owner.<p>
215     *
216     * @param owner the owner to set
217     */
218    public void setOwner(String owner) {
219
220        m_owner = owner;
221    }
222
223    /**
224     * Sets the path.<p>
225     *
226     * @param path the path to set
227     */
228    public void setPath(String path) {
229
230        m_path = path;
231    }
232
233    /**
234     * Sets the scope.<p>
235     *
236     * @param scope the scope to set
237     */
238    public void setScope(String scope) {
239
240        m_scope = scope;
241    }
242
243    /**
244     * Sets the type.<p>
245     *
246     * @param type the type to set
247     */
248    public void setType(String type) {
249
250        m_type = type;
251    }
252
253    /**
254     * Sets the username.<p>
255     *
256     * @param username the username to set
257     */
258    public void setUsername(String username) {
259
260        m_username = username;
261    }
262
263    /**
264     * Get a string representation of this lock info.<p>
265     *
266     * @return a string representation of this lock
267     */
268    @Override
269    public String toString() {
270
271        String result = "Type:" + m_type + "\n";
272        result += "Scope:" + m_scope + "\n";
273        result += "Depth:" + m_depth + "\n";
274        result += "Owner:" + m_owner + "\n";
275        result += "Expiration:" + new Date(m_expiresAt) + "\n";
276
277        return result;
278    }
279}