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