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