1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.DuplicateLockException;
18 import com.liferay.portal.ExpiredLockException;
19 import com.liferay.portal.NoSuchLockException;
20 import com.liferay.portal.PortalException;
21 import com.liferay.portal.SystemException;
22 import com.liferay.portal.model.Lock;
23 import com.liferay.portal.model.User;
24 import com.liferay.portal.service.base.LockLocalServiceBaseImpl;
25
26 import java.util.Date;
27 import java.util.List;
28
29
34 public class LockLocalServiceImpl extends LockLocalServiceBaseImpl {
35
36 public void clear() throws SystemException {
37 lockPersistence.removeByExpirationDate(new Date());
38 }
39
40 public Lock getLock(String className, long key)
41 throws PortalException, SystemException {
42
43 return getLock(className, String.valueOf(key));
44 }
45
46 public Lock getLock(String className, String key)
47 throws PortalException, SystemException {
48
49 Lock lock = lockPersistence.findByC_K(className, key);
50
51 if (lock.isExpired()) {
52 unlock(className, key);
53
54 throw new ExpiredLockException();
55 }
56
57 return lock;
58 }
59
60 public boolean hasLock(long userId, String className, long key)
61 throws PortalException, SystemException {
62
63 return hasLock(userId, className, String.valueOf(key));
64 }
65
66 public boolean hasLock(long userId, String className, String key)
67 throws PortalException, SystemException {
68
69 try {
70 Lock lock = getLock(className, key);
71
72 if (lock.getUserId() == userId) {
73 return true;
74 }
75 }
76 catch (ExpiredLockException ele) {
77 }
78 catch (NoSuchLockException nsle) {
79 }
80
81 return false;
82 }
83
84 public boolean isLocked(String className, long key)
85 throws PortalException, SystemException {
86
87 return isLocked(className, String.valueOf(key));
88 }
89
90 public boolean isLocked(String className, String key)
91 throws PortalException, SystemException {
92
93 try {
94 getLock(className, key);
95
96 return true;
97 }
98 catch (ExpiredLockException ele) {
99 }
100 catch (NoSuchLockException nsle) {
101 }
102
103 return false;
104 }
105
106 public Lock lock(
107 long userId, String className, long key, String owner,
108 boolean inheritable, long expirationTime)
109 throws PortalException, SystemException {
110
111 return lock(
112 userId, className, String.valueOf(key), owner, inheritable,
113 expirationTime);
114 }
115
116 public Lock lock(
117 long userId, String className, String key, String owner,
118 boolean inheritable, long expirationTime)
119 throws PortalException, SystemException {
120
121 Date now = new Date();
122
123 Lock lock = lockPersistence.fetchByC_K(className, key);
124
125 if (lock != null) {
126 if (lock.isExpired()) {
127 unlock(className, key);
128
129 lock = null;
130 }
131 else if (lock.getUserId() != userId) {
132 throw new DuplicateLockException(lock);
133 }
134 }
135
136 if (lock == null) {
137 User user = userPersistence.findByPrimaryKey(userId);
138
139 long lockId = counterLocalService.increment();
140
141 lock = lockPersistence.create(lockId);
142
143 lock.setCompanyId(user.getCompanyId());
144 lock.setUserId(user.getUserId());
145 lock.setUserName(user.getFullName());
146 lock.setUserId(userId);
147 lock.setClassName(className);
148 lock.setKey(key);
149 lock.setOwner(owner);
150 lock.setInheritable(inheritable);
151 }
152
153 lock.setCreateDate(now);
154
155 if (expirationTime == 0) {
156 lock.setExpirationDate(null);
157 }
158 else {
159 lock.setExpirationDate(new Date(now.getTime() + expirationTime));
160 }
161
162 lockPersistence.update(lock, false);
163
164 return lock;
165 }
166
167 public Lock refresh(String uuid, long expirationTime)
168 throws PortalException, SystemException {
169
170 Date now = new Date();
171
172 List<Lock> locks = lockPersistence.findByUuid(uuid);
173
174 if (locks.isEmpty()) {
175 throw new NoSuchLockException();
176 }
177
178 Lock lock = locks.get(0);
179
180 lock.setCreateDate(now);
181
182 if (expirationTime == 0) {
183 lock.setExpirationDate(null);
184 }
185 else {
186 lock.setExpirationDate(new Date(now.getTime() + expirationTime));
187 }
188
189 lockPersistence.update(lock, false);
190
191 return lock;
192 }
193
194 public void unlock(String className, long key) throws SystemException {
195 unlock(className, String.valueOf(key));
196 }
197
198 public void unlock(String className, String key) throws SystemException {
199 try {
200 lockPersistence.removeByC_K(className, key);
201 }
202 catch (NoSuchLockException nsle) {
203 }
204 }
205
206 }