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