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.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
31  import com.liferay.portlet.documentlibrary.service.base.DLFileEntryServiceBaseImpl;
32  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
33  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
34  import com.liferay.portlet.documentlibrary.util.DLUtil;
35  
36  import java.io.File;
37  
38  import java.rmi.RemoteException;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="DLFileEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
50  
51      public DLFileEntry addFileEntry(
52              long folderId, String name, String title, String description,
53              String[] tagsEntries, String extraSettings, File file,
54              boolean addCommunityPermissions, boolean addGuestPermissions)
55          throws PortalException, SystemException {
56  
57          DLFolderPermission.check(
58              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
59  
60          return dlFileEntryLocalService.addFileEntry(
61              getUserId(), folderId, name, title, description, tagsEntries,
62              extraSettings, file, addCommunityPermissions,
63              addGuestPermissions);
64      }
65  
66      public DLFileEntry addFileEntry(
67              long folderId, String name, String title, String description,
68              String[] tagsEntries, String extraSettings, byte[] byteArray,
69              boolean addCommunityPermissions, boolean addGuestPermissions)
70          throws PortalException, SystemException {
71  
72          DLFolderPermission.check(
73              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
74  
75          return dlFileEntryLocalService.addFileEntry(
76              getUserId(), folderId, name, title, description, tagsEntries,
77              extraSettings, byteArray, addCommunityPermissions,
78              addGuestPermissions);
79      }
80  
81      public DLFileEntry addFileEntry(
82              long folderId, String name, String title, String description,
83              String[] tagsEntries, String extraSettings, File file,
84              String[] communityPermissions, String[] guestPermissions)
85          throws PortalException, SystemException {
86  
87          DLFolderPermission.check(
88              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
89  
90          return dlFileEntryLocalService.addFileEntry(
91              getUserId(), folderId, name, title, description, tagsEntries,
92              extraSettings, file, communityPermissions, guestPermissions);
93      }
94  
95      public DLFileEntry addFileEntry(
96              long folderId, String name, String title, String description,
97              String[] tagsEntries, String extraSettings, byte[] byteArray,
98              String[] communityPermissions, String[] guestPermissions)
99          throws PortalException, SystemException {
100 
101         DLFolderPermission.check(
102             getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
103 
104         return dlFileEntryLocalService.addFileEntry(
105             getUserId(), folderId, name, title, description, tagsEntries,
106             extraSettings, byteArray, communityPermissions, guestPermissions);
107     }
108 
109     public void deleteFileEntry(long folderId, String name)
110         throws PortalException, RemoteException, SystemException {
111 
112         User user = getUser();
113 
114         DLFileEntryPermission.check(
115             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
116 
117         String lockId = DLUtil.getLockId(folderId, name);
118 
119         boolean alreadyHasLock = lockService.hasLock(
120             DLFileEntry.class.getName(), lockId, user.getUserId());
121 
122         if (!alreadyHasLock) {
123 
124             // Lock
125 
126             lockService.lock(
127                 DLFileEntry.class.getName(), lockId, user.getCompanyId(),
128                 user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
129         }
130 
131         dlFileEntryLocalService.deleteFileEntry(folderId, name);
132 
133         if (!alreadyHasLock) {
134 
135             // Unlock
136 
137             lockService.unlock(DLFileEntry.class.getName(), lockId);
138         }
139     }
140 
141     public void deleteFileEntry(long folderId, String name, double version)
142         throws PortalException, RemoteException, SystemException {
143 
144         User user = getUser();
145 
146         DLFileEntryPermission.check(
147             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
148 
149         String lockId = DLUtil.getLockId(folderId, name);
150 
151         boolean alreadyHasLock = lockService.hasLock(
152             DLFileEntry.class.getName(), lockId, user.getUserId());
153 
154         if (!alreadyHasLock) {
155 
156             // Lock
157 
158             lockService.lock(
159                 DLFileEntry.class.getName(), lockId, user.getCompanyId(),
160                 user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
161         }
162 
163         dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
164 
165         if (!alreadyHasLock) {
166 
167             // Unlock
168 
169             lockService.unlock(DLFileEntry.class.getName(), lockId);
170         }
171     }
172 
173     public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
174         throws PortalException, RemoteException, SystemException {
175 
176         DLFileEntry fileEntry = getFileEntryByTitle(
177             folderId, titleWithExtension);
178 
179         deleteFileEntry(folderId, fileEntry.getName());
180     }
181 
182     public List<DLFileEntry> getFileEntries(long folderId)
183         throws PortalException, SystemException {
184 
185         List<DLFileEntry> fileEntries = dlFileEntryLocalService.getFileEntries(
186             folderId);
187 
188         Iterator<DLFileEntry> itr = fileEntries.iterator();
189 
190         while (itr.hasNext()) {
191             DLFileEntry fileEntry = itr.next();
192 
193             if (!DLFileEntryPermission.contains(
194                     getPermissionChecker(), fileEntry, ActionKeys.VIEW)) {
195 
196                 itr.remove();
197             }
198         }
199 
200         return fileEntries;
201     }
202 
203     public DLFileEntry getFileEntry(long folderId, String name)
204         throws PortalException, SystemException {
205 
206         DLFileEntryPermission.check(
207             getPermissionChecker(), folderId, name, ActionKeys.VIEW);
208 
209         return dlFileEntryLocalService.getFileEntry(folderId, name);
210     }
211 
212     public DLFileEntry getFileEntryByTitle(
213             long folderId, String titleWithExtension)
214         throws PortalException, SystemException {
215 
216         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
217             folderId, titleWithExtension);
218 
219         DLFileEntryPermission.check(
220             getPermissionChecker(), fileEntry, ActionKeys.VIEW);
221 
222         return fileEntry;
223     }
224 
225     public void lockFileEntry(long folderId, String name)
226         throws PortalException, RemoteException, SystemException {
227 
228         User user = getUser();
229 
230         String lockId = DLUtil.getLockId(folderId, name);
231 
232         lockService.lock(
233             DLFileEntry.class.getName(), lockId, user.getCompanyId(),
234             user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
235     }
236 
237     public void unlockFileEntry(long folderId, String name)
238         throws PortalException, RemoteException, SystemException {
239 
240         String lockId = DLUtil.getLockId(folderId, name);
241 
242         lockService.unlock(DLFileEntry.class.getName(), lockId);
243     }
244 
245     public DLFileEntry updateFileEntry(
246             long folderId, long newFolderId, String name, String sourceFileName,
247             String title, String description, String[] tagsEntries,
248             String extraSettings, byte[] byteArray)
249         throws PortalException, RemoteException, SystemException {
250 
251         User user = getUser();
252 
253         DLFileEntryPermission.check(
254             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
255 
256         String lockId = DLUtil.getLockId(folderId, name);
257 
258         boolean alreadyHasLock = lockService.hasLock(
259             DLFileEntry.class.getName(), lockId, user.getUserId());
260 
261         if (!alreadyHasLock) {
262 
263             // Lock
264 
265             lockService.lock(
266                 DLFileEntry.class.getName(), lockId, user.getCompanyId(),
267                 user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
268         }
269 
270         DLFileEntry fileEntry = dlFileEntryLocalService.updateFileEntry(
271             getUserId(), folderId, newFolderId, name, sourceFileName, title,
272             description, tagsEntries, extraSettings, byteArray);
273 
274         if (!alreadyHasLock) {
275 
276             // Unlock
277 
278             lockService.unlock(DLFileEntry.class.getName(), lockId);
279         }
280 
281         return fileEntry;
282     }
283 
284 }