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.User;
28  import com.liferay.portal.model.impl.ResourceImpl;
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.Iterator;
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);
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);
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             ResourceImpl.SCOPE_INDIVIDUAL, fileShortcut.getFileShortcutId());
203 
204         // File shortcut
205 
206         dlFileShortcutPersistence.remove(fileShortcut.getFileShortcutId());
207     }
208 
209     public void deleteFileShortcuts(long toFolderId, String toName)
210         throws PortalException, SystemException {
211 
212         Iterator itr = dlFileShortcutPersistence.findByTF_TN(
213             toFolderId, toName).iterator();
214 
215         while (itr.hasNext()) {
216             DLFileShortcut fileShortcut = (DLFileShortcut)itr.next();
217 
218             deleteFileShortcut(fileShortcut);
219         }
220     }
221 
222     public DLFileShortcut getFileShortcut(long fileShortcutId)
223         throws PortalException, SystemException {
224 
225         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
226     }
227 
228     public DLFileShortcut updateFileShortcut(
229             long userId, long fileShortcutId, long folderId,
230             long toFolderId, String toName)
231         throws PortalException, SystemException {
232 
233         // File shortcut
234 
235         User user = userPersistence.findByPrimaryKey(userId);
236         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
237 
238         validate(user, toFolderId, toName);
239 
240         DLFileShortcut fileShortcut =
241             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
242 
243         fileShortcut.setModifiedDate(new Date());
244         fileShortcut.setFolderId(folderId);
245         fileShortcut.setToFolderId(toFolderId);
246         fileShortcut.setToName(toName);
247 
248         dlFileShortcutPersistence.update(fileShortcut);
249 
250         // Folder
251 
252         folder.setLastPostDate(fileShortcut.getModifiedDate());
253 
254         dlFolderPersistence.update(folder);
255 
256         return fileShortcut;
257     }
258 
259     public void updateFileShortcuts(
260             long oldToFolderId, String oldToName, long newToFolderId,
261             String newToName)
262         throws PortalException, SystemException {
263 
264         Iterator itr = dlFileShortcutPersistence.findByTF_TN(
265             oldToFolderId, oldToName).iterator();
266 
267         while (itr.hasNext()) {
268             DLFileShortcut fileShortcut = (DLFileShortcut)itr.next();
269 
270             fileShortcut.setToFolderId(newToFolderId);
271             fileShortcut.setToName(newToName);
272 
273             dlFileShortcutPersistence.update(fileShortcut);
274         }
275     }
276 
277     protected long getFolderId(long companyId, long folderId)
278         throws PortalException, SystemException {
279 
280         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
281 
282             // Ensure folder exists and belongs to the proper company
283 
284             try {
285                 DLFolder folder = dlFolderPersistence.findByPrimaryKey(
286                     folderId);
287 
288                 if (companyId != folder.getCompanyId()) {
289                     folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
290                 }
291             }
292             catch (NoSuchFolderException nsfe) {
293                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
294             }
295         }
296 
297         return folderId;
298     }
299 
300     protected void validate(User user, long toFolderId, String toName)
301         throws PortalException, SystemException {
302 
303         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
304             toFolderId, toName);
305 
306         if (user.getCompanyId() != fileEntry.getCompanyId()) {
307             throw new NoSuchFileEntryException();
308         }
309     }
310 
311 }