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.Session;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.util.TreePathUtil;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.ResourceConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
027    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
028    import com.liferay.portlet.documentlibrary.model.DLFolder;
029    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
030    import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutModelImpl;
031    import com.liferay.portlet.documentlibrary.model.impl.DLFolderModelImpl;
032    import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
033    
034    import java.util.Date;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class DLFileShortcutLocalServiceImpl
041            extends DLFileShortcutLocalServiceBaseImpl {
042    
043            @Override
044            public DLFileShortcut addFileShortcut(
045                            long userId, long groupId, long folderId, long toFileEntryId,
046                            ServiceContext serviceContext)
047                    throws PortalException, SystemException {
048    
049                    // File shortcut
050    
051                    User user = userPersistence.findByPrimaryKey(userId);
052                    folderId = getFolderId(user.getCompanyId(), folderId);
053                    Date now = new Date();
054    
055                    validate(user, toFileEntryId);
056    
057                    long fileShortcutId = counterLocalService.increment();
058    
059                    DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
060                            fileShortcutId);
061    
062                    fileShortcut.setUuid(serviceContext.getUuid());
063                    fileShortcut.setGroupId(groupId);
064                    fileShortcut.setCompanyId(user.getCompanyId());
065                    fileShortcut.setUserId(user.getUserId());
066                    fileShortcut.setUserName(user.getFullName());
067                    fileShortcut.setCreateDate(serviceContext.getCreateDate(now));
068                    fileShortcut.setModifiedDate(serviceContext.getModifiedDate(now));
069                    fileShortcut.setFolderId(folderId);
070                    fileShortcut.setToFileEntryId(toFileEntryId);
071                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
072                    fileShortcut.setActive(true);
073                    fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
074                    fileShortcut.setStatusByUserId(userId);
075                    fileShortcut.setStatusByUserName(user.getFullName());
076                    fileShortcut.setStatusDate(now);
077    
078                    dlFileShortcutPersistence.update(fileShortcut);
079    
080                    // Resources
081    
082                    if (serviceContext.isAddGroupPermissions() ||
083                            serviceContext.isAddGuestPermissions()) {
084    
085                            addFileShortcutResources(
086                                    fileShortcut, serviceContext.isAddGroupPermissions(),
087                                    serviceContext.isAddGuestPermissions());
088                    }
089                    else {
090                            addFileShortcutResources(
091                                    fileShortcut, serviceContext.getGroupPermissions(),
092                                    serviceContext.getGuestPermissions());
093                    }
094    
095                    // Folder
096    
097                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
098                            dlFolderLocalService.updateLastPostDate(
099                                    folderId, fileShortcut.getModifiedDate());
100                    }
101    
102                    // Asset
103    
104                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
105    
106                    copyAssetTags(fileEntry, serviceContext);
107    
108                    updateAsset(
109                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
110                            serviceContext.getAssetTagNames());
111    
112                    return fileShortcut;
113            }
114    
115            @Override
116            public void addFileShortcutResources(
117                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
118                            boolean addGuestPermissions)
119                    throws PortalException, SystemException {
120    
121                    resourceLocalService.addResources(
122                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
123                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
124                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
125                            addGuestPermissions);
126            }
127    
128            @Override
129            public void addFileShortcutResources(
130                            DLFileShortcut fileShortcut, String[] groupPermissions,
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(), groupPermissions,
138                            guestPermissions);
139            }
140    
141            @Override
142            public void addFileShortcutResources(
143                            long fileShortcutId, boolean addGroupPermissions,
144                            boolean addGuestPermissions)
145                    throws PortalException, SystemException {
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, String[] groupPermissions,
157                            String[] guestPermissions)
158                    throws PortalException, SystemException {
159    
160                    DLFileShortcut fileShortcut =
161                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
162    
163                    addFileShortcutResources(
164                            fileShortcut, groupPermissions, guestPermissions);
165            }
166    
167            @Override
168            public void deleteFileShortcut(DLFileShortcut fileShortcut)
169                    throws PortalException, SystemException {
170    
171                    // File shortcut
172    
173                    dlFileShortcutPersistence.remove(fileShortcut);
174    
175                    // Resources
176    
177                    resourceLocalService.deleteResource(
178                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
179                            ResourceConstants.SCOPE_INDIVIDUAL,
180                            fileShortcut.getFileShortcutId());
181    
182                    // Asset
183    
184                    assetEntryLocalService.deleteEntry(
185                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
186    
187                    // Trash
188    
189                    trashEntryLocalService.deleteEntry(
190                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
191            }
192    
193            @Override
194            public void deleteFileShortcut(long fileShortcutId)
195                    throws PortalException, SystemException {
196    
197                    DLFileShortcut fileShortcut =
198                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
199    
200                    deleteFileShortcut(fileShortcut);
201            }
202    
203            @Override
204            public void deleteFileShortcuts(long toFileEntryId)
205                    throws PortalException, SystemException {
206    
207                    List<DLFileShortcut> fileShortcuts =
208                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
209    
210                    for (DLFileShortcut fileShortcut : fileShortcuts) {
211                            deleteFileShortcut(fileShortcut);
212                    }
213            }
214    
215            @Override
216            public void deleteFileShortcuts(long groupId, long folderId)
217                    throws PortalException, SystemException {
218    
219                    deleteFileShortcuts(groupId, folderId, true);
220            }
221    
222            @Override
223            public void deleteFileShortcuts(
224                            long groupId, long folderId, boolean includeTrashedEntries)
225                    throws PortalException, SystemException {
226    
227                    List<DLFileShortcut> fileShortcuts =
228                            dlFileShortcutPersistence.findByG_F(groupId, folderId);
229    
230                    for (DLFileShortcut fileShortcut : fileShortcuts) {
231                            if (includeTrashedEntries || !fileShortcut.isInTrashExplicitly()) {
232                                    deleteFileShortcut(fileShortcut);
233                            }
234                    }
235            }
236    
237            @Override
238            public void disableFileShortcuts(long toFileEntryId)
239                    throws SystemException {
240    
241                    List<DLFileShortcut> fileShortcuts =
242                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
243    
244                    for (DLFileShortcut fileShortcut : fileShortcuts) {
245                            fileShortcut.setActive(false);
246    
247                            dlFileShortcutPersistence.update(fileShortcut);
248                    }
249            }
250    
251            @Override
252            public void enableFileShortcuts(long toFileEntryId) throws SystemException {
253                    List<DLFileShortcut> fileShortcuts =
254                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
255    
256                    for (DLFileShortcut fileShortcut : fileShortcuts) {
257                            fileShortcut.setActive(true);
258    
259                            dlFileShortcutPersistence.update(fileShortcut);
260                    }
261            }
262    
263            @Override
264            public DLFileShortcut getFileShortcut(long fileShortcutId)
265                    throws PortalException, SystemException {
266    
267                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
268            }
269    
270            @Override
271            public List<DLFileShortcut> getFileShortcuts(
272                            long groupId, long folderId, boolean active, int status, int start,
273                            int end)
274                    throws SystemException {
275    
276                    return dlFileShortcutPersistence.findByG_F_A_S(
277                            groupId, folderId, active, status, start, end);
278            }
279    
280            @Override
281            public int getFileShortcutsCount(
282                            long groupId, long folderId, boolean active, int status)
283                    throws SystemException {
284    
285                    return dlFileShortcutPersistence.countByG_F_A_S(
286                            groupId, folderId, active, status);
287            }
288    
289            @Override
290            public void rebuildTree(long companyId) throws SystemException {
291                    dlFolderLocalService.rebuildTree(companyId);
292    
293                    Session session = dlFileShortcutPersistence.openSession();
294    
295                    try {
296                            TreePathUtil.rebuildTree(
297                                    session, companyId, DLFileShortcutModelImpl.TABLE_NAME,
298                                    DLFolderModelImpl.TABLE_NAME, "folderId", true);
299                    }
300                    finally {
301                            dlFileShortcutPersistence.closeSession(session);
302    
303                            dlFileShortcutPersistence.clearCache();
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                            dlFolderLocalService.updateLastPostDate(
352                                    folderId, fileShortcut.getModifiedDate());
353                    }
354    
355                    // Asset
356    
357                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
358    
359                    copyAssetTags(fileEntry, serviceContext);
360    
361                    updateAsset(
362                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
363                            serviceContext.getAssetTagNames());
364    
365                    return fileShortcut;
366            }
367    
368            @Override
369            public void updateFileShortcuts(
370                            long oldToFileEntryId, long newToFileEntryId)
371                    throws SystemException {
372    
373                    List<DLFileShortcut> fileShortcuts =
374                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
375    
376                    for (DLFileShortcut fileShortcut : fileShortcuts) {
377                            fileShortcut.setToFileEntryId(newToFileEntryId);
378    
379                            dlFileShortcutPersistence.update(fileShortcut);
380                    }
381            }
382    
383            @Override
384            public void updateStatus(
385                            long userId, long fileShortcutId, int status,
386                            ServiceContext serviceContext)
387                    throws PortalException, SystemException {
388    
389                    User user = userPersistence.findByPrimaryKey(userId);
390    
391                    DLFileShortcut fileShortcut =
392                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
393    
394                    fileShortcut.setStatus(status);
395                    fileShortcut.setStatusByUserId(user.getUserId());
396                    fileShortcut.setStatusByUserName(user.getFullName());
397                    fileShortcut.setStatusDate(serviceContext.getModifiedDate(new Date()));
398    
399                    dlFileShortcutPersistence.update(fileShortcut);
400            }
401    
402            protected void copyAssetTags(
403                            FileEntry fileEntry, ServiceContext serviceContext)
404                    throws PortalException, SystemException {
405    
406                    String[] assetTagNames = assetTagLocalService.getTagNames(
407                            FileEntry.class.getName(), fileEntry.getFileEntryId());
408    
409                    assetTagLocalService.checkTags(
410                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
411                            assetTagNames);
412    
413                    serviceContext.setAssetTagNames(assetTagNames);
414            }
415    
416            protected long getFolderId(long companyId, long folderId)
417                    throws SystemException {
418    
419                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
420    
421                            // Ensure folder exists and belongs to the proper company
422    
423                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
424    
425                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
426                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
427                            }
428                    }
429    
430                    return folderId;
431            }
432    
433            protected void validate(User user, long toFileEntryId)
434                    throws PortalException, SystemException {
435    
436                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
437    
438                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
439                            throw new NoSuchFileEntryException(
440                                    "{fileEntryId=" + toFileEntryId + "}");
441                    }
442            }
443    
444    }