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