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