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.kernel.util.MimeTypesUtil;
20  import com.liferay.portal.model.ResourceConstants;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
24  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
25  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
26  import com.liferay.portlet.documentlibrary.model.DLFolder;
27  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
28  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class DLFileShortcutLocalServiceImpl
40      extends DLFileShortcutLocalServiceBaseImpl {
41  
42      public DLFileShortcut addFileShortcut(
43              long userId, long folderId, long toFolderId, String toName,
44              boolean addCommunityPermissions, boolean addGuestPermissions)
45          throws PortalException, SystemException {
46  
47          return addFileShortcut(
48              null, userId, folderId, toFolderId, toName,
49              Boolean.valueOf(addCommunityPermissions),
50              Boolean.valueOf(addGuestPermissions), null, null);
51      }
52  
53      public DLFileShortcut addFileShortcut(
54              long userId, long folderId, long toFolderId, String toName,
55              ServiceContext serviceContext)
56          throws PortalException, SystemException {
57  
58          return addFileShortcut(
59              null, userId, folderId, toFolderId, toName, serviceContext);
60      }
61  
62      public DLFileShortcut addFileShortcut(
63              long userId, long folderId, long toFolderId, String toName,
64              String[] communityPermissions, String[] guestPermissions)
65          throws PortalException, SystemException {
66  
67          return addFileShortcut(
68              null, userId, folderId, toFolderId, toName, null, null,
69              communityPermissions, guestPermissions);
70      }
71  
72      public DLFileShortcut addFileShortcut(
73              String uuid, long userId, long folderId, long toFolderId,
74              String toName, boolean addCommunityPermissions,
75              boolean addGuestPermissions)
76          throws PortalException, SystemException {
77  
78          return addFileShortcut(
79                  uuid, userId, folderId, toFolderId, toName,
80                  Boolean.valueOf(addCommunityPermissions),
81                  Boolean.valueOf(addGuestPermissions), null, null);
82      }
83  
84      public DLFileShortcut addFileShortcut(
85              String uuid, long userId, long folderId, long toFolderId,
86              String toName, Boolean addCommunityPermissions,
87              Boolean addGuestPermissions, String[] communityPermissions,
88              String[] guestPermissions)
89          throws PortalException, SystemException {
90  
91          ServiceContext serviceContext = new ServiceContext();
92  
93          serviceContext.setAddCommunityPermissions(addCommunityPermissions);
94          serviceContext.setAddGuestPermissions(addGuestPermissions);
95          serviceContext.setCommunityPermissions(communityPermissions);
96          serviceContext.setGuestPermissions(guestPermissions);
97  
98          return addFileShortcut(
99              uuid, userId, folderId, toFolderId, toName, serviceContext);
100     }
101 
102     public DLFileShortcut addFileShortcut(
103             String uuid, long userId, long folderId, long toFolderId,
104             String toName, ServiceContext serviceContext)
105         throws PortalException, SystemException {
106 
107         // File shortcut
108 
109         User user = userPersistence.findByPrimaryKey(userId);
110         folderId = getFolderId(user.getCompanyId(), folderId);
111         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
112         Date now = new Date();
113 
114         validate(user, toFolderId, toName);
115 
116         long fileShortcutId = counterLocalService.increment();
117 
118         DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
119             fileShortcutId);
120 
121         fileShortcut.setUuid(uuid);
122         fileShortcut.setGroupId(folder.getGroupId());
123         fileShortcut.setCompanyId(user.getCompanyId());
124         fileShortcut.setUserId(user.getUserId());
125         fileShortcut.setUserName(user.getFullName());
126         fileShortcut.setCreateDate(serviceContext.getCreateDate(now));
127         fileShortcut.setModifiedDate(serviceContext.getModifiedDate(now));
128         fileShortcut.setFolderId(folderId);
129         fileShortcut.setToFolderId(toFolderId);
130         fileShortcut.setToName(toName);
131 
132         dlFileShortcutPersistence.update(fileShortcut, false);
133 
134         // Resources
135 
136         if (serviceContext.getAddCommunityPermissions() ||
137             serviceContext.getAddGuestPermissions()) {
138 
139             addFileShortcutResources(
140                 fileShortcut, serviceContext.getAddCommunityPermissions(),
141                 serviceContext.getAddGuestPermissions());
142         }
143         else {
144             addFileShortcutResources(
145                 fileShortcut, serviceContext.getCommunityPermissions(),
146                 serviceContext.getGuestPermissions());
147         }
148 
149         // Folder
150 
151         folder.setLastPostDate(fileShortcut.getModifiedDate());
152 
153         dlFolderPersistence.update(folder, false);
154 
155         // Tags
156 
157         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
158             toFolderId, toName);
159 
160         copyTagEntries(fileEntry, serviceContext);
161 
162         updateTagsAsset(
163             userId, fileShortcut, serviceContext.getTagsCategories(),
164             serviceContext.getTagsEntries());
165 
166         return fileShortcut;
167     }
168 
169     public void addFileShortcutResources(
170             DLFileShortcut fileShortcut, boolean addCommunityPermissions,
171             boolean addGuestPermissions)
172         throws PortalException, SystemException {
173 
174         resourceLocalService.addResources(
175             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
176             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
177             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
178             addGuestPermissions);
179     }
180 
181     public void addFileShortcutResources(
182             DLFileShortcut fileShortcut, String[] communityPermissions,
183             String[] guestPermissions)
184         throws PortalException, SystemException {
185 
186         resourceLocalService.addModelResources(
187             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
188             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
189             fileShortcut.getFileShortcutId(), communityPermissions,
190             guestPermissions);
191     }
192 
193     public void addFileShortcutResources(
194             long fileShortcutId, boolean addCommunityPermissions,
195             boolean addGuestPermissions)
196         throws PortalException, SystemException {
197 
198         DLFileShortcut fileShortcut =
199             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
200 
201         addFileShortcutResources(
202             fileShortcut, addCommunityPermissions, addGuestPermissions);
203     }
204 
205     public void addFileShortcutResources(
206             long fileShortcutId, String[] communityPermissions,
207             String[] guestPermissions)
208         throws PortalException, SystemException {
209 
210         DLFileShortcut fileShortcut =
211             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
212 
213         addFileShortcutResources(
214             fileShortcut, communityPermissions, guestPermissions);
215     }
216 
217     public void deleteFileShortcut(DLFileShortcut fileShortcut)
218         throws PortalException, SystemException {
219 
220         // File shortcut
221 
222         dlFileShortcutPersistence.remove(fileShortcut);
223 
224         // Resources
225 
226         resourceLocalService.deleteResource(
227             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
228             ResourceConstants.SCOPE_INDIVIDUAL,
229             fileShortcut.getFileShortcutId());
230 
231         // Tags
232 
233         tagsAssetLocalService.deleteAsset(
234             DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
235     }
236 
237     public void deleteFileShortcut(long fileShortcutId)
238         throws PortalException, SystemException {
239 
240         DLFileShortcut fileShortcut =
241             dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
242 
243         deleteFileShortcut(fileShortcut);
244     }
245 
246     public void deleteFileShortcuts(long toFolderId, String toName)
247         throws PortalException, SystemException {
248 
249         List<DLFileShortcut> fileShortcuts =
250             dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
251 
252         for (DLFileShortcut fileShortcut : fileShortcuts) {
253             deleteFileShortcut(fileShortcut);
254         }
255     }
256 
257     public DLFileShortcut getFileShortcut(long fileShortcutId)
258         throws PortalException, SystemException {
259 
260         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
261     }
262 
263     public DLFileShortcut updateFileShortcut(
264             long userId, long fileShortcutId, long folderId,
265             long toFolderId, String toName)
266         throws PortalException, SystemException {
267 
268         ServiceContext serviceContext = new ServiceContext();
269 
270         return updateFileShortcut(
271             userId, fileShortcutId, folderId, toFolderId, toName,
272             serviceContext);
273     }
274 
275     public DLFileShortcut updateFileShortcut(
276             long userId, long fileShortcutId, long folderId,
277             long toFolderId, String toName, ServiceContext serviceContext)
278         throws PortalException, SystemException {
279 
280         // File shortcut
281 
282         User user = userPersistence.findByPrimaryKey(userId);
283         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
284 
285         validate(user, toFolderId, toName);
286 
287         DLFileShortcut fileShortcut =
288             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
289 
290         fileShortcut.setModifiedDate(
291             serviceContext.getModifiedDate(new Date()));
292         fileShortcut.setFolderId(folderId);
293         fileShortcut.setToFolderId(toFolderId);
294         fileShortcut.setToName(toName);
295 
296         dlFileShortcutPersistence.update(fileShortcut, false);
297 
298         // Folder
299 
300         folder.setLastPostDate(fileShortcut.getModifiedDate());
301 
302         dlFolderPersistence.update(folder, false);
303 
304         // Tags
305 
306         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
307             toFolderId, toName);
308 
309         copyTagEntries(fileEntry, serviceContext);
310 
311         updateTagsAsset(
312             userId, fileShortcut, serviceContext.getTagsCategories(),
313             serviceContext.getTagsEntries());
314 
315         return fileShortcut;
316     }
317 
318     public void updateFileShortcuts(
319             long oldToFolderId, String oldToName, long newToFolderId,
320             String newToName)
321         throws SystemException {
322 
323         List<DLFileShortcut> fileShortcuts =
324             dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
325 
326         for (DLFileShortcut fileShortcut : fileShortcuts) {
327             fileShortcut.setToFolderId(newToFolderId);
328             fileShortcut.setToName(newToName);
329 
330             dlFileShortcutPersistence.update(fileShortcut, false);
331         }
332     }
333 
334     public void updateTagsAsset(
335             long userId, DLFileShortcut fileShortcut, String[] tagsCategories,
336             String[] tagsEntries)
337         throws PortalException, SystemException {
338 
339         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
340             fileShortcut.getToFolderId(), fileShortcut.getToName());
341 
342         String mimeType = MimeTypesUtil.getContentType(fileEntry.getName());
343 
344         tagsAssetLocalService.updateAsset(
345             userId, fileShortcut.getGroupId(), DLFileShortcut.class.getName(),
346             fileShortcut.getFileShortcutId(), tagsCategories, tagsEntries,
347             false, null, null, null, null, mimeType, fileEntry.getTitle(),
348             fileEntry.getDescription(), null, null, 0, 0, null, false);
349     }
350 
351     protected void copyTagEntries(
352             DLFileEntry fileEntry, ServiceContext serviceContext)
353         throws PortalException, SystemException {
354 
355         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
356             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
357 
358         tagsEntryLocalService.checkEntries(
359             serviceContext.getUserId(), serviceContext.getScopeGroupId(),
360             tagsEntries);
361 
362         serviceContext.setTagsEntries(tagsEntries);
363     }
364 
365     protected long getFolderId(long companyId, long folderId)
366         throws SystemException {
367 
368         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
369 
370             // Ensure folder exists and belongs to the proper company
371 
372             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
373 
374             if ((folder == null) || (companyId != folder.getCompanyId())) {
375                 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
376             }
377         }
378 
379         return folderId;
380     }
381 
382     protected void validate(User user, long toFolderId, String toName)
383         throws PortalException, SystemException {
384 
385         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
386             toFolderId, toName);
387 
388         if (user.getCompanyId() != fileEntry.getCompanyId()) {
389             throw new NoSuchFileEntryException();
390         }
391     }
392 
393 }