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