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