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