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