1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
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.PortalException;
21  import com.liferay.portal.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.impl.DLFileEntryImpl;
29  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
30  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
31  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
32  import com.liferay.portlet.documentlibrary.util.DLUtil;
33  
34  import java.io.File;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="DLFileEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
45  
46      /**
47       * @deprecated
48       */
49      public DLFileEntry addFileEntry(
50              long folderId, String name, String title, String description,
51              String extraSettings, byte[] bytes, ServiceContext serviceContext)
52          throws PortalException, SystemException {
53  
54          return addFileEntry(
55              folderId, name, title, description, null, extraSettings, bytes,
56              serviceContext);
57      }
58  
59      /**
60       * @deprecated
61       */
62      public DLFileEntry addFileEntry(
63              long folderId, String name, String title, String description,
64              String extraSettings, File file, ServiceContext serviceContext)
65          throws PortalException, SystemException {
66  
67          return addFileEntry(
68              folderId, name, title, description, null, extraSettings, file,
69              serviceContext);
70      }
71  
72      public DLFileEntry addFileEntry(
73              long folderId, String name, String title, String description,
74              String versionDescription, String extraSettings, byte[] bytes,
75              ServiceContext serviceContext)
76          throws PortalException, SystemException {
77  
78          DLFolderPermission.check(
79              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
80  
81          return dlFileEntryLocalService.addFileEntry(
82              null, getUserId(), folderId, name, title, description,
83              versionDescription, extraSettings, bytes, serviceContext);
84      }
85  
86      public DLFileEntry addFileEntry(
87              long folderId, String name, String title, String description,
88              String versionDescription, String extraSettings, File file,
89              ServiceContext serviceContext)
90          throws PortalException, SystemException {
91  
92          DLFolderPermission.check(
93              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
94  
95          return dlFileEntryLocalService.addFileEntry(
96              null, getUserId(), folderId, name, title, description,
97              versionDescription, extraSettings, file, serviceContext);
98      }
99  
100     public void deleteFileEntry(long folderId, String name)
101         throws PortalException, SystemException {
102 
103         DLFileEntryPermission.check(
104             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
105 
106         boolean hasLock = hasFileEntryLock(folderId, name);
107 
108         if (!hasLock) {
109 
110             // Lock
111 
112             lockFileEntry(folderId, name);
113         }
114 
115         try {
116             dlFileEntryLocalService.deleteFileEntry(folderId, name);
117         }
118         finally {
119             if (!hasLock) {
120 
121                 // Unlock
122 
123                 unlockFileEntry(folderId, name);
124             }
125         }
126     }
127 
128     public void deleteFileEntry(long folderId, String name, double version)
129         throws PortalException, SystemException {
130 
131         DLFileEntryPermission.check(
132             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
133 
134         boolean hasLock = hasFileEntryLock(folderId, name);
135 
136         if (!hasLock) {
137 
138             // Lock
139 
140             lockFileEntry(folderId, name);
141         }
142 
143         try {
144             dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
145         }
146         finally {
147             if (!hasLock) {
148 
149                 // Unlock
150 
151                 unlockFileEntry(folderId, name);
152             }
153         }
154     }
155 
156     public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
157         throws PortalException, SystemException {
158 
159         DLFileEntry fileEntry = getFileEntryByTitle(
160             folderId, titleWithExtension);
161 
162         deleteFileEntry(folderId, fileEntry.getName());
163     }
164 
165     public List<DLFileEntry> getFileEntries(long folderId)
166         throws PortalException, SystemException {
167 
168         List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
169             folderId);
170 
171         fileEntries = ListUtil.copy(fileEntries);
172 
173         Iterator<DLFileEntry> itr = fileEntries.iterator();
174 
175         while (itr.hasNext()) {
176             DLFileEntry fileEntry = itr.next();
177 
178             if (!DLFileEntryPermission.contains(
179                     getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
180 
181                 itr.remove();
182             }
183         }
184 
185         return fileEntries;
186     }
187 
188     public DLFileEntry getFileEntry(long folderId, String name)
189         throws PortalException, SystemException {
190 
191         DLFileEntryPermission.check(
192             getPermissionChecker(), folderId, name, ActionKeys.VIEW);
193 
194         return dlFileEntryLocalService.getFileEntry(folderId, name);
195     }
196 
197     public DLFileEntry getFileEntryByTitle(
198             long folderId, String titleWithExtension)
199         throws PortalException, SystemException {
200 
201         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
202             folderId, titleWithExtension);
203 
204         DLFileEntryPermission.check(
205             getPermissionChecker(), fileEntry, ActionKeys.VIEW);
206 
207         return fileEntry;
208     }
209 
210     public boolean hasFileEntryLock(long folderId, String name)
211         throws PortalException, SystemException {
212 
213         String lockId = DLUtil.getLockId(folderId, name);
214 
215         boolean hasLock = lockLocalService.hasLock(
216             getUserId(), DLFileEntry.class.getName(), lockId);
217 
218         if (!hasLock) {
219             hasLock = dlFolderService.hasInheritableLock(folderId);
220         }
221 
222         return hasLock;
223     }
224 
225     public Lock lockFileEntry(long folderId, String name)
226         throws PortalException, SystemException {
227 
228         return lockFileEntry(
229             folderId, name, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
230     }
231 
232     public Lock lockFileEntry(
233             long folderId, String name, String owner, long expirationTime)
234         throws PortalException, SystemException {
235 
236         if ((expirationTime <= 0) ||
237             (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
238 
239             expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
240         }
241 
242         String lockId = DLUtil.getLockId(folderId, name);
243 
244         return lockLocalService.lock(
245             getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
246             false, System.currentTimeMillis() + expirationTime);
247     }
248 
249     public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
250         throws PortalException, SystemException {
251 
252         return lockLocalService.refresh(lockUuid, expirationTime);
253     }
254 
255     public void unlockFileEntry(long folderId, String name)
256         throws SystemException {
257 
258         String lockId = DLUtil.getLockId(folderId, name);
259 
260         lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
261     }
262 
263     public void unlockFileEntry(long folderId, String name, String lockUuid)
264         throws PortalException, SystemException {
265 
266         String lockId = DLUtil.getLockId(folderId, name);
267 
268         if (Validator.isNotNull(lockUuid)) {
269             try {
270                 Lock lock = lockLocalService.getLock(
271                     DLFileEntry.class.getName(), lockId);
272 
273                 if (!lock.getUuid().equals(lockUuid)) {
274                     throw new InvalidLockException("UUIDs do not match");
275                 }
276             }
277             catch (PortalException pe) {
278                 if (pe instanceof ExpiredLockException ||
279                     pe instanceof NoSuchLockException) {
280                 }
281                 else {
282                     throw pe;
283                 }
284             }
285         }
286 
287         lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
288     }
289 
290     /**
291      * @deprecated
292      */
293     public DLFileEntry updateFileEntry(
294             long folderId, long newFolderId, String name, String sourceFileName,
295             String title, String description, String extraSettings,
296             byte[] bytes, ServiceContext serviceContext)
297         throws PortalException, SystemException {
298 
299         return updateFileEntry(
300             folderId, newFolderId, name, sourceFileName, title, description,
301             null, extraSettings, bytes, serviceContext);
302     }
303 
304     /**
305      * @deprecated
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         return updateFileEntry(
314             folderId, newFolderId, name, sourceFileName, title, description,
315             null, extraSettings, file, serviceContext);
316     }
317 
318     public DLFileEntry updateFileEntry(
319             long folderId, long newFolderId, String name, String sourceFileName,
320             String title, String description, String versionDescription,
321             String extraSettings, byte[] bytes, ServiceContext serviceContext)
322         throws PortalException, SystemException {
323 
324         DLFileEntryPermission.check(
325             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
326 
327         boolean hasLock = hasFileEntryLock(folderId, name);
328 
329         if (!hasLock) {
330 
331             // Lock
332 
333             lockFileEntry(folderId, name);
334         }
335 
336         DLFileEntry fileEntry = null;
337 
338         try {
339             fileEntry = dlFileEntryLocalService.updateFileEntry(
340                 getUserId(), folderId, newFolderId, name, sourceFileName, title,
341                 description, extraSettings, bytes, serviceContext);
342         }
343         finally {
344             if (!hasLock) {
345 
346                 // Unlock
347 
348                 unlockFileEntry(folderId, name);
349             }
350         }
351 
352         return fileEntry;
353     }
354 
355     public DLFileEntry updateFileEntry(
356             long folderId, long newFolderId, String name, String sourceFileName,
357             String title, String description, String versionDescription,
358             String extraSettings, File file, ServiceContext serviceContext)
359         throws PortalException, SystemException {
360 
361         DLFileEntryPermission.check(
362             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
363 
364         boolean hasLock = hasFileEntryLock(folderId, name);
365 
366         if (!hasLock) {
367 
368             // Lock
369 
370             lockFileEntry(folderId, name);
371         }
372 
373         DLFileEntry fileEntry = null;
374 
375         try {
376             fileEntry = dlFileEntryLocalService.updateFileEntry(
377                 getUserId(), folderId, newFolderId, name, sourceFileName, title,
378                 description, extraSettings, file, serviceContext);
379         }
380         finally {
381             if (!hasLock) {
382 
383                 // Unlock
384 
385                 unlockFileEntry(folderId, name);
386             }
387         }
388 
389         return fileEntry;
390     }
391 
392     public boolean verifyFileEntryLock(
393             long folderId, String name, String lockUuid)
394         throws PortalException, SystemException {
395 
396         boolean verified = false;
397 
398         try {
399             String lockId = DLUtil.getLockId(folderId, name);
400 
401             Lock lock = lockLocalService.getLock(
402                 DLFileEntry.class.getName(), lockId);
403 
404             if (lock.getUuid().equals(lockUuid)) {
405                 verified = true;
406             }
407         }
408         catch (PortalException pe) {
409             if (pe instanceof ExpiredLockException ||
410                 pe instanceof NoSuchLockException) {
411 
412                 verified = dlFolderService.verifyInheritableLock(
413                     folderId, lockUuid);
414             }
415             else {
416                 throw pe;
417             }
418         }
419 
420         return verified;
421     }
422 
423 }