1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.MimeTypesUtil;
20  import com.liferay.portal.kernel.workflow.WorkflowConstants;
21  import com.liferay.portal.model.ResourceConstants;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
25  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
26  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
27  import com.liferay.portlet.documentlibrary.model.DLFolder;
28  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
29  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
30  
31  import java.util.Date;
32  import java.util.List;
33  
34  /**
35   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class DLFileShortcutLocalServiceImpl
41      extends DLFileShortcutLocalServiceBaseImpl {
42  
43      public DLFileShortcut addFileShortcut(
44              String uuid, long userId, long groupId, long folderId,
45              long toFolderId, String toName, ServiceContext serviceContext)
46          throws PortalException, SystemException {
47  
48          // File shortcut
49  
50          User user = userPersistence.findByPrimaryKey(userId);
51          folderId = getFolderId(user.getCompanyId(), folderId);
52          Date now = new Date();
53  
54          validate(user, groupId, toFolderId, toName);
55  
56          long fileShortcutId = counterLocalService.increment();
57  
58          DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
59              fileShortcutId);
60  
61          fileShortcut.setUuid(uuid);
62          fileShortcut.setGroupId(groupId);
63          fileShortcut.setCompanyId(user.getCompanyId());
64          fileShortcut.setUserId(user.getUserId());
65          fileShortcut.setUserName(user.getFullName());
66          fileShortcut.setCreateDate(serviceContext.getCreateDate(now));
67          fileShortcut.setModifiedDate(serviceContext.getModifiedDate(now));
68          fileShortcut.setFolderId(folderId);
69          fileShortcut.setToFolderId(toFolderId);
70          fileShortcut.setToName(toName);
71          fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
72          fileShortcut.setStatusByUserId(userId);
73          fileShortcut.setStatusByUserName(user.getFullName());
74          fileShortcut.setStatusDate(now);
75  
76          dlFileShortcutPersistence.update(fileShortcut, false);
77  
78          // Resources
79  
80          if (serviceContext.getAddCommunityPermissions() ||
81              serviceContext.getAddGuestPermissions()) {
82  
83              addFileShortcutResources(
84                  fileShortcut, serviceContext.getAddCommunityPermissions(),
85                  serviceContext.getAddGuestPermissions());
86          }
87          else {
88              addFileShortcutResources(
89                  fileShortcut, serviceContext.getCommunityPermissions(),
90                  serviceContext.getGuestPermissions());
91          }
92  
93          // Folder
94  
95          if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
96              DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
97  
98              folder.setLastPostDate(fileShortcut.getModifiedDate());
99  
100             dlFolderPersistence.update(folder, false);
101         }
102 
103         // Asset
104 
105         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
106             groupId, toFolderId, toName);
107 
108         copyAssetTags(fileEntry, serviceContext);
109 
110         updateAsset(
111             userId, fileShortcut, serviceContext.getAssetCategoryIds(),
112             serviceContext.getAssetTagNames());
113 
114         return fileShortcut;
115     }
116 
117     public void addFileShortcutResources(
118             DLFileShortcut fileShortcut, boolean addCommunityPermissions,
119             boolean addGuestPermissions)
120         throws PortalException, SystemException {
121 
122         resourceLocalService.addResources(
123             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
124             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
125             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
126             addGuestPermissions);
127     }
128 
129     public void addFileShortcutResources(
130             DLFileShortcut fileShortcut, String[] communityPermissions,
131             String[] guestPermissions)
132         throws PortalException, SystemException {
133 
134         resourceLocalService.addModelResources(
135             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
136             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
137             fileShortcut.getFileShortcutId(), communityPermissions,
138             guestPermissions);
139     }
140 
141     public void addFileShortcutResources(
142             long fileShortcutId, boolean addCommunityPermissions,
143             boolean addGuestPermissions)
144         throws PortalException, SystemException {
145 
146         DLFileShortcut fileShortcut =
147             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
148 
149         addFileShortcutResources(
150             fileShortcut, addCommunityPermissions, addGuestPermissions);
151     }
152 
153     public void addFileShortcutResources(
154             long fileShortcutId, String[] communityPermissions,
155             String[] guestPermissions)
156         throws PortalException, SystemException {
157 
158         DLFileShortcut fileShortcut =
159             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
160 
161         addFileShortcutResources(
162             fileShortcut, communityPermissions, guestPermissions);
163     }
164 
165     public void deleteFileShortcut(DLFileShortcut fileShortcut)
166         throws PortalException, SystemException {
167 
168         // File shortcut
169 
170         dlFileShortcutPersistence.remove(fileShortcut);
171 
172         // Resources
173 
174         resourceLocalService.deleteResource(
175             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
176             ResourceConstants.SCOPE_INDIVIDUAL,
177             fileShortcut.getFileShortcutId());
178 
179         // Asset
180 
181         assetEntryLocalService.deleteEntry(
182             DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
183     }
184 
185     public void deleteFileShortcut(long fileShortcutId)
186         throws PortalException, SystemException {
187 
188         DLFileShortcut fileShortcut =
189             dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
190 
191         deleteFileShortcut(fileShortcut);
192     }
193 
194     public void deleteFileShortcuts(
195             long groupId, long toFolderId, String toName)
196         throws PortalException, SystemException {
197 
198         List<DLFileShortcut> fileShortcuts =
199             dlFileShortcutPersistence.findByG_TF_TN(
200                 groupId, toFolderId, toName);
201 
202         for (DLFileShortcut fileShortcut : fileShortcuts) {
203             deleteFileShortcut(fileShortcut);
204         }
205     }
206 
207     public DLFileShortcut getFileShortcut(long fileShortcutId)
208         throws PortalException, SystemException {
209 
210         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
211     }
212 
213     public void updateAsset(
214             long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
215             String[] assetTagNames)
216         throws PortalException, SystemException {
217 
218         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
219             fileShortcut.getGroupId(), fileShortcut.getToFolderId(),
220             fileShortcut.getToName());
221 
222         String mimeType = MimeTypesUtil.getContentType(fileEntry.getTitle());
223 
224         assetEntryLocalService.updateEntry(
225             userId, fileShortcut.getGroupId(), DLFileShortcut.class.getName(),
226             fileShortcut.getFileShortcutId(), assetCategoryIds, assetTagNames,
227             false, null, null, null, null, mimeType, fileEntry.getTitle(),
228             fileEntry.getDescription(), null, null, 0, 0, null, false);
229     }
230 
231     public DLFileShortcut updateFileShortcut(
232             long userId, long fileShortcutId, long folderId,
233             long toFolderId, String toName, ServiceContext serviceContext)
234         throws PortalException, SystemException {
235 
236         // File shortcut
237 
238         User user = userPersistence.findByPrimaryKey(userId);
239 
240         DLFileShortcut fileShortcut =
241             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
242 
243         validate(user, fileShortcut.getGroupId(), toFolderId, toName);
244 
245         fileShortcut.setModifiedDate(
246             serviceContext.getModifiedDate(new Date()));
247         fileShortcut.setFolderId(folderId);
248         fileShortcut.setToFolderId(toFolderId);
249         fileShortcut.setToName(toName);
250 
251         dlFileShortcutPersistence.update(fileShortcut, false);
252 
253         // Folder
254 
255         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
256             DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
257 
258             folder.setLastPostDate(fileShortcut.getModifiedDate());
259 
260             dlFolderPersistence.update(folder, false);
261         }
262 
263         // Asset
264 
265         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
266             fileShortcut.getGroupId(), toFolderId, toName);
267 
268         copyAssetTags(fileEntry, serviceContext);
269 
270         updateAsset(
271             userId, fileShortcut, serviceContext.getAssetCategoryIds(),
272             serviceContext.getAssetTagNames());
273 
274         return fileShortcut;
275     }
276 
277     public void updateFileShortcuts(
278             long groupId, long oldToFolderId, String oldToName,
279             long newToFolderId, String newToName)
280         throws SystemException {
281 
282         List<DLFileShortcut> fileShortcuts =
283             dlFileShortcutPersistence.findByG_TF_TN(
284                 groupId, oldToFolderId, oldToName);
285 
286         for (DLFileShortcut fileShortcut : fileShortcuts) {
287             fileShortcut.setToFolderId(newToFolderId);
288             fileShortcut.setToName(newToName);
289 
290             dlFileShortcutPersistence.update(fileShortcut, false);
291         }
292     }
293 
294     protected void copyAssetTags(
295             DLFileEntry fileEntry, ServiceContext serviceContext)
296         throws PortalException, SystemException {
297 
298         String[] assetTagNames = assetTagLocalService.getTagNames(
299             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
300 
301         assetTagLocalService.checkTags(
302             serviceContext.getUserId(), serviceContext.getScopeGroupId(),
303             assetTagNames);
304 
305         serviceContext.setAssetTagNames(assetTagNames);
306     }
307 
308     protected long getFolderId(long companyId, long folderId)
309         throws SystemException {
310 
311         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
312 
313             // Ensure folder exists and belongs to the proper company
314 
315             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
316 
317             if ((folder == null) || (companyId != folder.getCompanyId())) {
318                 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
319             }
320         }
321 
322         return folderId;
323     }
324 
325     protected void validate(
326             User user, long groupId, long toFolderId, String toName)
327         throws PortalException, SystemException {
328 
329         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
330             groupId, toFolderId, toName);
331 
332         if (user.getCompanyId() != fileEntry.getCompanyId()) {
333             throw new NoSuchFileEntryException();
334         }
335     }
336 
337 }