001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.DuplicateLockException;
018 import com.liferay.portal.ExpiredLockException;
019 import com.liferay.portal.NoSuchLockException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.model.Lock;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.base.LockLocalServiceBaseImpl;
025
026 import java.util.Date;
027 import java.util.List;
028
029
032 public class LockLocalServiceImpl extends LockLocalServiceBaseImpl {
033
034 public void clear() throws SystemException {
035 lockPersistence.removeByLtExpirationDate(new Date());
036 }
037
038 public Lock getLock(String className, long key)
039 throws PortalException, SystemException {
040
041 return getLock(className, String.valueOf(key));
042 }
043
044 public Lock getLock(String className, String key)
045 throws PortalException, SystemException {
046
047 Lock lock = lockPersistence.findByC_K(className, key);
048
049 if (lock.isExpired()) {
050 unlock(className, key);
051
052 throw new ExpiredLockException();
053 }
054
055 return lock;
056 }
057
058 public boolean hasLock(long userId, String className, long key)
059 throws PortalException, SystemException {
060
061 return hasLock(userId, className, String.valueOf(key));
062 }
063
064 public boolean hasLock(long userId, String className, String key)
065 throws PortalException, SystemException {
066
067 try {
068 Lock lock = getLock(className, key);
069
070 if (lock.getUserId() == userId) {
071 return true;
072 }
073 }
074 catch (ExpiredLockException ele) {
075 }
076 catch (NoSuchLockException nsle) {
077 }
078
079 return false;
080 }
081
082 public boolean isLocked(String className, long key)
083 throws PortalException, SystemException {
084
085 return isLocked(className, String.valueOf(key));
086 }
087
088 public boolean isLocked(String className, String key)
089 throws PortalException, SystemException {
090
091 try {
092 getLock(className, key);
093
094 return true;
095 }
096 catch (ExpiredLockException ele) {
097 }
098 catch (NoSuchLockException nsle) {
099 }
100
101 return false;
102 }
103
104 public Lock lock(
105 long userId, String className, long key, String owner,
106 boolean inheritable, long expirationTime)
107 throws PortalException, SystemException {
108
109 return lock(
110 userId, className, String.valueOf(key), owner, inheritable,
111 expirationTime);
112 }
113
114 public Lock lock(
115 long userId, String className, String key, String owner,
116 boolean inheritable, long expirationTime)
117 throws PortalException, SystemException {
118
119 Date now = new Date();
120
121 Lock lock = lockPersistence.fetchByC_K(className, key);
122
123 if (lock != null) {
124 if (lock.isExpired()) {
125 unlock(className, key);
126
127 lock = null;
128 }
129 else if (lock.getUserId() != userId) {
130 throw new DuplicateLockException(lock);
131 }
132 }
133
134 if (lock == null) {
135 User user = userPersistence.findByPrimaryKey(userId);
136
137 long lockId = counterLocalService.increment();
138
139 lock = lockPersistence.create(lockId);
140
141 lock.setCompanyId(user.getCompanyId());
142 lock.setUserId(user.getUserId());
143 lock.setUserName(user.getFullName());
144 lock.setUserId(userId);
145 lock.setClassName(className);
146 lock.setKey(key);
147 lock.setOwner(owner);
148 lock.setInheritable(inheritable);
149 }
150
151 lock.setCreateDate(now);
152
153 if (expirationTime == 0) {
154 lock.setExpirationDate(null);
155 }
156 else {
157 lock.setExpirationDate(new Date(now.getTime() + expirationTime));
158 }
159
160 lockPersistence.update(lock, false);
161
162 return lock;
163 }
164
165 public Lock refresh(String uuid, long expirationTime)
166 throws PortalException, SystemException {
167
168 Date now = new Date();
169
170 List<Lock> locks = lockPersistence.findByUuid(uuid);
171
172 Lock lock = null;
173
174 if (locks.size() > 0) {
175 lock = locks.get(0);
176 }
177 else {
178 throw new NoSuchLockException();
179 }
180
181 lock.setCreateDate(now);
182
183 if (expirationTime == 0) {
184 lock.setExpirationDate(null);
185 }
186 else {
187 lock.setExpirationDate(new Date(now.getTime() + expirationTime));
188 }
189
190 lockPersistence.update(lock, false);
191
192 return lock;
193 }
194
195 public void unlock(String className, long key) throws SystemException {
196 unlock(className, String.valueOf(key));
197 }
198
199 public void unlock(String className, String key) throws SystemException {
200 try {
201 lockPersistence.removeByC_K(className, key);
202 }
203 catch (NoSuchLockException nsle) {
204 }
205 }
206
207 }