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