001    /**
002     * Copyright (c) 2000-2010 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.util.MimeTypesUtil;
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.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class DLFileShortcutLocalServiceImpl
038            extends DLFileShortcutLocalServiceBaseImpl {
039    
040            public DLFileShortcut addFileShortcut(
041                            long userId, long groupId, long folderId, long toFolderId,
042                            String toName, 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, groupId, toFolderId, toName);
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.setToFolderId(toFolderId);
067                    fileShortcut.setToName(toName);
068                    fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
069                    fileShortcut.setStatusByUserId(userId);
070                    fileShortcut.setStatusByUserName(user.getFullName());
071                    fileShortcut.setStatusDate(now);
072    
073                    dlFileShortcutPersistence.update(fileShortcut, false);
074    
075                    // Resources
076    
077                    if (serviceContext.getAddCommunityPermissions() ||
078                            serviceContext.getAddGuestPermissions()) {
079    
080                            addFileShortcutResources(
081                                    fileShortcut, serviceContext.getAddCommunityPermissions(),
082                                    serviceContext.getAddGuestPermissions());
083                    }
084                    else {
085                            addFileShortcutResources(
086                                    fileShortcut, serviceContext.getCommunityPermissions(),
087                                    serviceContext.getGuestPermissions());
088                    }
089    
090                    // Folder
091    
092                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
093                            DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
094    
095                            folder.setLastPostDate(fileShortcut.getModifiedDate());
096    
097                            dlFolderPersistence.update(folder, false);
098                    }
099    
100                    // Asset
101    
102                    DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
103                            groupId, toFolderId, toName);
104    
105                    copyAssetTags(fileEntry, serviceContext);
106    
107                    updateAsset(
108                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
109                            serviceContext.getAssetTagNames());
110    
111                    return fileShortcut;
112            }
113    
114            public void addFileShortcutResources(
115                            DLFileShortcut fileShortcut, boolean addCommunityPermissions,
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, addCommunityPermissions,
123                            addGuestPermissions);
124            }
125    
126            public void addFileShortcutResources(
127                            DLFileShortcut fileShortcut, String[] communityPermissions,
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(), communityPermissions,
135                            guestPermissions);
136            }
137    
138            public void addFileShortcutResources(
139                            long fileShortcutId, boolean addCommunityPermissions,
140                            boolean addGuestPermissions)
141                    throws PortalException, SystemException {
142    
143                    DLFileShortcut fileShortcut =
144                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
145    
146                    addFileShortcutResources(
147                            fileShortcut, addCommunityPermissions, addGuestPermissions);
148            }
149    
150            public void addFileShortcutResources(
151                            long fileShortcutId, String[] communityPermissions,
152                            String[] guestPermissions)
153                    throws PortalException, SystemException {
154    
155                    DLFileShortcut fileShortcut =
156                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
157    
158                    addFileShortcutResources(
159                            fileShortcut, communityPermissions, guestPermissions);
160            }
161    
162            public void deleteFileShortcut(DLFileShortcut fileShortcut)
163                    throws PortalException, SystemException {
164    
165                    // File shortcut
166    
167                    dlFileShortcutPersistence.remove(fileShortcut);
168    
169                    // Resources
170    
171                    resourceLocalService.deleteResource(
172                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
173                            ResourceConstants.SCOPE_INDIVIDUAL,
174                            fileShortcut.getFileShortcutId());
175    
176                    // Asset
177    
178                    assetEntryLocalService.deleteEntry(
179                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
180            }
181    
182            public void deleteFileShortcut(long fileShortcutId)
183                    throws PortalException, SystemException {
184    
185                    DLFileShortcut fileShortcut =
186                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
187    
188                    deleteFileShortcut(fileShortcut);
189            }
190    
191            public void deleteFileShortcuts(
192                            long groupId, long toFolderId, String toName)
193                    throws PortalException, SystemException {
194    
195                    List<DLFileShortcut> fileShortcuts =
196                            dlFileShortcutPersistence.findByG_TF_TN(
197                                    groupId, toFolderId, toName);
198    
199                    for (DLFileShortcut fileShortcut : fileShortcuts) {
200                            deleteFileShortcut(fileShortcut);
201                    }
202            }
203    
204            public DLFileShortcut getFileShortcut(long fileShortcutId)
205                    throws PortalException, SystemException {
206    
207                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
208            }
209    
210            public void updateAsset(
211                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
212                            String[] assetTagNames)
213                    throws PortalException, SystemException {
214    
215                    DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
216                            fileShortcut.getGroupId(), fileShortcut.getToFolderId(),
217                            fileShortcut.getToName());
218    
219                    String mimeType = MimeTypesUtil.getContentType(fileEntry.getTitle());
220    
221                    assetEntryLocalService.updateEntry(
222                            userId, fileShortcut.getGroupId(), DLFileShortcut.class.getName(),
223                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(),
224                            assetCategoryIds, assetTagNames, false, null, null, null, null,
225                            mimeType, fileEntry.getTitle(), fileEntry.getDescription(), null,
226                            null, 0, 0, null, false);
227            }
228    
229            public DLFileShortcut updateFileShortcut(
230                            long userId, long fileShortcutId, long folderId,
231                            long toFolderId, String toName, ServiceContext serviceContext)
232                    throws PortalException, SystemException {
233    
234                    // File shortcut
235    
236                    User user = userPersistence.findByPrimaryKey(userId);
237    
238                    DLFileShortcut fileShortcut =
239                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
240    
241                    validate(user, fileShortcut.getGroupId(), toFolderId, toName);
242    
243                    fileShortcut.setModifiedDate(
244                            serviceContext.getModifiedDate(new Date()));
245                    fileShortcut.setFolderId(folderId);
246                    fileShortcut.setToFolderId(toFolderId);
247                    fileShortcut.setToName(toName);
248    
249                    dlFileShortcutPersistence.update(fileShortcut, false);
250    
251                    // Folder
252    
253                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
254                            DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
255    
256                            folder.setLastPostDate(fileShortcut.getModifiedDate());
257    
258                            dlFolderPersistence.update(folder, false);
259                    }
260    
261                    // Asset
262    
263                    DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
264                            fileShortcut.getGroupId(), toFolderId, toName);
265    
266                    copyAssetTags(fileEntry, serviceContext);
267    
268                    updateAsset(
269                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
270                            serviceContext.getAssetTagNames());
271    
272                    return fileShortcut;
273            }
274    
275            public void updateFileShortcuts(
276                            long groupId, long oldToFolderId, String oldToName,
277                            long newToFolderId, String newToName)
278                    throws SystemException {
279    
280                    List<DLFileShortcut> fileShortcuts =
281                            dlFileShortcutPersistence.findByG_TF_TN(
282                                    groupId, oldToFolderId, oldToName);
283    
284                    for (DLFileShortcut fileShortcut : fileShortcuts) {
285                            fileShortcut.setToFolderId(newToFolderId);
286                            fileShortcut.setToName(newToName);
287    
288                            dlFileShortcutPersistence.update(fileShortcut, false);
289                    }
290            }
291    
292            protected void copyAssetTags(
293                            DLFileEntry fileEntry, ServiceContext serviceContext)
294                    throws PortalException, SystemException {
295    
296                    String[] assetTagNames = assetTagLocalService.getTagNames(
297                            DLFileEntry.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 folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
314    
315                            if ((folder == null) || (companyId != folder.getCompanyId())) {
316                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
317                            }
318                    }
319    
320                    return folderId;
321            }
322    
323            protected void validate(
324                            User user, long groupId, long toFolderId, String toName)
325                    throws PortalException, SystemException {
326    
327                    DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
328                            groupId, toFolderId, toName);
329    
330                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
331                            throw new NoSuchFileEntryException();
332                    }
333            }
334    
335    }