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                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
099    
100                            dlFolder.setLastPostDate(fileShortcut.getModifiedDate());
101    
102                            dlFolderPersistence.update(dlFolder);
103                    }
104    
105                    // Asset
106    
107                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
108    
109                    copyAssetTags(fileEntry, serviceContext);
110    
111                    updateAsset(
112                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
113                            serviceContext.getAssetTagNames());
114    
115                    return fileShortcut;
116            }
117    
118            @Override
119            public void addFileShortcutResources(
120                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
121                            boolean addGuestPermissions)
122                    throws PortalException, SystemException {
123    
124                    resourceLocalService.addResources(
125                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
126                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
127                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
128                            addGuestPermissions);
129            }
130    
131            @Override
132            public void addFileShortcutResources(
133                            DLFileShortcut fileShortcut, String[] groupPermissions,
134                            String[] guestPermissions)
135                    throws PortalException, SystemException {
136    
137                    resourceLocalService.addModelResources(
138                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
139                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
140                            fileShortcut.getFileShortcutId(), groupPermissions,
141                            guestPermissions);
142            }
143    
144            @Override
145            public void addFileShortcutResources(
146                            long fileShortcutId, boolean addGroupPermissions,
147                            boolean addGuestPermissions)
148                    throws PortalException, SystemException {
149    
150                    DLFileShortcut fileShortcut =
151                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
152    
153                    addFileShortcutResources(
154                            fileShortcut, addGroupPermissions, addGuestPermissions);
155            }
156    
157            @Override
158            public void addFileShortcutResources(
159                            long fileShortcutId, String[] groupPermissions,
160                            String[] guestPermissions)
161                    throws PortalException, SystemException {
162    
163                    DLFileShortcut fileShortcut =
164                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
165    
166                    addFileShortcutResources(
167                            fileShortcut, groupPermissions, guestPermissions);
168            }
169    
170            @Override
171            public void deleteFileShortcut(DLFileShortcut fileShortcut)
172                    throws PortalException, SystemException {
173    
174                    // File shortcut
175    
176                    dlFileShortcutPersistence.remove(fileShortcut);
177    
178                    // Resources
179    
180                    resourceLocalService.deleteResource(
181                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
182                            ResourceConstants.SCOPE_INDIVIDUAL,
183                            fileShortcut.getFileShortcutId());
184    
185                    // Asset
186    
187                    assetEntryLocalService.deleteEntry(
188                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
189    
190                    // Trash
191    
192                    trashEntryLocalService.deleteEntry(
193                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
194            }
195    
196            @Override
197            public void deleteFileShortcut(long fileShortcutId)
198                    throws PortalException, SystemException {
199    
200                    DLFileShortcut fileShortcut =
201                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
202    
203                    deleteFileShortcut(fileShortcut);
204            }
205    
206            @Override
207            public void deleteFileShortcuts(long toFileEntryId)
208                    throws PortalException, SystemException {
209    
210                    List<DLFileShortcut> fileShortcuts =
211                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
212    
213                    for (DLFileShortcut fileShortcut : fileShortcuts) {
214                            deleteFileShortcut(fileShortcut);
215                    }
216            }
217    
218            @Override
219            public void deleteFileShortcuts(long groupId, long folderId)
220                    throws PortalException, SystemException {
221    
222                    deleteFileShortcuts(groupId, folderId, true);
223            }
224    
225            @Override
226            public void deleteFileShortcuts(
227                            long groupId, long folderId, boolean includeTrashedEntries)
228                    throws PortalException, SystemException {
229    
230                    List<DLFileShortcut> fileShortcuts =
231                            dlFileShortcutPersistence.findByG_F(groupId, folderId);
232    
233                    for (DLFileShortcut fileShortcut : fileShortcuts) {
234                            if (includeTrashedEntries || !fileShortcut.isInTrash()) {
235                                    deleteFileShortcut(fileShortcut);
236                            }
237                    }
238            }
239    
240            @Override
241            public void disableFileShortcuts(long toFileEntryId)
242                    throws SystemException {
243    
244                    List<DLFileShortcut> fileShortcuts =
245                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
246    
247                    for (DLFileShortcut fileShortcut : fileShortcuts) {
248                            fileShortcut.setActive(false);
249    
250                            dlFileShortcutPersistence.update(fileShortcut);
251                    }
252            }
253    
254            @Override
255            public void enableFileShortcuts(long toFileEntryId) throws SystemException {
256                    List<DLFileShortcut> fileShortcuts =
257                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
258    
259                    for (DLFileShortcut fileShortcut : fileShortcuts) {
260                            fileShortcut.setActive(true);
261    
262                            dlFileShortcutPersistence.update(fileShortcut);
263                    }
264            }
265    
266            @Override
267            public DLFileShortcut getFileShortcut(long fileShortcutId)
268                    throws PortalException, SystemException {
269    
270                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
271            }
272    
273            @Override
274            public List<DLFileShortcut> getFileShortcuts(
275                            long groupId, long folderId, boolean active, int status, int start,
276                            int end)
277                    throws SystemException {
278    
279                    return dlFileShortcutPersistence.findByG_F_A_S(
280                            groupId, folderId, active, status, start, end);
281            }
282    
283            @Override
284            public int getFileShortcutsCount(
285                            long groupId, long folderId, boolean active, int status)
286                    throws SystemException {
287    
288                    return dlFileShortcutPersistence.countByG_F_A_S(
289                            groupId, folderId, active, status);
290            }
291    
292            @Override
293            public void rebuildTree(long companyId) throws SystemException {
294                    dlFolderLocalService.rebuildTree(companyId);
295    
296                    Session session = dlFileShortcutPersistence.openSession();
297    
298                    try {
299                            TreePathUtil.rebuildTree(
300                                    session, companyId, DLFileShortcutModelImpl.TABLE_NAME,
301                                    DLFolderModelImpl.TABLE_NAME, "folderId", true);
302                    }
303                    finally {
304                            dlFileShortcutPersistence.closeSession(session);
305    
306                            dlFileShortcutPersistence.clearCache();
307                    }
308            }
309    
310            @Override
311            public void updateAsset(
312                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
313                            String[] assetTagNames)
314                    throws PortalException, SystemException {
315    
316                    FileEntry fileEntry = dlAppLocalService.getFileEntry(
317                            fileShortcut.getToFileEntryId());
318    
319                    assetEntryLocalService.updateEntry(
320                            userId, fileShortcut.getGroupId(), fileShortcut.getCreateDate(),
321                            fileShortcut.getModifiedDate(), DLFileShortcut.class.getName(),
322                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), 0,
323                            assetCategoryIds, assetTagNames, false, null, null, null,
324                            fileEntry.getMimeType(), fileEntry.getTitle(),
325                            fileEntry.getDescription(), null, null, null, 0, 0, null, false);
326            }
327    
328            @Override
329            public DLFileShortcut updateFileShortcut(
330                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
331                            ServiceContext serviceContext)
332                    throws PortalException, SystemException {
333    
334                    // File shortcut
335    
336                    User user = userPersistence.findByPrimaryKey(userId);
337    
338                    DLFileShortcut fileShortcut =
339                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
340    
341                    validate(user, toFileEntryId);
342    
343                    fileShortcut.setModifiedDate(
344                            serviceContext.getModifiedDate(new Date()));
345                    fileShortcut.setFolderId(folderId);
346                    fileShortcut.setToFileEntryId(toFileEntryId);
347                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
348    
349                    dlFileShortcutPersistence.update(fileShortcut);
350    
351                    // Folder
352    
353                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
354                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
355    
356                            dlFolder.setLastPostDate(fileShortcut.getModifiedDate());
357    
358                            dlFolderPersistence.update(dlFolder);
359                    }
360    
361                    // Asset
362    
363                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
364    
365                    copyAssetTags(fileEntry, serviceContext);
366    
367                    updateAsset(
368                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
369                            serviceContext.getAssetTagNames());
370    
371                    return fileShortcut;
372            }
373    
374            @Override
375            public void updateFileShortcuts(
376                            long oldToFileEntryId, long newToFileEntryId)
377                    throws SystemException {
378    
379                    List<DLFileShortcut> fileShortcuts =
380                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
381    
382                    for (DLFileShortcut fileShortcut : fileShortcuts) {
383                            fileShortcut.setToFileEntryId(newToFileEntryId);
384    
385                            dlFileShortcutPersistence.update(fileShortcut);
386                    }
387            }
388    
389            @Override
390            public void updateStatus(
391                            long userId, long fileShortcutId, int status,
392                            ServiceContext serviceContext)
393                    throws PortalException, SystemException {
394    
395                    User user = userPersistence.findByPrimaryKey(userId);
396    
397                    DLFileShortcut fileShortcut =
398                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
399    
400                    fileShortcut.setStatus(status);
401                    fileShortcut.setStatusByUserId(user.getUserId());
402                    fileShortcut.setStatusByUserName(user.getFullName());
403                    fileShortcut.setStatusDate(serviceContext.getModifiedDate(new Date()));
404    
405                    dlFileShortcutPersistence.update(fileShortcut);
406            }
407    
408            protected void copyAssetTags(
409                            FileEntry fileEntry, ServiceContext serviceContext)
410                    throws PortalException, SystemException {
411    
412                    String[] assetTagNames = assetTagLocalService.getTagNames(
413                            FileEntry.class.getName(), fileEntry.getFileEntryId());
414    
415                    assetTagLocalService.checkTags(
416                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
417                            assetTagNames);
418    
419                    serviceContext.setAssetTagNames(assetTagNames);
420            }
421    
422            protected long getFolderId(long companyId, long folderId)
423                    throws SystemException {
424    
425                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
426    
427                            // Ensure folder exists and belongs to the proper company
428    
429                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
430    
431                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
432                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
433                            }
434                    }
435    
436                    return folderId;
437            }
438    
439            protected void validate(User user, long toFileEntryId)
440                    throws PortalException, SystemException {
441    
442                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
443    
444                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
445                            throw new NoSuchFileEntryException();
446                    }
447            }
448    
449    }