1
19
20 package com.liferay.portlet.documentlibrary.service.impl;
21
22 import com.liferay.lock.ExpiredLockException;
23 import com.liferay.lock.InvalidLockException;
24 import com.liferay.lock.NoSuchLockException;
25 import com.liferay.lock.model.Lock;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.ListUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.security.permission.ActionKeys;
31 import com.liferay.portal.service.ServiceContext;
32 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
34 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
35 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
36 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
37 import com.liferay.portlet.documentlibrary.util.DLUtil;
38
39 import java.io.File;
40
41 import java.util.Iterator;
42 import java.util.List;
43
44
50 public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
51
52 public DLFileEntry addFileEntry(
53 long folderId, String name, String title, String description,
54 String extraSettings, File file, ServiceContext serviceContext)
55 throws PortalException, SystemException {
56
57 DLFolderPermission.check(
58 getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
59
60 return dlFileEntryLocalService.addFileEntry(
61 getUserId(), folderId, name, title, description, extraSettings,
62 file, serviceContext);
63 }
64
65 public DLFileEntry addFileEntry(
66 long folderId, String name, String title, String description,
67 String extraSettings, byte[] bytes, ServiceContext serviceContext)
68 throws PortalException, SystemException {
69
70 DLFolderPermission.check(
71 getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
72
73 return dlFileEntryLocalService.addFileEntry(
74 getUserId(), folderId, name, title, description, extraSettings,
75 bytes, serviceContext);
76 }
77
78 public void deleteFileEntry(long folderId, String name)
79 throws PortalException, SystemException {
80
81 DLFileEntryPermission.check(
82 getPermissionChecker(), folderId, name, ActionKeys.DELETE);
83
84 boolean hasLock = hasFileEntryLock(folderId, name);
85
86 if (!hasLock) {
87
88
90 lockFileEntry(folderId, name);
91 }
92
93 try {
94 dlFileEntryLocalService.deleteFileEntry(folderId, name);
95 }
96 finally {
97 if (!hasLock) {
98
99
101 unlockFileEntry(folderId, name);
102 }
103 }
104 }
105
106 public void deleteFileEntry(long folderId, String name, double version)
107 throws PortalException, SystemException {
108
109 DLFileEntryPermission.check(
110 getPermissionChecker(), folderId, name, ActionKeys.DELETE);
111
112 boolean hasLock = hasFileEntryLock(folderId, name);
113
114 if (!hasLock) {
115
116
118 lockFileEntry(folderId, name);
119 }
120
121 try {
122 dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
123 }
124 finally {
125 if (!hasLock) {
126
127
129 unlockFileEntry(folderId, name);
130 }
131 }
132 }
133
134 public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
135 throws PortalException, SystemException {
136
137 DLFileEntry fileEntry = getFileEntryByTitle(
138 folderId, titleWithExtension);
139
140 deleteFileEntry(folderId, fileEntry.getName());
141 }
142
143 public List<DLFileEntry> getFileEntries(long folderId)
144 throws PortalException, SystemException {
145
146 List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
147 folderId);
148
149 fileEntries = ListUtil.copy(fileEntries);
150
151 Iterator<DLFileEntry> itr = fileEntries.iterator();
152
153 while (itr.hasNext()) {
154 DLFileEntry fileEntry = itr.next();
155
156 if (!DLFileEntryPermission.contains(
157 getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
158
159 itr.remove();
160 }
161 }
162
163 return fileEntries;
164 }
165
166 public DLFileEntry getFileEntry(long folderId, String name)
167 throws PortalException, SystemException {
168
169 DLFileEntryPermission.check(
170 getPermissionChecker(), folderId, name, ActionKeys.VIEW);
171
172 return dlFileEntryLocalService.getFileEntry(folderId, name);
173 }
174
175 public DLFileEntry getFileEntryByTitle(
176 long folderId, String titleWithExtension)
177 throws PortalException, SystemException {
178
179 DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
180 folderId, titleWithExtension);
181
182 DLFileEntryPermission.check(
183 getPermissionChecker(), fileEntry, ActionKeys.VIEW);
184
185 return fileEntry;
186 }
187
188 public boolean hasFileEntryLock(long folderId, String name)
189 throws PortalException {
190
191 String lockId = DLUtil.getLockId(folderId, name);
192
193 boolean hasLock = lockService.hasLock(
194 DLFileEntry.class.getName(), lockId, getUserId());
195
196 if (!hasLock) {
197 hasLock = dlFolderService.hasInheritableLock(folderId);
198 }
199
200 return hasLock;
201 }
202
203 public Lock lockFileEntry(long folderId, String name)
204 throws PortalException, SystemException {
205
206 return lockFileEntry(
207 folderId, name, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
208 }
209
210 public Lock lockFileEntry(
211 long folderId, String name, String owner, long expirationTime)
212 throws PortalException, SystemException {
213
214 if ((expirationTime <= 0) ||
215 (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
216
217 expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
218 }
219
220 String lockId = DLUtil.getLockId(folderId, name);
221
222 return lockService.lock(
223 DLFileEntry.class.getName(), lockId, getUser().getUserId(), owner,
224 expirationTime);
225 }
226
227 public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
228 throws PortalException {
229
230 return lockService.refresh(lockUuid, expirationTime);
231 }
232
233 public void unlockFileEntry(long folderId, String name) {
234 String lockId = DLUtil.getLockId(folderId, name);
235
236 lockService.unlock(DLFileEntry.class.getName(), lockId);
237 }
238
239 public void unlockFileEntry(long folderId, String name, String lockUuid)
240 throws PortalException {
241
242 String lockId = DLUtil.getLockId(folderId, name);
243
244 if (Validator.isNotNull(lockUuid)) {
245 try {
246 Lock lock = lockService.getLock(
247 DLFileEntry.class.getName(), lockId);
248
249 if (!lock.getUuid().equals(lockUuid)) {
250 throw new InvalidLockException("UUIDs do not match");
251 }
252 }
253 catch (PortalException pe) {
254 if (pe instanceof ExpiredLockException ||
255 pe instanceof NoSuchLockException) {
256 }
257 else {
258 throw pe;
259 }
260 }
261 }
262
263 lockService.unlock(DLFileEntry.class.getName(), lockId);
264 }
265
266 public DLFileEntry updateFileEntry(
267 long folderId, long newFolderId, String name, String sourceFileName,
268 String title, String description, String extraSettings,
269 byte[] bytes, ServiceContext serviceContext)
270 throws PortalException, SystemException {
271
272 DLFileEntryPermission.check(
273 getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
274
275 boolean hasLock = hasFileEntryLock(folderId, name);
276
277 if (!hasLock) {
278
279
281 lockFileEntry(folderId, name);
282 }
283
284 DLFileEntry fileEntry = null;
285
286 try {
287 fileEntry = dlFileEntryLocalService.updateFileEntry(
288 getUserId(), folderId, newFolderId, name, sourceFileName, title,
289 description, extraSettings, bytes, serviceContext);
290 }
291 finally {
292 if (!hasLock) {
293
294
296 unlockFileEntry(folderId, name);
297 }
298 }
299
300 return fileEntry;
301 }
302
303 public boolean verifyFileEntryLock(
304 long folderId, String name, String lockUuid)
305 throws PortalException {
306
307 boolean verified = false;
308
309 try {
310 String lockId = DLUtil.getLockId(folderId, name);
311
312 Lock lock = lockService.getLock(
313 DLFileEntry.class.getName(), lockId);
314
315 if (lock.getUuid().equals(lockUuid)) {
316 verified = true;
317 }
318 }
319 catch (PortalException pe) {
320 if (pe instanceof ExpiredLockException ||
321 pe instanceof NoSuchLockException) {
322
323 verified = dlFolderService.verifyInheritableLock(
324 folderId, lockUuid);
325 }
326 else {
327 throw pe;
328 }
329 }
330
331 return verified;
332 }
333
334 }