001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.ResourceConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
025    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
026    import com.liferay.portlet.documentlibrary.model.DLFolder;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DLFileShortcutLocalServiceImpl
037            extends DLFileShortcutLocalServiceBaseImpl {
038    
039            @Override
040            public DLFileShortcut addFileShortcut(
041                            long userId, long groupId, long folderId, long toFileEntryId,
042                            ServiceContext serviceContext)
043                    throws PortalException, SystemException {
044    
045                    // File shortcut
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    folderId = getFolderId(user.getCompanyId(), folderId);
049                    Date now = new Date();
050    
051                    validate(user, toFileEntryId);
052    
053                    long fileShortcutId = counterLocalService.increment();
054    
055                    DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
056                            fileShortcutId);
057    
058                    fileShortcut.setUuid(serviceContext.getUuid());
059                    fileShortcut.setGroupId(groupId);
060                    fileShortcut.setCompanyId(user.getCompanyId());
061                    fileShortcut.setUserId(user.getUserId());
062                    fileShortcut.setUserName(user.getFullName());
063                    fileShortcut.setCreateDate(serviceContext.getCreateDate(now));
064                    fileShortcut.setModifiedDate(serviceContext.getModifiedDate(now));
065                    fileShortcut.setFolderId(folderId);
066                    fileShortcut.setToFileEntryId(toFileEntryId);
067                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
068                    fileShortcut.setActive(true);
069                    fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
070                    fileShortcut.setStatusByUserId(userId);
071                    fileShortcut.setStatusByUserName(user.getFullName());
072                    fileShortcut.setStatusDate(now);
073    
074                    dlFileShortcutPersistence.update(fileShortcut);
075    
076                    // Resources
077    
078                    if (serviceContext.isAddGroupPermissions() ||
079                            serviceContext.isAddGuestPermissions()) {
080    
081                            addFileShortcutResources(
082                                    fileShortcut, serviceContext.isAddGroupPermissions(),
083                                    serviceContext.isAddGuestPermissions());
084                    }
085                    else {
086                            addFileShortcutResources(
087                                    fileShortcut, serviceContext.getGroupPermissions(),
088                                    serviceContext.getGuestPermissions());
089                    }
090    
091                    // Folder
092    
093                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
094                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
095    
096                            dlFolder.setLastPostDate(fileShortcut.getModifiedDate());
097    
098                            dlFolderPersistence.update(dlFolder);
099                    }
100    
101                    // Asset
102    
103                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
104    
105                    copyAssetTags(fileEntry, serviceContext);
106    
107                    updateAsset(
108                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
109                            serviceContext.getAssetTagNames());
110    
111                    return fileShortcut;
112            }
113    
114            @Override
115            public void addFileShortcutResources(
116                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
117                            boolean addGuestPermissions)
118                    throws PortalException, SystemException {
119    
120                    resourceLocalService.addResources(
121                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
122                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
123                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
124                            addGuestPermissions);
125            }
126    
127            @Override
128            public void addFileShortcutResources(
129                            DLFileShortcut fileShortcut, String[] groupPermissions,
130                            String[] guestPermissions)
131                    throws PortalException, SystemException {
132    
133                    resourceLocalService.addModelResources(
134                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
135                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
136                            fileShortcut.getFileShortcutId(), groupPermissions,
137                            guestPermissions);
138            }
139    
140            @Override
141            public void addFileShortcutResources(
142                            long fileShortcutId, boolean addGroupPermissions,
143                            boolean addGuestPermissions)
144                    throws PortalException, SystemException {
145    
146                    DLFileShortcut fileShortcut =
147                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
148    
149                    addFileShortcutResources(
150                            fileShortcut, addGroupPermissions, addGuestPermissions);
151            }
152    
153            @Override
154            public void addFileShortcutResources(
155                            long fileShortcutId, String[] groupPermissions,
156                            String[] guestPermissions)
157                    throws PortalException, SystemException {
158    
159                    DLFileShortcut fileShortcut =
160                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
161    
162                    addFileShortcutResources(
163                            fileShortcut, groupPermissions, guestPermissions);
164            }
165    
166            @Override
167            public void deleteFileShortcut(DLFileShortcut fileShortcut)
168                    throws PortalException, SystemException {
169    
170                    // File shortcut
171    
172                    dlFileShortcutPersistence.remove(fileShortcut);
173    
174                    // Resources
175    
176                    resourceLocalService.deleteResource(
177                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
178                            ResourceConstants.SCOPE_INDIVIDUAL,
179                            fileShortcut.getFileShortcutId());
180    
181                    // Asset
182    
183                    assetEntryLocalService.deleteEntry(
184                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
185    
186                    // Trash
187    
188                    trashEntryLocalService.deleteEntry(
189                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
190            }
191    
192            @Override
193            public void deleteFileShortcut(long fileShortcutId)
194                    throws PortalException, SystemException {
195    
196                    DLFileShortcut fileShortcut =
197                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
198    
199                    deleteFileShortcut(fileShortcut);
200            }
201    
202            @Override
203            public void deleteFileShortcuts(long toFileEntryId)
204                    throws PortalException, SystemException {
205    
206                    List<DLFileShortcut> fileShortcuts =
207                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
208    
209                    for (DLFileShortcut fileShortcut : fileShortcuts) {
210                            deleteFileShortcut(fileShortcut);
211                    }
212            }
213    
214            @Override
215            public void deleteFileShortcuts(long groupId, long folderId)
216                    throws PortalException, SystemException {
217    
218                    deleteFileShortcuts(groupId, folderId, true);
219            }
220    
221            @Override
222            public void deleteFileShortcuts(
223                            long groupId, long folderId, boolean includeTrashedEntries)
224                    throws PortalException, SystemException {
225    
226                    List<DLFileShortcut> fileShortcuts =
227                            dlFileShortcutPersistence.findByG_F(groupId, folderId);
228    
229                    for (DLFileShortcut fileShortcut : fileShortcuts) {
230                            if (includeTrashedEntries || !fileShortcut.isInTrash()) {
231                                    deleteFileShortcut(fileShortcut);
232                            }
233                    }
234            }
235    
236            @Override
237            public void disableFileShortcuts(long toFileEntryId)
238                    throws SystemException {
239    
240                    List<DLFileShortcut> fileShortcuts =
241                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
242    
243                    for (DLFileShortcut fileShortcut : fileShortcuts) {
244                            fileShortcut.setActive(false);
245    
246                            dlFileShortcutPersistence.update(fileShortcut);
247                    }
248            }
249    
250            @Override
251            public void enableFileShortcuts(long toFileEntryId) throws SystemException {
252                    List<DLFileShortcut> fileShortcuts =
253                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
254    
255                    for (DLFileShortcut fileShortcut : fileShortcuts) {
256                            fileShortcut.setActive(true);
257    
258                            dlFileShortcutPersistence.update(fileShortcut);
259                    }
260            }
261    
262            @Override
263            public DLFileShortcut getFileShortcut(long fileShortcutId)
264                    throws PortalException, SystemException {
265    
266                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
267            }
268    
269            @Override
270            public List<DLFileShortcut> getFileShortcuts(
271                            long groupId, long folderId, boolean active, int status, int start,
272                            int end)
273                    throws SystemException {
274    
275                    return dlFileShortcutPersistence.findByG_F_A_S(
276                            groupId, folderId, active, status, start, end);
277            }
278    
279            @Override
280            public int getFileShortcutsCount(
281                            long groupId, long folderId, boolean active, int status)
282                    throws SystemException {
283    
284                    return dlFileShortcutPersistence.countByG_F_A_S(
285                            groupId, folderId, active, status);
286            }
287    
288            @Override
289            public void rebuildTree(long companyId)
290                    throws PortalException, SystemException {
291    
292                    List<DLFileShortcut> fileShortcuts =
293                            dlFileShortcutPersistence.findByC_NotS(
294                                    companyId, WorkflowConstants.STATUS_IN_TRASH);
295    
296                    for (DLFileShortcut fileShortcut : fileShortcuts) {
297                            if (fileShortcut.isInTrashContainer()) {
298                                    continue;
299                            }
300    
301                            fileShortcut.setTreePath(fileShortcut.buildTreePath());
302    
303                            dlFileShortcutPersistence.update(fileShortcut);
304                    }
305            }
306    
307            @Override
308            public void updateAsset(
309                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
310                            String[] assetTagNames)
311                    throws PortalException, SystemException {
312    
313                    FileEntry fileEntry = dlAppLocalService.getFileEntry(
314                            fileShortcut.getToFileEntryId());
315    
316                    assetEntryLocalService.updateEntry(
317                            userId, fileShortcut.getGroupId(), fileShortcut.getCreateDate(),
318                            fileShortcut.getModifiedDate(), DLFileShortcut.class.getName(),
319                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), 0,
320                            assetCategoryIds, assetTagNames, false, null, null, null,
321                            fileEntry.getMimeType(), fileEntry.getTitle(),
322                            fileEntry.getDescription(), null, null, null, 0, 0, null, false);
323            }
324    
325            @Override
326            public DLFileShortcut updateFileShortcut(
327                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
328                            ServiceContext serviceContext)
329                    throws PortalException, SystemException {
330    
331                    // File shortcut
332    
333                    User user = userPersistence.findByPrimaryKey(userId);
334    
335                    DLFileShortcut fileShortcut =
336                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
337    
338                    validate(user, toFileEntryId);
339    
340                    fileShortcut.setModifiedDate(
341                            serviceContext.getModifiedDate(new Date()));
342                    fileShortcut.setFolderId(folderId);
343                    fileShortcut.setToFileEntryId(toFileEntryId);
344                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
345    
346                    dlFileShortcutPersistence.update(fileShortcut);
347    
348                    // Folder
349    
350                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
351                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
352    
353                            dlFolder.setLastPostDate(fileShortcut.getModifiedDate());
354    
355                            dlFolderPersistence.update(dlFolder);
356                    }
357    
358                    // Asset
359    
360                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
361    
362                    copyAssetTags(fileEntry, serviceContext);
363    
364                    updateAsset(
365                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
366                            serviceContext.getAssetTagNames());
367    
368                    return fileShortcut;
369            }
370    
371            @Override
372            public void updateFileShortcuts(
373                            long oldToFileEntryId, long newToFileEntryId)
374                    throws SystemException {
375    
376                    List<DLFileShortcut> fileShortcuts =
377                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
378    
379                    for (DLFileShortcut fileShortcut : fileShortcuts) {
380                            fileShortcut.setToFileEntryId(newToFileEntryId);
381    
382                            dlFileShortcutPersistence.update(fileShortcut);
383                    }
384            }
385    
386            @Override
387            public void updateStatus(
388                            long userId, long fileShortcutId, int status,
389                            ServiceContext serviceContext)
390                    throws PortalException, SystemException {
391    
392                    User user = userPersistence.findByPrimaryKey(userId);
393    
394                    DLFileShortcut fileShortcut =
395                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
396    
397                    fileShortcut.setStatus(status);
398                    fileShortcut.setStatusByUserId(user.getUserId());
399                    fileShortcut.setStatusByUserName(user.getFullName());
400                    fileShortcut.setStatusDate(serviceContext.getModifiedDate(new Date()));
401    
402                    dlFileShortcutPersistence.update(fileShortcut);
403            }
404    
405            protected void copyAssetTags(
406                            FileEntry fileEntry, ServiceContext serviceContext)
407                    throws PortalException, SystemException {
408    
409                    String[] assetTagNames = assetTagLocalService.getTagNames(
410                            FileEntry.class.getName(), fileEntry.getFileEntryId());
411    
412                    assetTagLocalService.checkTags(
413                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
414                            assetTagNames);
415    
416                    serviceContext.setAssetTagNames(assetTagNames);
417            }
418    
419            protected long getFolderId(long companyId, long folderId)
420                    throws SystemException {
421    
422                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
423    
424                            // Ensure folder exists and belongs to the proper company
425    
426                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
427    
428                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
429                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
430                            }
431                    }
432    
433                    return folderId;
434            }
435    
436            protected void validate(User user, long toFileEntryId)
437                    throws PortalException, SystemException {
438    
439                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
440    
441                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
442                            throw new NoSuchFileEntryException();
443                    }
444            }
445    
446    }