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.impl.DLFolderImpl;
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.setCompanyId(user.getCompanyId());
105         fileShortcut.setUserId(user.getUserId());
106         fileShortcut.setUserName(user.getFullName());
107         fileShortcut.setCreateDate(now);
108         fileShortcut.setModifiedDate(now);
109         fileShortcut.setFolderId(folderId);
110         fileShortcut.setToFolderId(toFolderId);
111         fileShortcut.setToName(toName);
112 
113         dlFileShortcutPersistence.update(fileShortcut, false);
114 
115         // Resources
116 
117         if ((addCommunityPermissions != null) &&
118             (addGuestPermissions != null)) {
119 
120             addFileShortcutResources(
121                 folder, fileShortcut, addCommunityPermissions.booleanValue(),
122                 addGuestPermissions.booleanValue());
123         }
124         else {
125             addFileShortcutResources(
126                 folder, fileShortcut, communityPermissions, guestPermissions);
127         }
128 
129         // Folder
130 
131         folder.setLastPostDate(fileShortcut.getModifiedDate());
132 
133         dlFolderPersistence.update(folder, false);
134 
135         return fileShortcut;
136     }
137 
138     public void addFileShortcutResources(
139             long fileShortcutId, boolean addCommunityPermissions,
140             boolean addGuestPermissions)
141         throws PortalException, SystemException {
142 
143         DLFileShortcut fileShortcut =
144             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
145         DLFolder folder = fileShortcut.getFolder();
146 
147         addFileShortcutResources(
148             folder, fileShortcut, addCommunityPermissions, addGuestPermissions);
149     }
150 
151     public void addFileShortcutResources(
152             DLFolder folder, DLFileShortcut fileShortcut,
153             boolean addCommunityPermissions, boolean addGuestPermissions)
154         throws PortalException, SystemException {
155 
156         resourceLocalService.addResources(
157             fileShortcut.getCompanyId(), folder.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         DLFolder folder = fileShortcut.getFolder();
171 
172         addFileShortcutResources(
173             folder, fileShortcut, communityPermissions, guestPermissions);
174     }
175 
176     public void addFileShortcutResources(
177             DLFolder folder, DLFileShortcut fileShortcut,
178             String[] communityPermissions, String[] guestPermissions)
179         throws PortalException, SystemException {
180 
181         resourceLocalService.addModelResources(
182             fileShortcut.getCompanyId(), folder.getGroupId(),
183             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
184             fileShortcut.getFileShortcutId(), communityPermissions,
185             guestPermissions);
186     }
187 
188     public void deleteFileShortcut(long fileShortcutId)
189         throws PortalException, SystemException {
190 
191         dlFileShortcutPersistence.remove(fileShortcutId);
192     }
193 
194     public void deleteFileShortcut(DLFileShortcut fileShortcut)
195         throws PortalException, SystemException {
196 
197         // Resources
198 
199         resourceLocalService.deleteResource(
200             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
201             ResourceConstants.SCOPE_INDIVIDUAL,
202             fileShortcut.getFileShortcutId());
203 
204         // File shortcut
205 
206         dlFileShortcutPersistence.remove(fileShortcut);
207     }
208 
209     public void deleteFileShortcuts(long toFolderId, String toName)
210         throws PortalException, SystemException {
211 
212         List<DLFileShortcut> fileShortcuts =
213             dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
214 
215         for (DLFileShortcut fileShortcut : fileShortcuts) {
216             deleteFileShortcut(fileShortcut);
217         }
218     }
219 
220     public DLFileShortcut getFileShortcut(long fileShortcutId)
221         throws PortalException, SystemException {
222 
223         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
224     }
225 
226     public DLFileShortcut updateFileShortcut(
227             long userId, long fileShortcutId, long folderId,
228             long toFolderId, String toName)
229         throws PortalException, SystemException {
230 
231         // File shortcut
232 
233         User user = userPersistence.findByPrimaryKey(userId);
234         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
235 
236         validate(user, toFolderId, toName);
237 
238         DLFileShortcut fileShortcut =
239             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
240 
241         fileShortcut.setModifiedDate(new Date());
242         fileShortcut.setFolderId(folderId);
243         fileShortcut.setToFolderId(toFolderId);
244         fileShortcut.setToName(toName);
245 
246         dlFileShortcutPersistence.update(fileShortcut, false);
247 
248         // Folder
249 
250         folder.setLastPostDate(fileShortcut.getModifiedDate());
251 
252         dlFolderPersistence.update(folder, false);
253 
254         return fileShortcut;
255     }
256 
257     public void updateFileShortcuts(
258             long oldToFolderId, String oldToName, long newToFolderId,
259             String newToName)
260         throws SystemException {
261 
262         List<DLFileShortcut> fileShortcuts =
263             dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
264 
265         for (DLFileShortcut fileShortcut : fileShortcuts) {
266             fileShortcut.setToFolderId(newToFolderId);
267             fileShortcut.setToName(newToName);
268 
269             dlFileShortcutPersistence.update(fileShortcut, false);
270         }
271     }
272 
273     protected long getFolderId(long companyId, long folderId)
274         throws SystemException {
275 
276         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
277 
278             // Ensure folder exists and belongs to the proper company
279 
280             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
281 
282             if ((folder == null) || (companyId != folder.getCompanyId())) {
283                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
284             }
285         }
286 
287         return folderId;
288     }
289 
290     protected void validate(User user, long toFolderId, String toName)
291         throws PortalException, SystemException {
292 
293         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
294             toFolderId, toName);
295 
296         if (user.getCompanyId() != fileEntry.getCompanyId()) {
297             throw new NoSuchFileEntryException();
298         }
299     }
300 
301 }