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