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