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