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.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.ResourceConstants;
25  import com.liferay.portal.model.User;
26  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
27  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
28  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
29  import com.liferay.portlet.documentlibrary.model.DLFolder;
30  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
31  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
32  
33  import java.util.Date;
34  import java.util.List;
35  
36  /**
37   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class DLFileShortcutLocalServiceImpl
44      extends DLFileShortcutLocalServiceBaseImpl {
45  
46      public DLFileShortcut addFileShortcut(
47              long userId, long folderId, long toFolderId, String toName,
48              boolean addCommunityPermissions, boolean addGuestPermissions)
49          throws PortalException, SystemException {
50  
51          return addFileShortcut(
52              null, userId, folderId, toFolderId, toName,
53              Boolean.valueOf(addCommunityPermissions),
54              Boolean.valueOf(addGuestPermissions), null, null);
55      }
56  
57      public DLFileShortcut addFileShortcut(
58              String uuid, long userId, long folderId, long toFolderId,
59              String toName, boolean addCommunityPermissions,
60              boolean addGuestPermissions)
61          throws PortalException, SystemException {
62  
63          return addFileShortcut(
64              uuid, userId, folderId, toFolderId, toName,
65              Boolean.valueOf(addCommunityPermissions),
66              Boolean.valueOf(addGuestPermissions), null, null);
67      }
68  
69      public DLFileShortcut addFileShortcut(
70              long userId, long folderId, long toFolderId, String toName,
71              String[] communityPermissions, String[] guestPermissions)
72          throws PortalException, SystemException {
73  
74          return addFileShortcut(
75              null, userId, folderId, toFolderId, toName, null, null,
76              communityPermissions, guestPermissions);
77      }
78  
79      public DLFileShortcut addFileShortcut(
80              String uuid, long userId, long folderId, long toFolderId,
81              String toName, Boolean addCommunityPermissions,
82              Boolean addGuestPermissions, String[] communityPermissions,
83              String[] guestPermissions)
84          throws PortalException, SystemException {
85  
86          // File shortcut
87  
88          User user = userPersistence.findByPrimaryKey(userId);
89          folderId = getFolderId(user.getCompanyId(), folderId);
90          DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
91          Date now = new Date();
92  
93          validate(user, toFolderId, toName);
94  
95          long fileShortcutId = counterLocalService.increment();
96  
97          DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
98              fileShortcutId);
99  
100         fileShortcut.setUuid(uuid);
101         fileShortcut.setCompanyId(user.getCompanyId());
102         fileShortcut.setUserId(user.getUserId());
103         fileShortcut.setUserName(user.getFullName());
104         fileShortcut.setCreateDate(now);
105         fileShortcut.setModifiedDate(now);
106         fileShortcut.setFolderId(folderId);
107         fileShortcut.setToFolderId(toFolderId);
108         fileShortcut.setToName(toName);
109 
110         dlFileShortcutPersistence.update(fileShortcut, false);
111 
112         // Resources
113 
114         if ((addCommunityPermissions != null) &&
115             (addGuestPermissions != null)) {
116 
117             addFileShortcutResources(
118                 folder, fileShortcut, addCommunityPermissions.booleanValue(),
119                 addGuestPermissions.booleanValue());
120         }
121         else {
122             addFileShortcutResources(
123                 folder, fileShortcut, communityPermissions, guestPermissions);
124         }
125 
126         // Folder
127 
128         folder.setLastPostDate(fileShortcut.getModifiedDate());
129 
130         dlFolderPersistence.update(folder, false);
131 
132         return fileShortcut;
133     }
134 
135     public void addFileShortcutResources(
136             long fileShortcutId, boolean addCommunityPermissions,
137             boolean addGuestPermissions)
138         throws PortalException, SystemException {
139 
140         DLFileShortcut fileShortcut =
141             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
142         DLFolder folder = fileShortcut.getFolder();
143 
144         addFileShortcutResources(
145             folder, fileShortcut, addCommunityPermissions, addGuestPermissions);
146     }
147 
148     public void addFileShortcutResources(
149             DLFolder folder, DLFileShortcut fileShortcut,
150             boolean addCommunityPermissions, boolean addGuestPermissions)
151         throws PortalException, SystemException {
152 
153         resourceLocalService.addResources(
154             fileShortcut.getCompanyId(), folder.getGroupId(),
155             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
156             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
157             addGuestPermissions);
158     }
159 
160     public void addFileShortcutResources(
161             long fileShortcutId, String[] communityPermissions,
162             String[] guestPermissions)
163         throws PortalException, SystemException {
164 
165         DLFileShortcut fileShortcut =
166             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
167         DLFolder folder = fileShortcut.getFolder();
168 
169         addFileShortcutResources(
170             folder, fileShortcut, communityPermissions, guestPermissions);
171     }
172 
173     public void addFileShortcutResources(
174             DLFolder folder, DLFileShortcut fileShortcut,
175             String[] communityPermissions, String[] guestPermissions)
176         throws PortalException, SystemException {
177 
178         resourceLocalService.addModelResources(
179             fileShortcut.getCompanyId(), folder.getGroupId(),
180             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
181             fileShortcut.getFileShortcutId(), communityPermissions,
182             guestPermissions);
183     }
184 
185     public void deleteFileShortcut(long fileShortcutId)
186         throws PortalException, SystemException {
187 
188         dlFileShortcutPersistence.remove(fileShortcutId);
189     }
190 
191     public void deleteFileShortcut(DLFileShortcut fileShortcut)
192         throws PortalException, SystemException {
193 
194         // Resources
195 
196         resourceLocalService.deleteResource(
197             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
198             ResourceConstants.SCOPE_INDIVIDUAL,
199             fileShortcut.getFileShortcutId());
200 
201         // File shortcut
202 
203         dlFileShortcutPersistence.remove(fileShortcut);
204     }
205 
206     public void deleteFileShortcuts(long toFolderId, String toName)
207         throws PortalException, SystemException {
208 
209         List<DLFileShortcut> fileShortcuts =
210             dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
211 
212         for (DLFileShortcut fileShortcut : fileShortcuts) {
213             deleteFileShortcut(fileShortcut);
214         }
215     }
216 
217     public DLFileShortcut getFileShortcut(long fileShortcutId)
218         throws PortalException, SystemException {
219 
220         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
221     }
222 
223     public DLFileShortcut updateFileShortcut(
224             long userId, long fileShortcutId, long folderId,
225             long toFolderId, String toName)
226         throws PortalException, SystemException {
227 
228         // File shortcut
229 
230         User user = userPersistence.findByPrimaryKey(userId);
231         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
232 
233         validate(user, toFolderId, toName);
234 
235         DLFileShortcut fileShortcut =
236             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
237 
238         fileShortcut.setModifiedDate(new Date());
239         fileShortcut.setFolderId(folderId);
240         fileShortcut.setToFolderId(toFolderId);
241         fileShortcut.setToName(toName);
242 
243         dlFileShortcutPersistence.update(fileShortcut, false);
244 
245         // Folder
246 
247         folder.setLastPostDate(fileShortcut.getModifiedDate());
248 
249         dlFolderPersistence.update(folder, false);
250 
251         return fileShortcut;
252     }
253 
254     public void updateFileShortcuts(
255             long oldToFolderId, String oldToName, long newToFolderId,
256             String newToName)
257         throws SystemException {
258 
259         List<DLFileShortcut> fileShortcuts =
260             dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
261 
262         for (DLFileShortcut fileShortcut : fileShortcuts) {
263             fileShortcut.setToFolderId(newToFolderId);
264             fileShortcut.setToName(newToName);
265 
266             dlFileShortcutPersistence.update(fileShortcut, false);
267         }
268     }
269 
270     protected long getFolderId(long companyId, long folderId)
271         throws SystemException {
272 
273         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
274 
275             // Ensure folder exists and belongs to the proper company
276 
277             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
278 
279             if ((folder == null) || (companyId != folder.getCompanyId())) {
280                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
281             }
282         }
283 
284         return folderId;
285     }
286 
287     protected void validate(User user, long toFolderId, String toName)
288         throws PortalException, SystemException {
289 
290         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
291             toFolderId, toName);
292 
293         if (user.getCompanyId() != fileEntry.getCompanyId()) {
294             throw new NoSuchFileEntryException();
295         }
296     }
297 
298 }