1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.lock.model;
24  
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import java.io.Serializable;
28  
29  import java.util.Date;
30  
31  /**
32   * <a href="Lock.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class Lock implements Comparable<Lock>, Serializable {
38  
39      public Lock() {
40      }
41  
42      public Lock(
43          String uuid, String className, Comparable<?> pk, long userId,
44          String owner, boolean inheritable, long expirationTime) {
45  
46          _uuid = uuid;
47          _className = className;
48          _pk = pk;
49          _userId = userId;
50          _owner = owner;
51          _inheritable = inheritable;
52          _expirationTime = expirationTime;
53          _date = new Date();
54      }
55  
56      public int compareTo(Lock lock) {
57          if (lock == null) {
58              return -1;
59          }
60  
61          int value = getUuid().compareTo(lock.getUuid());
62  
63          if (value != 0) {
64              return value;
65          }
66  
67          value = getClassName().compareTo(lock.getClassName());
68  
69          if (value != 0) {
70              return value;
71          }
72  
73          value = ((Comparable<Object>)getPrimaryKey()).compareTo(
74              lock.getPrimaryKey());
75  
76          if (value != 0) {
77              return value;
78          }
79  
80          value = getOwner().compareTo(lock.getOwner());
81  
82          if (value != 0) {
83              return value;
84          }
85  
86          value = ((Comparable<Boolean>)isInheritable()).compareTo(
87              lock.isInheritable());
88  
89          if (value != 0) {
90              return value;
91          }
92  
93          value = getDate().compareTo(lock.getDate());
94  
95          if (value != 0) {
96              return value;
97          }
98  
99          return 0;
100     }
101 
102     public boolean equals(Lock lock) {
103         if (lock == null) {
104             return false;
105         }
106 
107         if (getClassName().equals(lock.getClassName()) &&
108             getPrimaryKey().equals(lock.getPrimaryKey())) {
109 
110             return true;
111         }
112         else {
113             return false;
114         }
115     }
116 
117     public String getClassName() {
118         return _className;
119     }
120 
121     public Date getDate() {
122         return _date;
123     }
124 
125     public long getExpirationTime() {
126         return _expirationTime;
127     }
128 
129     public String getOwner() {
130         if (Validator.isNull(_owner)) {
131             return String.valueOf(_userId);
132         }
133         else {
134             return _owner;
135         }
136     }
137 
138     public Comparable<?> getPrimaryKey() {
139         return _pk;
140     }
141 
142     public long getUserId() {
143         return _userId;
144     }
145 
146     public String getUuid() {
147         return _uuid;
148     }
149 
150     public int hashCode() {
151         return getClassName().hashCode() + getPrimaryKey().hashCode();
152     }
153 
154     public boolean isExpired() {
155         if (_expirationTime <= 0) {
156             return false;
157         }
158         else {
159             Date now = new Date();
160 
161             if (now.getTime() > (_date.getTime() + _expirationTime)) {
162                 return true;
163             }
164             else {
165                 return false;
166             }
167         }
168     }
169 
170     public boolean isInheritable() {
171         return _inheritable;
172     }
173 
174     public void setExpirationTime(long expirationTime) {
175         _expirationTime = expirationTime;
176         _date = new Date();
177     }
178 
179     public void setUuid(String uuid) {
180         _uuid = uuid;
181     }
182 
183     private String _uuid;
184     private String _className;
185     private Comparable<?> _pk;
186     private long _userId;
187     private String _owner;
188     private boolean _inheritable;
189     private long _expirationTime;
190     private Date _date;
191 
192 }