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