1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.lock.model;
21  
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.io.Serializable;
25  
26  import java.util.Date;
27  
28  /**
29   * <a href="Lock.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   *
33   */
34  public class Lock implements Comparable<Lock>, Serializable {
35  
36      public Lock() {
37      }
38  
39      public Lock(
40          String uuid, String className, Comparable<?> pk, long userId,
41          String owner, long expirationTime) {
42  
43          _uuid = uuid;
44          _className = className;
45          _pk = pk;
46          _userId = userId;
47          _owner = owner;
48          _expirationTime = expirationTime;
49          _date = new Date();
50      }
51  
52      public int compareTo(Lock lock) {
53          if (lock == null) {
54              return -1;
55          }
56  
57          int value = 0;
58          value = getClassName().compareTo(lock.getClassName());
59  
60          if (value != 0) {
61              return value;
62          }
63  
64          value = getUuid().compareTo(lock.getUuid());
65  
66          if (value != 0) {
67              return value;
68          }
69  
70          value = ((Comparable<Object>)getPrimaryKey()).compareTo(
71              lock.getPrimaryKey());
72  
73          if (value != 0) {
74              return value;
75          }
76  
77          value = getOwner().compareTo(lock.getOwner());
78  
79          if (value != 0) {
80              return value;
81          }
82  
83          value = getDate().compareTo(lock.getDate());
84  
85          if (value != 0) {
86              return value;
87          }
88  
89          return 0;
90      }
91  
92      public boolean equals(Lock lock) {
93          if (lock == null) {
94              return false;
95          }
96  
97          if (getClassName().equals(lock.getClassName()) &&
98              getPrimaryKey().equals(lock.getPrimaryKey())) {
99  
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106 
107     public String getClassName() {
108         return _className;
109     }
110 
111     public Date getDate() {
112         return _date;
113     }
114 
115     public long getExpirationTime() {
116         return _expirationTime;
117     }
118 
119     public String getOwner() {
120         if (Validator.isNull(_owner)) {
121             return String.valueOf(_userId);
122         }
123         else {
124             return _owner;
125         }
126     }
127 
128     public Comparable<?> getPrimaryKey() {
129         return _pk;
130     }
131 
132     public long getUserId() {
133         return _userId;
134     }
135 
136     public String getUuid() {
137         return _uuid;
138     }
139 
140     public int hashCode() {
141         return getClassName().hashCode() + getPrimaryKey().hashCode();
142     }
143 
144     public boolean isExpired() {
145         if (_expirationTime <= 0) {
146             return false;
147         }
148         else {
149             Date now = new Date();
150 
151             if (now.getTime() > (_date.getTime() + _expirationTime)) {
152                 return true;
153             }
154             else {
155                 return false;
156             }
157         }
158     }
159 
160     public void setExpirationTime(long expirationTime) {
161         _expirationTime = expirationTime;
162         _date = new Date();
163     }
164 
165     public void setUuid(String uuid) {
166         _uuid = uuid;
167     }
168 
169     private String _className;
170     private Comparable<?> _pk;
171     private long _userId;
172     private String _owner;
173     private String _uuid;
174     private long _expirationTime;
175     private Date _date;
176 
177 }