1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.lock.ExpiredLockException;
26  import com.liferay.lock.InvalidLockException;
27  import com.liferay.lock.NoSuchLockException;
28  import com.liferay.lock.model.Lock;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.SystemException;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.security.permission.ActionKeys;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
36  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
37  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
38  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
39  import com.liferay.portlet.documentlibrary.util.DLUtil;
40  
41  import java.io.File;
42  
43  import java.rmi.RemoteException;
44  
45  import java.util.Iterator;
46  import java.util.List;
47  
48  /**
49   * <a href="DLFileEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
55  
56      public DLFileEntry addFileEntry(
57              long folderId, String name, String title, String description,
58              String[] tagsEntries, String extraSettings, File file,
59              boolean addCommunityPermissions, boolean addGuestPermissions)
60          throws PortalException, SystemException {
61  
62          DLFolderPermission.check(
63              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
64  
65          return dlFileEntryLocalService.addFileEntry(
66              getUserId(), folderId, name, title, description, tagsEntries,
67              extraSettings, file, addCommunityPermissions,
68              addGuestPermissions);
69      }
70  
71      public DLFileEntry addFileEntry(
72              long folderId, String name, String title, String description,
73              String[] tagsEntries, String extraSettings, byte[] bytes,
74              boolean addCommunityPermissions, boolean addGuestPermissions)
75          throws PortalException, SystemException {
76  
77          DLFolderPermission.check(
78              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
79  
80          return dlFileEntryLocalService.addFileEntry(
81              getUserId(), folderId, name, title, description, tagsEntries,
82              extraSettings, bytes, addCommunityPermissions,
83              addGuestPermissions);
84      }
85  
86      public DLFileEntry addFileEntry(
87              long folderId, String name, String title, String description,
88              String[] tagsEntries, String extraSettings, File file,
89              String[] communityPermissions, String[] guestPermissions)
90          throws PortalException, SystemException {
91  
92          DLFolderPermission.check(
93              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
94  
95          return dlFileEntryLocalService.addFileEntry(
96              getUserId(), folderId, name, title, description, tagsEntries,
97              extraSettings, file, communityPermissions, guestPermissions);
98      }
99  
100     public DLFileEntry addFileEntry(
101             long folderId, String name, String title, String description,
102             String[] tagsEntries, String extraSettings, byte[] bytes,
103             String[] communityPermissions, String[] guestPermissions)
104         throws PortalException, SystemException {
105 
106         DLFolderPermission.check(
107             getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
108 
109         return dlFileEntryLocalService.addFileEntry(
110             getUserId(), folderId, name, title, description, tagsEntries,
111             extraSettings, bytes, communityPermissions, guestPermissions);
112     }
113 
114     public void deleteFileEntry(long folderId, String name)
115         throws PortalException, RemoteException, SystemException {
116 
117         User user = getUser();
118 
119         DLFileEntryPermission.check(
120             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
121 
122         String lockId = DLUtil.getLockId(folderId, name);
123 
124         boolean alreadyHasLock = lockService.hasLock(
125             DLFileEntry.class.getName(), lockId, user.getUserId());
126 
127         if (!alreadyHasLock) {
128 
129             // Lock
130 
131             lockService.lock(
132                 DLFileEntry.class.getName(), lockId,
133                 user.getUserId(), null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
134         }
135 
136         try {
137             dlFileEntryLocalService.deleteFileEntry(folderId, name);
138         }
139         finally {
140             if (!alreadyHasLock) {
141 
142                 // Unlock
143 
144                 lockService.unlock(DLFileEntry.class.getName(), lockId);
145             }
146         }
147     }
148 
149     public void deleteFileEntry(long folderId, String name, double version)
150         throws PortalException, RemoteException, SystemException {
151 
152         User user = getUser();
153 
154         DLFileEntryPermission.check(
155             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
156 
157         String lockId = DLUtil.getLockId(folderId, name);
158 
159         boolean alreadyHasLock = lockService.hasLock(
160             DLFileEntry.class.getName(), lockId, user.getUserId());
161 
162         if (!alreadyHasLock) {
163 
164             // Lock
165 
166             lockService.lock(
167                 DLFileEntry.class.getName(), lockId,
168                 user.getUserId(), null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
169         }
170 
171         try {
172             dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
173         }
174         finally {
175             if (!alreadyHasLock) {
176 
177                 // Unlock
178 
179                 lockService.unlock(DLFileEntry.class.getName(), lockId);
180             }
181         }
182     }
183 
184     public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
185         throws PortalException, RemoteException, SystemException {
186 
187         DLFileEntry fileEntry = getFileEntryByTitle(
188             folderId, titleWithExtension);
189 
190         deleteFileEntry(folderId, fileEntry.getName());
191     }
192 
193     public List<DLFileEntry> getFileEntries(long folderId)
194         throws PortalException, SystemException {
195 
196         List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
197             folderId);
198 
199         Iterator<DLFileEntry> itr = fileEntries.iterator();
200 
201         while (itr.hasNext()) {
202             DLFileEntry fileEntry = itr.next();
203 
204             if (!DLFileEntryPermission.contains(
205                     getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
206 
207                 itr.remove();
208             }
209         }
210 
211         return fileEntries;
212     }
213 
214     public DLFileEntry getFileEntry(long folderId, String name)
215         throws PortalException, SystemException {
216 
217         DLFileEntryPermission.check(
218             getPermissionChecker(), folderId, name, ActionKeys.VIEW);
219 
220         return dlFileEntryLocalService.getFileEntry(folderId, name);
221     }
222 
223     public DLFileEntry getFileEntryByTitle(
224             long folderId, String titleWithExtension)
225         throws PortalException, SystemException {
226 
227         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
228             folderId, titleWithExtension);
229 
230         DLFileEntryPermission.check(
231             getPermissionChecker(), fileEntry, ActionKeys.VIEW);
232 
233         return fileEntry;
234     }
235 
236     public Lock getFileEntryLock(long folderId, String name)
237         throws PortalException, RemoteException {
238 
239         String lockId = DLUtil.getLockId(folderId, name);
240 
241         return lockService.getLock(DLFileEntry.class.getName(), lockId);
242     }
243 
244     public Lock lockFileEntry(long folderId, String name)
245         throws PortalException, RemoteException, SystemException {
246 
247         return lockFileEntry(
248             folderId, name, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
249     }
250 
251     public Lock lockFileEntry(
252             long folderId, String name, String owner, long expirationTime)
253         throws PortalException, RemoteException, SystemException {
254 
255         if ((expirationTime <= 0) ||
256             (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
257 
258             expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
259         }
260 
261         String lockId = DLUtil.getLockId(folderId, name);
262 
263         return lockService.lock(
264             DLFileEntry.class.getName(), lockId, getUser().getUserId(), owner,
265             expirationTime);
266     }
267 
268     public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
269         throws PortalException, RemoteException {
270 
271         return lockService.refresh(lockUuid, expirationTime);
272     }
273 
274     public void unlockFileEntry(long folderId, String name)
275         throws RemoteException {
276 
277         String lockId = DLUtil.getLockId(folderId, name);
278 
279         lockService.unlock(DLFileEntry.class.getName(), lockId);
280     }
281 
282     public void unlockFileEntry(long folderId, String name, String lockUuid)
283         throws PortalException, RemoteException {
284 
285         String lockId = DLUtil.getLockId(folderId, name);
286 
287         if (Validator.isNotNull(lockUuid)) {
288             try {
289                 Lock lock = lockService.getLock(
290                     DLFileEntry.class.getName(), lockId);
291 
292                 if (!lock.getUuid().equals(lockUuid)) {
293                     throw new InvalidLockException("UUIDs do not match");
294                 }
295             }
296             catch (PortalException pe) {
297                 if (pe instanceof ExpiredLockException ||
298                     pe instanceof NoSuchLockException) {
299                 }
300                 else {
301                     throw pe;
302                 }
303             }
304         }
305 
306         lockService.unlock(DLFileEntry.class.getName(), lockId);
307     }
308 
309     public DLFileEntry updateFileEntry(
310             long folderId, long newFolderId, String name, String sourceFileName,
311             String title, String description, String[] tagsEntries,
312             String extraSettings, byte[] bytes)
313         throws PortalException, RemoteException, SystemException {
314 
315         User user = getUser();
316 
317         DLFileEntryPermission.check(
318             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
319 
320         String lockId = DLUtil.getLockId(folderId, name);
321 
322         boolean alreadyHasLock = lockService.hasLock(
323             DLFileEntry.class.getName(), lockId, user.getUserId());
324 
325         if (!alreadyHasLock) {
326 
327             // Lock
328 
329             lockService.lock(
330                 DLFileEntry.class.getName(), lockId,
331                 user.getUserId(), null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
332         }
333 
334         DLFileEntry fileEntry = null;
335 
336         try {
337             fileEntry = dlFileEntryLocalService.updateFileEntry(
338                 getUserId(), folderId, newFolderId, name, sourceFileName, title,
339                 description, tagsEntries, extraSettings, bytes);
340         }
341         finally {
342             if (!alreadyHasLock) {
343 
344                 // Unlock
345 
346                 lockService.unlock(DLFileEntry.class.getName(), lockId);
347             }
348         }
349 
350         return fileEntry;
351     }
352 
353 }