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.kernel.security.permission.ActionKeys;
28  import com.liferay.portal.model.User;
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.rmi.RemoteException;
37  
38  /**
39   * <a href="DLFileEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class DLFileEntryServiceImpl extends DLFileEntryServiceBaseImpl {
45  
46      public DLFileEntry addFileEntry(
47              long folderId, String name, String title, String description,
48              String[] tagsEntries, String extraSettings, byte[] byteArray,
49              boolean addCommunityPermissions, boolean addGuestPermissions)
50          throws PortalException, SystemException {
51  
52          DLFolderPermission.check(
53              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
54  
55          return dlFileEntryLocalService.addFileEntry(
56              getUserId(), folderId, name, title, description, tagsEntries,
57              extraSettings, byteArray, addCommunityPermissions,
58              addGuestPermissions);
59      }
60  
61      public DLFileEntry addFileEntry(
62              long folderId, String name, String title, String description,
63              String[] tagsEntries, String extraSettings, byte[] byteArray,
64              String[] communityPermissions, String[] guestPermissions)
65          throws PortalException, SystemException {
66  
67          DLFolderPermission.check(
68              getPermissionChecker(), folderId, ActionKeys.ADD_DOCUMENT);
69  
70          return dlFileEntryLocalService.addFileEntry(
71              getUserId(), folderId, name, title, description, tagsEntries,
72              extraSettings, byteArray, communityPermissions, guestPermissions);
73      }
74  
75      public void deleteFileEntry(long folderId, String name)
76          throws PortalException, RemoteException, SystemException {
77  
78          User user = getUser();
79  
80          DLFileEntryPermission.check(
81              getPermissionChecker(), folderId, name, ActionKeys.DELETE);
82  
83          String lockId = DLUtil.getLockId(folderId, name);
84  
85          boolean alreadyHasLock = lockService.hasLock(
86              DLFileEntry.class.getName(), lockId, user.getUserId());
87  
88          if (!alreadyHasLock) {
89  
90              // Lock
91  
92              lockService.lock(
93                  DLFileEntry.class.getName(), lockId, user.getCompanyId(),
94                  user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
95          }
96  
97          dlFileEntryLocalService.deleteFileEntry(folderId, name);
98  
99          if (!alreadyHasLock) {
100 
101             // Unlock
102 
103             lockService.unlock(DLFileEntry.class.getName(), lockId);
104         }
105     }
106 
107     public void deleteFileEntry(long folderId, String name, double version)
108         throws PortalException, RemoteException, SystemException {
109 
110         User user = getUser();
111 
112         DLFileEntryPermission.check(
113             getPermissionChecker(), folderId, name, ActionKeys.DELETE);
114 
115         String lockId = DLUtil.getLockId(folderId, name);
116 
117         boolean alreadyHasLock = lockService.hasLock(
118             DLFileEntry.class.getName(), lockId, user.getUserId());
119 
120         if (!alreadyHasLock) {
121 
122             // Lock
123 
124             lockService.lock(
125                 DLFileEntry.class.getName(), lockId, user.getCompanyId(),
126                 user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
127         }
128 
129         dlFileEntryLocalService.deleteFileEntry(folderId, name, version);
130 
131         if (!alreadyHasLock) {
132 
133             // Unlock
134 
135             lockService.unlock(DLFileEntry.class.getName(), lockId);
136         }
137     }
138 
139     public void deleteFileEntryByTitle(long folderId, String titleWithExtension)
140         throws PortalException, RemoteException, SystemException {
141 
142         DLFileEntry fileEntry = getFileEntryByTitle(
143             folderId, titleWithExtension);
144 
145         deleteFileEntry(folderId, fileEntry.getName());
146     }
147 
148     public DLFileEntry getFileEntry(long folderId, String name)
149         throws PortalException, SystemException {
150 
151         DLFileEntryPermission.check(
152             getPermissionChecker(), folderId, name, ActionKeys.VIEW);
153 
154         return dlFileEntryLocalService.getFileEntry(folderId, name);
155     }
156 
157     public DLFileEntry getFileEntryByTitle(
158             long folderId, String titleWithExtension)
159         throws PortalException, SystemException {
160 
161         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntryByTitle(
162             folderId, titleWithExtension);
163 
164         DLFileEntryPermission.check(
165             getPermissionChecker(), fileEntry, ActionKeys.VIEW);
166 
167         return fileEntry;
168     }
169 
170     public void lockFileEntry(long folderId, String name)
171         throws PortalException, RemoteException, SystemException {
172 
173         User user = getUser();
174 
175         String lockId = DLUtil.getLockId(folderId, name);
176 
177         lockService.lock(
178             DLFileEntry.class.getName(), lockId, user.getCompanyId(),
179             user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
180     }
181 
182     public void unlockFileEntry(long folderId, String name)
183         throws PortalException, RemoteException, SystemException {
184 
185         String lockId = DLUtil.getLockId(folderId, name);
186 
187         lockService.unlock(DLFileEntry.class.getName(), lockId);
188     }
189 
190     public DLFileEntry updateFileEntry(
191             long folderId, long newFolderId, String name, String sourceFileName,
192             String title, String description, String[] tagsEntries,
193             String extraSettings, byte[] byteArray)
194         throws PortalException, RemoteException, SystemException {
195 
196         User user = getUser();
197 
198         DLFileEntryPermission.check(
199             getPermissionChecker(), folderId, name, ActionKeys.UPDATE);
200 
201         String lockId = DLUtil.getLockId(folderId, name);
202 
203         boolean alreadyHasLock = lockService.hasLock(
204             DLFileEntry.class.getName(), lockId, user.getUserId());
205 
206         if (!alreadyHasLock) {
207 
208             // Lock
209 
210             lockService.lock(
211                 DLFileEntry.class.getName(), lockId, user.getCompanyId(),
212                 user.getUserId(), DLFileEntryImpl.LOCK_EXPIRATION_TIME);
213         }
214 
215         DLFileEntry fileEntry = dlFileEntryLocalService.updateFileEntry(
216             getUserId(), folderId, newFolderId, name, sourceFileName, title,
217             description, tagsEntries, extraSettings, byteArray);
218 
219         if (!alreadyHasLock) {
220 
221             // Unlock
222 
223             lockService.unlock(DLFileEntry.class.getName(), lockId);
224         }
225 
226         return fileEntry;
227     }
228 
229 }