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.setStatus(WorkflowConstants.STATUS_APPROVED);
068                    fileShortcut.setStatusByUserId(userId);
069                    fileShortcut.setStatusByUserName(user.getFullName());
070                    fileShortcut.setStatusDate(now);
071    
072                    dlFileShortcutPersistence.update(fileShortcut, false);
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, false);
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            @Override
113            public void addFileShortcutResources(
114                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
115                            boolean addGuestPermissions)
116                    throws PortalException, SystemException {
117    
118                    resourceLocalService.addResources(
119                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
120                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
121                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
122                            addGuestPermissions);
123            }
124    
125            @Override
126            public void addFileShortcutResources(
127                            DLFileShortcut fileShortcut, String[] groupPermissions,
128                            String[] guestPermissions)
129                    throws PortalException, SystemException {
130    
131                    resourceLocalService.addModelResources(
132                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
133                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
134                            fileShortcut.getFileShortcutId(), groupPermissions,
135                            guestPermissions);
136            }
137    
138            @Override
139            public void addFileShortcutResources(
140                            long fileShortcutId, boolean addGroupPermissions,
141                            boolean addGuestPermissions)
142                    throws PortalException, SystemException {
143    
144                    DLFileShortcut fileShortcut =
145                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
146    
147                    addFileShortcutResources(
148                            fileShortcut, addGroupPermissions, addGuestPermissions);
149            }
150    
151            @Override
152            public void addFileShortcutResources(
153                            long fileShortcutId, String[] groupPermissions,
154                            String[] guestPermissions)
155                    throws PortalException, SystemException {
156    
157                    DLFileShortcut fileShortcut =
158                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
159    
160                    addFileShortcutResources(
161                            fileShortcut, groupPermissions, guestPermissions);
162            }
163    
164            @Override
165            public void deleteFileShortcut(DLFileShortcut fileShortcut)
166                    throws PortalException, SystemException {
167    
168                    // File shortcut
169    
170                    dlFileShortcutPersistence.remove(fileShortcut);
171    
172                    // Resources
173    
174                    resourceLocalService.deleteResource(
175                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
176                            ResourceConstants.SCOPE_INDIVIDUAL,
177                            fileShortcut.getFileShortcutId());
178    
179                    // Asset
180    
181                    assetEntryLocalService.deleteEntry(
182                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
183            }
184    
185            @Override
186            public void deleteFileShortcut(long fileShortcutId)
187                    throws PortalException, SystemException {
188    
189                    DLFileShortcut fileShortcut =
190                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
191    
192                    deleteFileShortcut(fileShortcut);
193            }
194    
195            @Override
196            public void deleteFileShortcuts(long toFileEntryId)
197                    throws PortalException, SystemException {
198    
199                    List<DLFileShortcut> fileShortcuts =
200                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
201    
202                    for (DLFileShortcut fileShortcut : fileShortcuts) {
203                            deleteFileShortcut(fileShortcut);
204                    }
205            }
206    
207            @Override
208            public DLFileShortcut getFileShortcut(long fileShortcutId)
209                    throws PortalException, SystemException {
210    
211                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
212            }
213    
214            @Override
215            public void updateAsset(
216                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
217                            String[] assetTagNames)
218                    throws PortalException, SystemException {
219    
220                    FileEntry fileEntry = dlAppLocalService.getFileEntry(
221                            fileShortcut.getToFileEntryId());
222    
223                    assetEntryLocalService.updateEntry(
224                            userId, fileShortcut.getGroupId(), fileShortcut.getCreateDate(),
225                            fileShortcut.getModifiedDate(), DLFileShortcut.class.getName(),
226                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), 0,
227                            assetCategoryIds, assetTagNames, false, null, null, null, null,
228                            fileEntry.getMimeType(), fileEntry.getTitle(),
229                            fileEntry.getDescription(), null, null, null, 0, 0, null, false);
230            }
231    
232            @Override
233            public DLFileShortcut updateFileShortcut(
234                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
235                            ServiceContext serviceContext)
236                    throws PortalException, SystemException {
237    
238                    // File shortcut
239    
240                    User user = userPersistence.findByPrimaryKey(userId);
241    
242                    DLFileShortcut fileShortcut =
243                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
244    
245                    validate(user, toFileEntryId);
246    
247                    fileShortcut.setModifiedDate(
248                            serviceContext.getModifiedDate(new Date()));
249                    fileShortcut.setFolderId(folderId);
250                    fileShortcut.setToFileEntryId(toFileEntryId);
251    
252                    dlFileShortcutPersistence.update(fileShortcut, false);
253    
254                    // Folder
255    
256                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
257                            DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(folderId);
258    
259                            dlFolder.setLastPostDate(fileShortcut.getModifiedDate());
260    
261                            dlFolderPersistence.update(dlFolder, false);
262                    }
263    
264                    // Asset
265    
266                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
267    
268                    copyAssetTags(fileEntry, serviceContext);
269    
270                    updateAsset(
271                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
272                            serviceContext.getAssetTagNames());
273    
274                    return fileShortcut;
275            }
276    
277            @Override
278            public void updateFileShortcuts(
279                            long oldToFileEntryId, long newToFileEntryId)
280                    throws SystemException {
281    
282                    List<DLFileShortcut> fileShortcuts =
283                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
284    
285                    for (DLFileShortcut fileShortcut : fileShortcuts) {
286                            fileShortcut.setToFileEntryId(newToFileEntryId);
287    
288                            dlFileShortcutPersistence.update(fileShortcut, false);
289                    }
290            }
291    
292            protected void copyAssetTags(
293                            FileEntry fileEntry, ServiceContext serviceContext)
294                    throws PortalException, SystemException {
295    
296                    String[] assetTagNames = assetTagLocalService.getTagNames(
297                            FileEntry.class.getName(), fileEntry.getFileEntryId());
298    
299                    assetTagLocalService.checkTags(
300                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
301                            assetTagNames);
302    
303                    serviceContext.setAssetTagNames(assetTagNames);
304            }
305    
306            protected long getFolderId(long companyId, long folderId)
307                    throws SystemException {
308    
309                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
310    
311                            // Ensure folder exists and belongs to the proper company
312    
313                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
314    
315                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
316                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
317                            }
318                    }
319    
320                    return folderId;
321            }
322    
323            protected void validate(User user, long toFileEntryId)
324                    throws PortalException, SystemException {
325    
326                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
327    
328                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
329                            throw new NoSuchFileEntryException();
330                    }
331            }
332    
333    }