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, long expirationTime) {
45  
46          _uuid = uuid;
47          _className = className;
48          _pk = pk;
49          _userId = userId;
50          _owner = owner;
51          _expirationTime = expirationTime;
52          _date = new Date();
53      }
54  
55      public int compareTo(Lock lock) {
56          if (lock == null) {
57              return -1;
58          }
59  
60          int value = 0;
61          value = getClassName().compareTo(lock.getClassName());
62  
63          if (value != 0) {
64              return value;
65          }
66  
67          value = getUuid().compareTo(lock.getUuid());
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 = getDate().compareTo(lock.getDate());
87  
88          if (value != 0) {
89              return value;
90          }
91  
92          return 0;
93      }
94  
95      public boolean equals(Lock lock) {
96          if (lock == null) {
97              return false;
98          }
99  
100         if (getClassName().equals(lock.getClassName()) &&
101             getPrimaryKey().equals(lock.getPrimaryKey())) {
102 
103             return true;
104         }
105         else {
106             return false;
107         }
108     }
109 
110     public String getClassName() {
111         return _className;
112     }
113 
114     public Date getDate() {
115         return _date;
116     }
117 
118     public long getExpirationTime() {
119         return _expirationTime;
120     }
121 
122     public String getOwner() {
123         if (Validator.isNull(_owner)) {
124             return String.valueOf(_userId);
125         }
126         else {
127             return _owner;
128         }
129     }
130 
131     public Comparable<?> getPrimaryKey() {
132         return _pk;
133     }
134 
135     public long getUserId() {
136         return _userId;
137     }
138 
139     public String getUuid() {
140         return _uuid;
141     }
142 
143     public int hashCode() {
144         return getClassName().hashCode() + getPrimaryKey().hashCode();
145     }
146 
147     public boolean isExpired() {
148         if (_expirationTime <= 0) {
149             return false;
150         }
151         else {
152             Date now = new Date();
153 
154             if (now.getTime() > (_date.getTime() + _expirationTime)) {
155                 return true;
156             }
157             else {
158                 return false;
159             }
160         }
161     }
162 
163     public void setExpirationTime(long expirationTime) {
164         _expirationTime = expirationTime;
165         _date = new Date();
166     }
167 
168     public void setUuid(String uuid) {
169         _uuid = uuid;
170     }
171 
172     private String _className;
173     private Comparable<?> _pk;
174     private long _userId;
175     private String _owner;
176     private String _uuid;
177     private long _expirationTime;
178     private Date _date;
179 
180 }