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.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.FileUtil;
25  import com.liferay.portal.kernel.util.ListUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.Lock;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
34  import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
35  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
36  
37  import java.io.File;
38  import java.io.InputStream;
39  
40  import java.rmi.RemoteException;
41  
42  import java.util.HashSet;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Set;
46  
47  /**
48   * <a href="DLFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
53  
54      public DLFolder addFolder(
55              long groupId, long parentFolderId, String name, String description,
56              ServiceContext serviceContext)
57          throws PortalException, SystemException {
58  
59          DLFolderPermission.check(
60              getPermissionChecker(), groupId, parentFolderId,
61              ActionKeys.ADD_FOLDER);
62  
63          return dlFolderLocalService.addFolder(
64              getUserId(), groupId, parentFolderId, name, description,
65              serviceContext);
66      }
67  
68      public DLFolder copyFolder(
69              long groupId, long sourceFolderId, long parentFolderId, String name,
70              String description, ServiceContext serviceContext)
71          throws PortalException, RemoteException, SystemException {
72  
73          DLFolder srcFolder = getFolder(sourceFolderId);
74  
75          DLFolder destFolder = addFolder(
76              groupId, parentFolderId, name, description, serviceContext);
77  
78          copyFolder(srcFolder, destFolder, serviceContext);
79  
80          return destFolder;
81      }
82  
83      public void deleteFolder(long folderId)
84          throws PortalException, RemoteException, SystemException {
85  
86          DLFolderPermission.check(
87              getPermissionChecker(), folderId, ActionKeys.DELETE);
88  
89          boolean hasLock = lockLocalService.hasLock(
90              getUserId(), DLFolder.class.getName(), folderId);
91  
92          Lock lock = null;
93  
94          if (!hasLock) {
95  
96              // Lock
97  
98              lock = lockFolder(folderId);
99          }
100 
101         try {
102             dlFolderLocalService.deleteFolder(folderId);
103         }
104         finally {
105             if (!hasLock) {
106 
107                 // Unlock
108 
109                 unlockFolder(folderId, lock.getUuid());
110             }
111         }
112     }
113 
114     public void deleteFolder(long groupId, long parentFolderId, String name)
115         throws PortalException, RemoteException, SystemException {
116 
117         long folderId = getFolderId(groupId, parentFolderId, name);
118 
119         deleteFolder(folderId);
120     }
121 
122     public DLFolder getFolder(long folderId)
123         throws PortalException, SystemException {
124 
125         DLFolderPermission.check(
126             getPermissionChecker(), folderId, ActionKeys.VIEW);
127 
128         return dlFolderLocalService.getFolder(folderId);
129     }
130 
131     public DLFolder getFolder(long groupId, long parentFolderId, String name)
132         throws PortalException, SystemException {
133 
134         DLFolder folder = dlFolderLocalService.getFolder(
135             groupId, parentFolderId, name);
136 
137         DLFolderPermission.check(
138             getPermissionChecker(), folder, ActionKeys.VIEW);
139 
140         return folder;
141     }
142 
143     public long getFolderId(long groupId, long parentFolderId, String name)
144         throws PortalException, SystemException {
145 
146         DLFolder folder = getFolder(groupId, parentFolderId, name);
147 
148         return folder.getFolderId();
149     }
150 
151     public List<DLFolder> getFolders(long groupId, long parentFolderId)
152         throws PortalException, SystemException {
153 
154         List<DLFolder> folders = dlFolderLocalService.getFolders(
155             groupId, parentFolderId);
156 
157         folders = ListUtil.copy(folders);
158 
159         Iterator<DLFolder> itr = folders.iterator();
160 
161         while (itr.hasNext()) {
162             DLFolder folder = itr.next();
163 
164             if (!DLFolderPermission.contains(
165                     getPermissionChecker(), folder.getFolderId(),
166                     ActionKeys.VIEW)) {
167 
168                 itr.remove();
169             }
170         }
171 
172         return folders;
173     }
174 
175     public boolean hasInheritableLock(long folderId)
176         throws PortalException, SystemException {
177 
178         boolean inheritable = false;
179 
180         try {
181             Lock lock = lockLocalService.getLock(
182                 DLFolder.class.getName(), folderId);
183 
184             inheritable = lock.isInheritable();
185         }
186         catch (ExpiredLockException ele) {
187         }
188         catch (NoSuchLockException nsle) {
189         }
190 
191         return inheritable;
192     }
193 
194     public Lock lockFolder(long folderId)
195         throws PortalException, RemoteException, SystemException {
196 
197         return lockFolder(
198             folderId, null, false, DLFolderImpl.LOCK_EXPIRATION_TIME);
199     }
200 
201     public Lock lockFolder(
202             long folderId, String owner, boolean inheritable,
203             long expirationTime)
204         throws PortalException, RemoteException, SystemException {
205 
206         if ((expirationTime <= 0) ||
207             (expirationTime > DLFolderImpl.LOCK_EXPIRATION_TIME)) {
208 
209             expirationTime = DLFolderImpl.LOCK_EXPIRATION_TIME;
210         }
211 
212         Lock lock = lockLocalService.lock(
213             getUser().getUserId(), DLFolder.class.getName(), folderId, owner,
214             inheritable, expirationTime);
215 
216         Set<String> fileNames = new HashSet<String>();
217 
218         try {
219             List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
220                 folderId);
221 
222             for (DLFileEntry fileEntry : fileEntries) {
223                 dlFileEntryService.lockFileEntry(
224                     folderId, fileEntry.getName(), owner, expirationTime);
225 
226                 fileNames.add(fileEntry.getName());
227             }
228         }
229         catch (Exception e) {
230             for (String fileName : fileNames) {
231                 dlFileEntryService.unlockFileEntry(folderId, fileName);
232             }
233 
234             unlockFolder(folderId, lock.getUuid());
235 
236             if (e instanceof PortalException) {
237                 throw (PortalException)e;
238             }
239             else if (e instanceof RemoteException) {
240                 throw (RemoteException)e;
241             }
242             else if (e instanceof SystemException) {
243                 throw (SystemException)e;
244             }
245             else {
246                 throw new PortalException(e);
247             }
248         }
249 
250         return lock;
251     }
252 
253     public Lock refreshFolderLock(String lockUuid, long expirationTime)
254         throws PortalException, SystemException {
255 
256         return lockLocalService.refresh(lockUuid, expirationTime);
257     }
258 
259     public void reIndexSearch(long companyId)
260         throws PortalException, SystemException {
261 
262         if (!getPermissionChecker().isOmniadmin()) {
263             throw new PrincipalException();
264         }
265 
266         String[] ids = new String[] {String.valueOf(companyId)};
267 
268         dlFolderLocalService.reIndex(ids);
269     }
270 
271     public void unlockFolder(
272             long groupId, long parentFolderId, String name, String lockUuid)
273         throws PortalException, SystemException {
274 
275         long folderId = getFolderId(groupId, parentFolderId, name);
276 
277         unlockFolder(folderId, lockUuid);
278     }
279 
280     public void unlockFolder(long folderId, String lockUuid)
281         throws PortalException, SystemException {
282 
283         if (Validator.isNotNull(lockUuid)) {
284             try {
285                 Lock lock = lockLocalService.getLock(
286                     DLFolder.class.getName(), folderId);
287 
288                 if (!lock.getUuid().equals(lockUuid)) {
289                     throw new InvalidLockException("UUIDs do not match");
290                 }
291             }
292             catch (PortalException pe) {
293                 if (pe instanceof ExpiredLockException ||
294                     pe instanceof NoSuchLockException) {
295                 }
296                 else {
297                     throw pe;
298                 }
299             }
300         }
301 
302         lockLocalService.unlock(DLFolder.class.getName(), folderId);
303 
304         try {
305             List<DLFileEntry> fileEntries = dlFileEntryService.getFileEntries(
306                 folderId);
307 
308             for (DLFileEntry fileEntry : fileEntries) {
309                 dlFileEntryService.unlockFileEntry(
310                     folderId, fileEntry.getName());
311             }
312         }
313         catch (Exception e) {
314             _log.error(e, e);
315         }
316     }
317 
318     public DLFolder updateFolder(
319             long folderId, long parentFolderId, String name, String description,
320             ServiceContext serviceContext)
321         throws PortalException, RemoteException, SystemException {
322 
323         DLFolderPermission.check(
324             getPermissionChecker(), folderId, ActionKeys.UPDATE);
325 
326         boolean hasLock = lockLocalService.hasLock(
327             getUserId(), DLFolder.class.getName(), folderId);
328 
329         Lock lock = null;
330 
331         if (!hasLock) {
332 
333             // Lock
334 
335             lock = lockFolder(folderId);
336         }
337 
338         try {
339             return dlFolderLocalService.updateFolder(
340                 folderId, parentFolderId, name, description, serviceContext);
341         }
342         finally {
343             if (!hasLock) {
344 
345                 // Unlock
346 
347                 unlockFolder(folderId, lock.getUuid());
348             }
349         }
350     }
351 
352     public boolean verifyInheritableLock(long folderId, String lockUuid)
353         throws PortalException, SystemException {
354 
355         boolean verified = false;
356 
357         try {
358             Lock lock = lockLocalService.getLock(
359                 DLFolder.class.getName(), folderId);
360 
361             if (!lock.isInheritable()) {
362                 throw new NoSuchLockException();
363             }
364 
365             if (lock.getUuid().equals(lockUuid)) {
366                 verified = true;
367             }
368         }
369         catch (ExpiredLockException ele) {
370             throw new NoSuchLockException(ele);
371         }
372 
373         return verified;
374     }
375 
376     protected void copyFolder(
377             DLFolder srcFolder, DLFolder destFolder,
378             ServiceContext serviceContext)
379         throws PortalException, RemoteException, SystemException {
380 
381         List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
382             srcFolder.getFolderId());
383 
384         for (DLFileEntry srcFileEntry : srcFileEntries) {
385             String name = srcFileEntry.getName();
386             String title = srcFileEntry.getTitleWithExtension();
387             String description = srcFileEntry.getDescription();
388             String extraSettings = srcFileEntry.getExtraSettings();
389 
390             File file = null;
391 
392             try {
393                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
394 
395                 InputStream is = dlLocalService.getFileAsStream(
396                     srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
397 
398                 FileUtil.write(file, is);
399             }
400             catch (Exception e) {
401                 _log.error(e, e);
402 
403                 continue;
404             }
405 
406             dlFileEntryService.addFileEntry(
407                 destFolder.getFolderId(), name, title, description,
408                 extraSettings, file, serviceContext);
409 
410             file.delete();
411         }
412 
413         List<DLFolder> srcSubfolders = getFolders(
414             srcFolder.getGroupId(), srcFolder.getFolderId());
415 
416         for (DLFolder srcSubfolder : srcSubfolders) {
417             String name = srcSubfolder.getName();
418             String description = srcSubfolder.getDescription();
419 
420             DLFolder destSubfolder = addFolder(
421                 destFolder.getGroupId(), destFolder.getFolderId(), name,
422                 description, serviceContext);
423 
424             copyFolder(srcSubfolder, destSubfolder, serviceContext);
425         }
426     }
427 
428     private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
429 
430 }