1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.impl;
21  
22  import com.liferay.lock.ExpiredLockException;
23  import com.liferay.lock.InvalidLockException;
24  import com.liferay.lock.NoSuchLockException;
25  import com.liferay.lock.model.Lock;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.ListUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
34  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
35  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
36  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
37  import com.liferay.portlet.documentlibrary.util.DLUtil;
38  
39  import java.io.File;
40  
41  import java.rmi.RemoteException;
42  
43  import java.util.Iterator;
44  import java.util.List;
45  
46  /**
47   * <a href="DLFileEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
53  
54      public DLFileEntry addFileEntry(
55              long folderId, String name, String title, String description,
56              String[] tagsEntries, String extraSettings, File file,
57              boolean addCommunityPermissions, boolean addGuestPermissions)
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, tagsEntries,
65              extraSettings, file, addCommunityPermissions,
66              addGuestPermissions);
67      }
68  
69      public DLFileEntry addFileEntry(
70              long folderId, String name, String title, String description,
71              String[] tagsEntries, String extraSettings, byte[] bytes,
72              boolean addCommunityPermissions, boolean addGuestPermissions)
73          throws PortalException, SystemException {
74  
75          DLFolderPermission.check(
76              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
77  
78          return dlFileEntryLocalService.addFileEntry(
79              getUserId(), folderId, name, title, description, tagsEntries,
80              extraSettings, bytes, addCommunityPermissions,
81              addGuestPermissions);
82      }
83  
84      public DLFileEntry addFileEntry(
85              long folderId, String name, String title, String description,
86              String[] tagsEntries, String extraSettings, File file,
87              String[] communityPermissions, String[] guestPermissions)
88          throws PortalException, SystemException {
89  
90          DLFolderPermission.check(
91              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
92  
93          return dlFileEntryLocalService.addFileEntry(
94              getUserId(), folderId, name, title, description, tagsEntries,
95              extraSettings, file, communityPermissions, guestPermissions);
96      }
97  
98      public DLFileEntry addFileEntry(
99              long folderId, String name, String title, String description,
100             String[] tagsEntries, String extraSettings, byte[] bytes,
101             String[] communityPermissions, String[] guestPermissions)
102         throws PortalException, SystemException {
103 
104         DLFolderPermission.check(
105             getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
106 
107         return dlFileEntryLocalService.addFileEntry(
108             getUserId(), folderId, name, title, description, tagsEntries,
109             extraSettings, bytes, communityPermissions, guestPermissions);
110     }
111 
112     public void deleteFileEntry(long folderId, String name)
113         throws PortalException, RemoteException, SystemException {
114 
115         User user = getUser();
116 
117         DLFileEntryPermission.check(
118             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
119 
120         String lockId = DLUtil.getLockId(folderId, name);
121 
122         boolean alreadyHasLock = lockService.hasLock(
123             DLFileEntry.class.getName(), lockId, user.getUserId());
124 
125         if (!alreadyHasLock) {
126 
127             // Lock
128 
129             lockService.lock(
130                 DLFileEntry.class.getName(), lockId,
131                 user.getUserId(), null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
132         }
133 
134         try {
135             dlFileEntryLocalService.deleteFileEntry(folderId, name);
136         }
137         finally {
138             if (!alreadyHasLock) {
139 
140                 // Unlock
141 
142                 lockService.unlock(DLFileEntry.class.getName(), lockId);
143             }
144         }
145     }
146 
147     public void deleteFileEntry(long folderId, String name, double version)
148         throws PortalException, RemoteException, SystemException {
149 
150         User user = getUser();
151 
152         DLFileEntryPermission.check(
153             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
154 
155         String lockId = DLUtil.getLockId(folderId, name);
156 
157         boolean alreadyHasLock = lockService.hasLock(
158             DLFileEntry.class.getName(), lockId, user.getUserId());
159 
160         if (!alreadyHasLock) {
161 
162             // Lock
163 
164             lockService.lock(
165                 DLFileEntry.class.getName(), lockId,
166                 user.getUserId(), null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
167         }
168 
169         try {
170             dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
171         }
172         finally {
173             if (!alreadyHasLock) {
174 
175                 // Unlock
176 
177                 lockService.unlock(DLFileEntry.class.getName(), lockId);
178             }
179         }
180     }
181 
182     public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
183         throws PortalException, RemoteException, SystemException {
184 
185         DLFileEntry fileEntry = getFileEntryByTitle(
186             folderId, titleWithExtension);
187 
188         deleteFileEntry(folderId, fileEntry.getName());
189     }
190 
191     public List<DLFileEntry> getFileEntries(long folderId)
192         throws PortalException, SystemException {
193 
194         List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
195             folderId);
196 
197         fileEntries = ListUtil.copy(fileEntries);
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 }