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.util.ListUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Lock;
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  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 = lockLocalService.hasLock(
125             user.getUserId(), DLFileEntry.class.getName(), lockId);
126 
127         if (!alreadyHasLock) {
128 
129             // Lock
130 
131             lockLocalService.lock(
132                 user.getUserId(), DLFileEntry.class.getName(), lockId, null,
133                 false, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
134         }
135 
136         try {
137             dlFileEntryLocalService.deleteFileEntry(folderId, name);
138         }
139         finally {
140             if (!alreadyHasLock) {
141 
142                 // Unlock
143 
144                 lockLocalService.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 = lockLocalService.hasLock(
160             user.getUserId(), DLFileEntry.class.getName(), lockId);
161 
162         if (!alreadyHasLock) {
163 
164             // Lock
165 
166             lockLocalService.lock(
167                 user.getUserId(), DLFileEntry.class.getName(), lockId, null,
168                 false, 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                 lockLocalService.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         fileEntries = ListUtil.copy(fileEntries);
200 
201         Iterator<DLFileEntry> itr = fileEntries.iterator();
202 
203         while (itr.hasNext()) {
204             DLFileEntry fileEntry = itr.next();
205 
206             if (!DLFileEntryPermission.contains(
207                     getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
208 
209                 itr.remove();
210             }
211         }
212 
213         return fileEntries;
214     }
215 
216     public DLFileEntry getFileEntry(long folderId, String name)
217         throws PortalException, SystemException {
218 
219         DLFileEntryPermission.check(
220             getPermissionChecker(), folderId, name, ActionKeys.VIEW);
221 
222         return dlFileEntryLocalService.getFileEntry(folderId, name);
223     }
224 
225     public DLFileEntry getFileEntryByTitle(
226             long folderId, String titleWithExtension)
227         throws PortalException, SystemException {
228 
229         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
230             folderId, titleWithExtension);
231 
232         DLFileEntryPermission.check(
233             getPermissionChecker(), fileEntry, ActionKeys.VIEW);
234 
235         return fileEntry;
236     }
237 
238     public Lock getFileEntryLock(long folderId, String name)
239         throws PortalException, RemoteException, SystemException {
240 
241         String lockId = DLUtil.getLockId(folderId, name);
242 
243         return lockLocalService.getLock(DLFileEntry.class.getName(), lockId);
244     }
245 
246     public Lock lockFileEntry(long folderId, String name)
247         throws PortalException, RemoteException, SystemException {
248 
249         return lockFileEntry(
250             folderId, name, null, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
251     }
252 
253     public Lock lockFileEntry(
254             long folderId, String name, String owner, long expirationTime)
255         throws PortalException, RemoteException, SystemException {
256 
257         if ((expirationTime <= 0) ||
258             (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
259 
260             expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
261         }
262 
263         String lockId = DLUtil.getLockId(folderId, name);
264 
265         return lockLocalService.lock(
266             getUser().getUserId(), DLFileEntry.class.getName(), lockId, owner,
267             false, expirationTime);
268     }
269 
270     public Lock refreshFileEntryLock(String lockUuid, long expirationTime)
271         throws PortalException, RemoteException, SystemException {
272 
273         return lockLocalService.refresh(lockUuid, expirationTime);
274     }
275 
276     public void unlockFileEntry(long folderId, String name)
277         throws RemoteException, SystemException {
278 
279         String lockId = DLUtil.getLockId(folderId, name);
280 
281         lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
282     }
283 
284     public void unlockFileEntry(long folderId, String name, String lockUuid)
285         throws PortalException, RemoteException, SystemException {
286 
287         String lockId = DLUtil.getLockId(folderId, name);
288 
289         if (Validator.isNotNull(lockUuid)) {
290             try {
291                 Lock lock = lockLocalService.getLock(
292                     DLFileEntry.class.getName(), lockId);
293 
294                 if (!lock.getUuid().equals(lockUuid)) {
295                     throw new InvalidLockException("UUIDs do not match");
296                 }
297             }
298             catch (PortalException pe) {
299                 if (pe instanceof ExpiredLockException ||
300                     pe instanceof NoSuchLockException) {
301                 }
302                 else {
303                     throw pe;
304                 }
305             }
306         }
307 
308         lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
309     }
310 
311     public DLFileEntry updateFileEntry(
312             long folderId, long newFolderId, String name, String sourceFileName,
313             String title, String description, String[] tagsEntries,
314             String extraSettings, byte[] bytes)
315         throws PortalException, RemoteException, SystemException {
316 
317         User user = getUser();
318 
319         DLFileEntryPermission.check(
320             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
321 
322         String lockId = DLUtil.getLockId(folderId, name);
323 
324         boolean alreadyHasLock = lockLocalService.hasLock(
325             user.getUserId(), DLFileEntry.class.getName(), lockId);
326 
327         if (!alreadyHasLock) {
328 
329             // Lock
330 
331             lockLocalService.lock(
332                 user.getUserId(), DLFileEntry.class.getName(), lockId, null,
333                 false, DLFileEntryImpl.LOCK_EXPIRATION_TIME);
334         }
335 
336         DLFileEntry fileEntry = null;
337 
338         try {
339             fileEntry = dlFileEntryLocalService.updateFileEntry(
340                 getUserId(), folderId, newFolderId, name, sourceFileName, title,
341                 description, tagsEntries, extraSettings, bytes);
342         }
343         finally {
344             if (!alreadyHasLock) {
345 
346                 // Unlock
347 
348                 lockLocalService.unlock(DLFileEntry.class.getName(), lockId);
349             }
350         }
351 
352         return fileEntry;
353     }
354 
355 }