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.StatusConstants;
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(now);
67          fileShortcut.setModifiedDate(now);
68          fileShortcut.setFolderId(folderId);
69          fileShortcut.setToFolderId(toFolderId);
70          fileShortcut.setToName(toName);
71          fileShortcut.setStatus(StatusConstants.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(new Date());
246         fileShortcut.setFolderId(folderId);
247         fileShortcut.setToFolderId(toFolderId);
248         fileShortcut.setToName(toName);
249 
250         dlFileShortcutPersistence.update(fileShortcut, false);
251 
252         // Folder
253 
254         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
255             DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
256 
257             folder.setLastPostDate(fileShortcut.getModifiedDate());
258 
259             dlFolderPersistence.update(folder, false);
260         }
261 
262         // Asset
263 
264         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
265             fileShortcut.getGroupId(), toFolderId, toName);
266 
267         copyAssetTags(fileEntry, serviceContext);
268 
269         updateAsset(
270             userId, fileShortcut, serviceContext.getAssetCategoryIds(),
271             serviceContext.getAssetTagNames());
272 
273         return fileShortcut;
274     }
275 
276     public void updateFileShortcuts(
277             long groupId, long oldToFolderId, String oldToName,
278             long newToFolderId, String newToName)
279         throws SystemException {
280 
281         List<DLFileShortcut> fileShortcuts =
282             dlFileShortcutPersistence.findByG_TF_TN(
283                 groupId, oldToFolderId, oldToName);
284 
285         for (DLFileShortcut fileShortcut : fileShortcuts) {
286             fileShortcut.setToFolderId(newToFolderId);
287             fileShortcut.setToName(newToName);
288 
289             dlFileShortcutPersistence.update(fileShortcut, false);
290         }
291     }
292 
293     protected void copyAssetTags(
294             DLFileEntry fileEntry, ServiceContext serviceContext)
295         throws PortalException, SystemException {
296 
297         String[] assetTagNames = assetTagLocalService.getTagNames(
298             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
299 
300         assetTagLocalService.checkTags(
301             serviceContext.getUserId(), serviceContext.getScopeGroupId(),
302             assetTagNames);
303 
304         serviceContext.setAssetTagNames(assetTagNames);
305     }
306 
307     protected long getFolderId(long companyId, long folderId)
308         throws SystemException {
309 
310         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
311 
312             // Ensure folder exists and belongs to the proper company
313 
314             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
315 
316             if ((folder == null) || (companyId != folder.getCompanyId())) {
317                 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
318             }
319         }
320 
321         return folderId;
322     }
323 
324     protected void validate(
325             User user, long groupId, long toFolderId, String toName)
326         throws PortalException, SystemException {
327 
328         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
329             groupId, toFolderId, toName);
330 
331         if (user.getCompanyId() != fileEntry.getCompanyId()) {
332             throw new NoSuchFileEntryException();
333         }
334     }
335 
336 }