001    /**
002     * Copyright (c) 2000-present 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.document.library.kernel.exception.NoSuchFileEntryException;
018    import com.liferay.document.library.kernel.model.DLFileShortcut;
019    import com.liferay.document.library.kernel.model.DLFileShortcutConstants;
020    import com.liferay.document.library.kernel.model.DLFolder;
021    import com.liferay.document.library.kernel.model.DLFolderConstants;
022    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
023    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
024    import com.liferay.portal.kernel.dao.orm.Property;
025    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
026    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
027    import com.liferay.portal.kernel.exception.PortalException;
028    import com.liferay.portal.kernel.model.ResourceConstants;
029    import com.liferay.portal.kernel.model.SystemEventConstants;
030    import com.liferay.portal.kernel.model.User;
031    import com.liferay.portal.kernel.repository.model.FileEntry;
032    import com.liferay.portal.kernel.service.ServiceContext;
033    import com.liferay.portal.kernel.service.permission.ModelPermissions;
034    import com.liferay.portal.kernel.systemevent.SystemEvent;
035    import com.liferay.portal.kernel.workflow.WorkflowConstants;
036    import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
037    
038    import java.util.Date;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class DLFileShortcutLocalServiceImpl
045            extends DLFileShortcutLocalServiceBaseImpl {
046    
047            @Override
048            public DLFileShortcut addFileShortcut(
049                            long userId, long groupId, long repositoryId, long folderId,
050                            long toFileEntryId, ServiceContext serviceContext)
051                    throws PortalException {
052    
053                    // File shortcut
054    
055                    User user = userPersistence.findByPrimaryKey(userId);
056    
057                    folderId = getFolderId(user.getCompanyId(), folderId);
058    
059                    validate(user, toFileEntryId);
060    
061                    long fileShortcutId = counterLocalService.increment();
062    
063                    DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
064                            fileShortcutId);
065    
066                    fileShortcut.setUuid(serviceContext.getUuid());
067                    fileShortcut.setGroupId(groupId);
068                    fileShortcut.setCompanyId(user.getCompanyId());
069                    fileShortcut.setUserId(user.getUserId());
070                    fileShortcut.setUserName(user.getFullName());
071                    fileShortcut.setRepositoryId(repositoryId);
072                    fileShortcut.setFolderId(folderId);
073                    fileShortcut.setToFileEntryId(toFileEntryId);
074                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
075                    fileShortcut.setActive(true);
076                    fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
077                    fileShortcut.setStatusByUserId(userId);
078                    fileShortcut.setStatusByUserName(user.getFullName());
079                    fileShortcut.setStatusDate(new Date());
080    
081                    dlFileShortcutPersistence.update(fileShortcut);
082    
083                    // Resources
084    
085                    if (serviceContext.isAddGroupPermissions() ||
086                            serviceContext.isAddGuestPermissions()) {
087    
088                            addFileShortcutResources(
089                                    fileShortcut, serviceContext.isAddGroupPermissions(),
090                                    serviceContext.isAddGuestPermissions());
091                    }
092                    else {
093                            addFileShortcutResources(
094                                    fileShortcut, serviceContext.getModelPermissions());
095                    }
096    
097                    // Folder
098    
099                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
100                            dlFolderLocalService.updateLastPostDate(
101                                    folderId, fileShortcut.getModifiedDate());
102                    }
103    
104                    // Asset
105    
106                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
107    
108                    copyAssetTags(fileEntry, serviceContext);
109    
110                    updateAsset(
111                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
112                            serviceContext.getAssetTagNames());
113    
114                    return fileShortcut;
115            }
116    
117            @Override
118            public void addFileShortcutResources(
119                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
120                            boolean addGuestPermissions)
121                    throws PortalException {
122    
123                    resourceLocalService.addResources(
124                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
125                            fileShortcut.getUserId(), DLFileShortcutConstants.getClassName(),
126                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
127                            addGuestPermissions);
128            }
129    
130            @Override
131            public void addFileShortcutResources(
132                            DLFileShortcut fileShortcut, ModelPermissions modelPermissions)
133                    throws PortalException {
134    
135                    resourceLocalService.addModelResources(
136                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
137                            fileShortcut.getUserId(), DLFileShortcutConstants.getClassName(),
138                            fileShortcut.getFileShortcutId(), modelPermissions);
139            }
140    
141            @Override
142            public void addFileShortcutResources(
143                            long fileShortcutId, boolean addGroupPermissions,
144                            boolean addGuestPermissions)
145                    throws PortalException {
146    
147                    DLFileShortcut fileShortcut =
148                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
149    
150                    addFileShortcutResources(
151                            fileShortcut, addGroupPermissions, addGuestPermissions);
152            }
153    
154            @Override
155            public void addFileShortcutResources(
156                            long fileShortcutId, ModelPermissions modelPermissions)
157                    throws PortalException {
158    
159                    DLFileShortcut fileShortcut =
160                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
161    
162                    addFileShortcutResources(fileShortcut, modelPermissions);
163            }
164    
165            @Override
166            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
167            public void deleteFileShortcut(DLFileShortcut fileShortcut)
168                    throws PortalException {
169    
170                    // File shortcut
171    
172                    dlFileShortcutPersistence.remove(fileShortcut);
173    
174                    // Resources
175    
176                    resourceLocalService.deleteResource(
177                            fileShortcut.getCompanyId(), DLFileShortcutConstants.getClassName(),
178                            ResourceConstants.SCOPE_INDIVIDUAL,
179                            fileShortcut.getFileShortcutId());
180    
181                    // Asset
182    
183                    assetEntryLocalService.deleteEntry(
184                            DLFileShortcutConstants.getClassName(),
185                            fileShortcut.getFileShortcutId());
186    
187                    // Trash
188    
189                    if (fileShortcut.isInTrashExplicitly()) {
190                            trashEntryLocalService.deleteEntry(
191                                    DLFileShortcutConstants.getClassName(),
192                                    fileShortcut.getFileShortcutId());
193                    }
194                    else {
195                            trashVersionLocalService.deleteTrashVersion(
196                                    DLFileShortcutConstants.getClassName(),
197                                    fileShortcut.getFileShortcutId());
198                    }
199            }
200    
201            @Override
202            public void deleteFileShortcut(long fileShortcutId) throws PortalException {
203                    DLFileShortcut fileShortcut =
204                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
205    
206                    dlFileShortcutLocalService.deleteFileShortcut(fileShortcut);
207            }
208    
209            @Override
210            public void deleteFileShortcuts(long toFileEntryId) throws PortalException {
211                    List<DLFileShortcut> fileShortcuts =
212                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
213    
214                    for (DLFileShortcut fileShortcut : fileShortcuts) {
215                            dlFileShortcutLocalService.deleteFileShortcut(fileShortcut);
216                    }
217            }
218    
219            @Override
220            public void deleteFileShortcuts(long groupId, long folderId)
221                    throws PortalException {
222    
223                    deleteFileShortcuts(groupId, folderId, true);
224            }
225    
226            @Override
227            public void deleteFileShortcuts(
228                            long groupId, long folderId, boolean includeTrashedEntries)
229                    throws PortalException {
230    
231                    List<DLFileShortcut> fileShortcuts =
232                            dlFileShortcutPersistence.findByG_F(groupId, folderId);
233    
234                    for (DLFileShortcut fileShortcut : fileShortcuts) {
235                            if (includeTrashedEntries || !fileShortcut.isInTrashExplicitly()) {
236                                    dlFileShortcutLocalService.deleteFileShortcut(fileShortcut);
237                            }
238                    }
239            }
240    
241            @Override
242            public void disableFileShortcuts(long toFileEntryId) {
243                    updateFileShortcutsActive(toFileEntryId, false);
244            }
245    
246            @Override
247            public void enableFileShortcuts(long toFileEntryId) {
248                    updateFileShortcutsActive(toFileEntryId, true);
249            }
250    
251            @Override
252            public DLFileShortcut getFileShortcut(long fileShortcutId)
253                    throws PortalException {
254    
255                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
256            }
257    
258            @Override
259            public List<DLFileShortcut> getFileShortcuts(long toFileEntryId) {
260                    return dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
261            }
262    
263            @Override
264            public List<DLFileShortcut> getFileShortcuts(
265                    long groupId, long folderId, boolean active, int status, int start,
266                    int end) {
267    
268                    return dlFileShortcutPersistence.findByG_F_A_S(
269                            groupId, folderId, active, status, start, end);
270            }
271    
272            @Override
273            public int getFileShortcutsCount(
274                    long groupId, long folderId, boolean active, int status) {
275    
276                    return dlFileShortcutPersistence.countByG_F_A_S(
277                            groupId, folderId, active, status);
278            }
279    
280            @Override
281            public void rebuildTree(long companyId) throws PortalException {
282                    dlFolderLocalService.rebuildTree(companyId);
283            }
284    
285            @Override
286            public void setTreePaths(final long folderId, final String treePath)
287                    throws PortalException {
288    
289                    if (treePath == null) {
290                            throw new IllegalArgumentException("Tree path is null");
291                    }
292    
293                    ActionableDynamicQuery actionableDynamicQuery =
294                            getActionableDynamicQuery();
295    
296                    actionableDynamicQuery.setAddCriteriaMethod(
297                            new ActionableDynamicQuery.AddCriteriaMethod() {
298    
299                                    @Override
300                                    public void addCriteria(DynamicQuery dynamicQuery) {
301                                            Property folderIdProperty = PropertyFactoryUtil.forName(
302                                                    "folderId");
303    
304                                            dynamicQuery.add(folderIdProperty.eq(folderId));
305    
306                                            Property treePathProperty = PropertyFactoryUtil.forName(
307                                                    "treePath");
308    
309                                            dynamicQuery.add(
310                                                    RestrictionsFactoryUtil.or(
311                                                            treePathProperty.isNull(),
312                                                            treePathProperty.ne(treePath)));
313                                    }
314    
315                            });
316    
317                    actionableDynamicQuery.setPerformActionMethod(
318                            new ActionableDynamicQuery.PerformActionMethod<DLFileShortcut>() {
319    
320                                    @Override
321                                    public void performAction(DLFileShortcut dlFileShortcut) {
322                                            dlFileShortcut.setTreePath(treePath);
323    
324                                            updateDLFileShortcut(dlFileShortcut);
325                                    }
326    
327                            });
328    
329                    actionableDynamicQuery.performActions();
330            }
331    
332            @Override
333            public void updateAsset(
334                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
335                            String[] assetTagNames)
336                    throws PortalException {
337    
338                    FileEntry fileEntry = dlAppLocalService.getFileEntry(
339                            fileShortcut.getToFileEntryId());
340    
341                    assetEntryLocalService.updateEntry(
342                            userId, fileShortcut.getGroupId(), fileShortcut.getCreateDate(),
343                            fileShortcut.getModifiedDate(),
344                            DLFileShortcutConstants.getClassName(),
345                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), 0,
346                            assetCategoryIds, assetTagNames, true, false, null, null,
347                            fileShortcut.getCreateDate(), null, fileEntry.getMimeType(),
348                            fileEntry.getTitle(), fileEntry.getDescription(), null, null, null,
349                            0, 0, null);
350            }
351    
352            @Override
353            public DLFileShortcut updateFileShortcut(
354                            long userId, long fileShortcutId, long repositoryId, long folderId,
355                            long toFileEntryId, ServiceContext serviceContext)
356                    throws PortalException {
357    
358                    // File shortcut
359    
360                    User user = userPersistence.findByPrimaryKey(userId);
361    
362                    DLFileShortcut fileShortcut =
363                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
364    
365                    validate(user, toFileEntryId);
366    
367                    fileShortcut.setFolderId(folderId);
368                    fileShortcut.setToFileEntryId(toFileEntryId);
369                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
370    
371                    dlFileShortcutPersistence.update(fileShortcut);
372    
373                    // Folder
374    
375                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
376                            dlFolderLocalService.updateLastPostDate(
377                                    folderId, fileShortcut.getModifiedDate());
378                    }
379    
380                    // Asset
381    
382                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
383    
384                    copyAssetTags(fileEntry, serviceContext);
385    
386                    updateAsset(
387                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
388                            serviceContext.getAssetTagNames());
389    
390                    return fileShortcut;
391            }
392    
393            @Override
394            public void updateFileShortcuts(
395                    long oldToFileEntryId, long newToFileEntryId) {
396    
397                    List<DLFileShortcut> fileShortcuts =
398                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
399    
400                    for (DLFileShortcut fileShortcut : fileShortcuts) {
401                            fileShortcut.setToFileEntryId(newToFileEntryId);
402    
403                            dlFileShortcutPersistence.update(fileShortcut);
404                    }
405            }
406    
407            @Override
408            public void updateFileShortcutsActive(long toFileEntryId, boolean active) {
409                    List<DLFileShortcut> fileShortcuts =
410                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
411    
412                    for (DLFileShortcut fileShortcut : fileShortcuts) {
413                            fileShortcut.setActive(active);
414    
415                            dlFileShortcutPersistence.update(fileShortcut);
416                    }
417            }
418    
419            @Override
420            public void updateStatus(
421                            long userId, long fileShortcutId, int status,
422                            ServiceContext serviceContext)
423                    throws PortalException {
424    
425                    User user = userPersistence.findByPrimaryKey(userId);
426    
427                    DLFileShortcut fileShortcut =
428                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
429    
430                    fileShortcut.setStatus(status);
431                    fileShortcut.setStatusByUserId(user.getUserId());
432                    fileShortcut.setStatusByUserName(user.getFullName());
433                    fileShortcut.setStatusDate(serviceContext.getModifiedDate(null));
434    
435                    dlFileShortcutPersistence.update(fileShortcut);
436            }
437    
438            protected void copyAssetTags(
439                            FileEntry fileEntry, ServiceContext serviceContext)
440                    throws PortalException {
441    
442                    String[] assetTagNames = assetTagLocalService.getTagNames(
443                            FileEntry.class.getName(), fileEntry.getFileEntryId());
444    
445                    assetTagLocalService.checkTags(
446                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
447                            assetTagNames);
448    
449                    serviceContext.setAssetTagNames(assetTagNames);
450            }
451    
452            protected long getFolderId(long companyId, long folderId) {
453                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
454    
455                            // Ensure folder exists and belongs to the proper company
456    
457                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
458    
459                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
460                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
461                            }
462                    }
463    
464                    return folderId;
465            }
466    
467            protected void validate(User user, long toFileEntryId)
468                    throws PortalException {
469    
470                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
471    
472                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
473                            throw new NoSuchFileEntryException(
474                                    "{fileEntryId=" + toFileEntryId + "}");
475                    }
476            }
477    
478    }