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