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