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.gwt.shared;
029
030import com.google.gwt.user.client.rpc.IsSerializable;
031
032/**
033 * A bean for storing information about whether a resource could be locked or not, and if not, why.<p>
034 *
035 * @since 8.0.0
036 */
037public class CmsLockInfo implements IsSerializable {
038
039    /**
040     * An enum indicating the success or type of failure of a locking operation.<p>
041     */
042    public enum State {
043        /** The resource was already locked by another user. */
044        locked, /** The resource has changed in the meantime. */
045        changed, /** The resource was successfully locked. */
046        success, /** Some other error occurred. */
047        other;
048    }
049
050    /** The state indicating whether (and how) the locking operation succeeded or failed. */
051    private State m_state;
052
053    /** A user name. */
054    private String m_user;
055
056    /** An additional error message. */
057    private String m_exceptionMessage;
058
059    /**
060     * Creates a new lock info bean.<p>
061     *
062     * @param state the state of the locking operation
063     * @param user a user name
064     * @param exceptionMessage an additional error message
065     */
066    public CmsLockInfo(State state, String user, String exceptionMessage) {
067
068        m_state = state;
069        m_user = user;
070        m_exceptionMessage = exceptionMessage;
071    }
072
073    /**
074     * Empty constructor for serialization.<p>
075     */
076    protected CmsLockInfo() {
077
078        // do nothing
079    }
080
081    /**
082     * Creates a new info bean for a resource which has changed since it was opened.<p>
083     *
084     * @param user the user by which it was changed
085     * @return the new lock info bean
086     */
087    public static CmsLockInfo forChangedResource(String user) {
088
089        return new CmsLockInfo(State.changed, user, null);
090    }
091
092    /**
093     * Creates a new info bean for other types of errors.<p>
094     *
095     * @param errorMessage the additional error message
096     *
097     * @return the new lock info bean
098     */
099    public static CmsLockInfo forError(String errorMessage) {
100
101        return new CmsLockInfo(State.other, null, errorMessage);
102    }
103
104    /**
105     * Returns a lock info bean for  a resource locked by another user.<p>
106     *
107     * @param lockUser the other user
108     *
109     * @return the new lock info bean
110     */
111    public static CmsLockInfo forLockedResource(String lockUser) {
112
113        return new CmsLockInfo(State.locked, lockUser, null);
114    }
115
116    /**
117     * Returns a lock info bean for a successful lock operation.<p>
118     *
119     * @return the new lock info bean
120     */
121    public static CmsLockInfo forSuccess() {
122
123        return new CmsLockInfo(State.success, null, null);
124    }
125
126    /**
127     * Returns true if the locking succeeded.<p>
128     *
129     * @return true if the locking succeeded
130     */
131    public boolean couldLock() {
132
133        return m_state == State.success;
134    }
135
136    /**
137     * Returns the additional error message.<p>
138     *
139     * @return the additional error message
140     */
141    public String getErrorMessage() {
142
143        return m_exceptionMessage;
144    }
145
146    /**
147     * Returns the state of the locking operation.<p>
148     *
149     * @return the state of the locking operation
150     */
151    public State getState() {
152
153        return m_state;
154    }
155
156    /**
157     * Returns the user name.<p>
158     *
159     * @return a user name
160     */
161    public String getUser() {
162
163        return m_user;
164    }
165
166}