001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.ResourceConstants;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
029    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
030    import com.liferay.portlet.documentlibrary.model.DLFolder;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
033    import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutActionableDynamicQuery;
034    
035    import java.util.Date;
036    import java.util.List;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class DLFileShortcutLocalServiceImpl
042            extends DLFileShortcutLocalServiceBaseImpl {
043    
044            @Override
045            public DLFileShortcut addFileShortcut(
046                            long userId, long groupId, long folderId, long toFileEntryId,
047                            ServiceContext serviceContext)
048                    throws PortalException, SystemException {
049    
050                    // File shortcut
051    
052                    User user = userPersistence.findByPrimaryKey(userId);
053                    folderId = getFolderId(user.getCompanyId(), folderId);
054                    Date now = new Date();
055    
056                    validate(user, toFileEntryId);
057    
058                    long fileShortcutId = counterLocalService.increment();
059    
060                    DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
061                            fileShortcutId);
062    
063                    fileShortcut.setUuid(serviceContext.getUuid());
064                    fileShortcut.setGroupId(groupId);
065                    fileShortcut.setCompanyId(user.getCompanyId());
066                    fileShortcut.setUserId(user.getUserId());
067                    fileShortcut.setUserName(user.getFullName());
068                    fileShortcut.setCreateDate(serviceContext.getCreateDate(now));
069                    fileShortcut.setModifiedDate(serviceContext.getModifiedDate(now));
070                    fileShortcut.setFolderId(folderId);
071                    fileShortcut.setToFileEntryId(toFileEntryId);
072                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
073                    fileShortcut.setActive(true);
074                    fileShortcut.setStatus(WorkflowConstants.STATUS_APPROVED);
075                    fileShortcut.setStatusByUserId(userId);
076                    fileShortcut.setStatusByUserName(user.getFullName());
077                    fileShortcut.setStatusDate(now);
078    
079                    dlFileShortcutPersistence.update(fileShortcut);
080    
081                    // Resources
082    
083                    if (serviceContext.isAddGroupPermissions() ||
084                            serviceContext.isAddGuestPermissions()) {
085    
086                            addFileShortcutResources(
087                                    fileShortcut, serviceContext.isAddGroupPermissions(),
088                                    serviceContext.isAddGuestPermissions());
089                    }
090                    else {
091                            addFileShortcutResources(
092                                    fileShortcut, serviceContext.getGroupPermissions(),
093                                    serviceContext.getGuestPermissions());
094                    }
095    
096                    // Folder
097    
098                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
099                            dlFolderLocalService.updateLastPostDate(
100                                    folderId, fileShortcut.getModifiedDate());
101                    }
102    
103                    // Asset
104    
105                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
106    
107                    copyAssetTags(fileEntry, serviceContext);
108    
109                    updateAsset(
110                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
111                            serviceContext.getAssetTagNames());
112    
113                    return fileShortcut;
114            }
115    
116            @Override
117            public void addFileShortcutResources(
118                            DLFileShortcut fileShortcut, boolean addGroupPermissions,
119                            boolean addGuestPermissions)
120                    throws PortalException, SystemException {
121    
122                    resourceLocalService.addResources(
123                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
124                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
125                            fileShortcut.getFileShortcutId(), false, addGroupPermissions,
126                            addGuestPermissions);
127            }
128    
129            @Override
130            public void addFileShortcutResources(
131                            DLFileShortcut fileShortcut, String[] groupPermissions,
132                            String[] guestPermissions)
133                    throws PortalException, SystemException {
134    
135                    resourceLocalService.addModelResources(
136                            fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
137                            fileShortcut.getUserId(), DLFileShortcut.class.getName(),
138                            fileShortcut.getFileShortcutId(), groupPermissions,
139                            guestPermissions);
140            }
141    
142            @Override
143            public void addFileShortcutResources(
144                            long fileShortcutId, boolean addGroupPermissions,
145                            boolean addGuestPermissions)
146                    throws PortalException, SystemException {
147    
148                    DLFileShortcut fileShortcut =
149                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
150    
151                    addFileShortcutResources(
152                            fileShortcut, addGroupPermissions, addGuestPermissions);
153            }
154    
155            @Override
156            public void addFileShortcutResources(
157                            long fileShortcutId, String[] groupPermissions,
158                            String[] guestPermissions)
159                    throws PortalException, SystemException {
160    
161                    DLFileShortcut fileShortcut =
162                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
163    
164                    addFileShortcutResources(
165                            fileShortcut, groupPermissions, guestPermissions);
166            }
167    
168            @Override
169            public void deleteFileShortcut(DLFileShortcut fileShortcut)
170                    throws PortalException, SystemException {
171    
172                    // File shortcut
173    
174                    dlFileShortcutPersistence.remove(fileShortcut);
175    
176                    // Resources
177    
178                    resourceLocalService.deleteResource(
179                            fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
180                            ResourceConstants.SCOPE_INDIVIDUAL,
181                            fileShortcut.getFileShortcutId());
182    
183                    // Asset
184    
185                    assetEntryLocalService.deleteEntry(
186                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
187    
188                    // Trash
189    
190                    trashEntryLocalService.deleteEntry(
191                            DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
192            }
193    
194            @Override
195            public void deleteFileShortcut(long fileShortcutId)
196                    throws PortalException, SystemException {
197    
198                    DLFileShortcut fileShortcut =
199                            dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
200    
201                    deleteFileShortcut(fileShortcut);
202            }
203    
204            @Override
205            public void deleteFileShortcuts(long toFileEntryId)
206                    throws PortalException, SystemException {
207    
208                    List<DLFileShortcut> fileShortcuts =
209                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
210    
211                    for (DLFileShortcut fileShortcut : fileShortcuts) {
212                            deleteFileShortcut(fileShortcut);
213                    }
214            }
215    
216            @Override
217            public void deleteFileShortcuts(long groupId, long folderId)
218                    throws PortalException, SystemException {
219    
220                    deleteFileShortcuts(groupId, folderId, true);
221            }
222    
223            @Override
224            public void deleteFileShortcuts(
225                            long groupId, long folderId, boolean includeTrashedEntries)
226                    throws PortalException, SystemException {
227    
228                    List<DLFileShortcut> fileShortcuts =
229                            dlFileShortcutPersistence.findByG_F(groupId, folderId);
230    
231                    for (DLFileShortcut fileShortcut : fileShortcuts) {
232                            if (includeTrashedEntries || !fileShortcut.isInTrashExplicitly()) {
233                                    deleteFileShortcut(fileShortcut);
234                            }
235                    }
236            }
237    
238            @Override
239            public void disableFileShortcuts(long toFileEntryId)
240                    throws SystemException {
241    
242                    List<DLFileShortcut> fileShortcuts =
243                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
244    
245                    for (DLFileShortcut fileShortcut : fileShortcuts) {
246                            fileShortcut.setActive(false);
247    
248                            dlFileShortcutPersistence.update(fileShortcut);
249                    }
250            }
251    
252            @Override
253            public void enableFileShortcuts(long toFileEntryId) throws SystemException {
254                    List<DLFileShortcut> fileShortcuts =
255                            dlFileShortcutPersistence.findByToFileEntryId(toFileEntryId);
256    
257                    for (DLFileShortcut fileShortcut : fileShortcuts) {
258                            fileShortcut.setActive(true);
259    
260                            dlFileShortcutPersistence.update(fileShortcut);
261                    }
262            }
263    
264            @Override
265            public DLFileShortcut getFileShortcut(long fileShortcutId)
266                    throws PortalException, SystemException {
267    
268                    return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
269            }
270    
271            @Override
272            public List<DLFileShortcut> getFileShortcuts(
273                            long groupId, long folderId, boolean active, int status, int start,
274                            int end)
275                    throws SystemException {
276    
277                    return dlFileShortcutPersistence.findByG_F_A_S(
278                            groupId, folderId, active, status, start, end);
279            }
280    
281            @Override
282            public int getFileShortcutsCount(
283                            long groupId, long folderId, boolean active, int status)
284                    throws SystemException {
285    
286                    return dlFileShortcutPersistence.countByG_F_A_S(
287                            groupId, folderId, active, status);
288            }
289    
290            @Override
291            public void rebuildTree(long companyId)
292                    throws PortalException, SystemException {
293    
294                    dlFolderLocalService.rebuildTree(companyId);
295            }
296    
297            @Override
298            public void setTreePaths(final long folderId, final String treePath)
299                    throws PortalException, SystemException {
300    
301                    ActionableDynamicQuery actionableDynamicQuery =
302                            new DLFileShortcutActionableDynamicQuery() {
303    
304                            @Override
305                            protected void addCriteria(DynamicQuery dynamicQuery) {
306                                    Property folderIdProperty = PropertyFactoryUtil.forName(
307                                            "folderId");
308    
309                                    dynamicQuery.add(folderIdProperty.eq(folderId));
310    
311                                    Property treePathProperty = PropertyFactoryUtil.forName(
312                                            "treePath");
313    
314                                    dynamicQuery.add(treePathProperty.ne(treePath));
315                            }
316    
317                            @Override
318                            protected void performAction(Object object)
319                                    throws PortalException, SystemException {
320    
321                                    DLFileShortcut shortcut = (DLFileShortcut)object;
322    
323                                    shortcut.setTreePath(treePath);
324    
325                                    updateDLFileShortcut(shortcut);
326                            }
327    
328                    };
329    
330                    actionableDynamicQuery.performActions();
331            }
332    
333            @Override
334            public void updateAsset(
335                            long userId, DLFileShortcut fileShortcut, long[] assetCategoryIds,
336                            String[] assetTagNames)
337                    throws PortalException, SystemException {
338    
339                    FileEntry fileEntry = dlAppLocalService.getFileEntry(
340                            fileShortcut.getToFileEntryId());
341    
342                    assetEntryLocalService.updateEntry(
343                            userId, fileShortcut.getGroupId(), fileShortcut.getCreateDate(),
344                            fileShortcut.getModifiedDate(), DLFileShortcut.class.getName(),
345                            fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), 0,
346                            assetCategoryIds, assetTagNames, false, null, null, null,
347                            fileEntry.getMimeType(), fileEntry.getTitle(),
348                            fileEntry.getDescription(), null, null, null, 0, 0, null, false);
349            }
350    
351            @Override
352            public DLFileShortcut updateFileShortcut(
353                            long userId, long fileShortcutId, long folderId, long toFileEntryId,
354                            ServiceContext serviceContext)
355                    throws PortalException, SystemException {
356    
357                    // File shortcut
358    
359                    User user = userPersistence.findByPrimaryKey(userId);
360    
361                    DLFileShortcut fileShortcut =
362                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
363    
364                    validate(user, toFileEntryId);
365    
366                    fileShortcut.setModifiedDate(
367                            serviceContext.getModifiedDate(new Date()));
368                    fileShortcut.setFolderId(folderId);
369                    fileShortcut.setToFileEntryId(toFileEntryId);
370                    fileShortcut.setTreePath(fileShortcut.buildTreePath());
371    
372                    dlFileShortcutPersistence.update(fileShortcut);
373    
374                    // Folder
375    
376                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
377                            dlFolderLocalService.updateLastPostDate(
378                                    folderId, fileShortcut.getModifiedDate());
379                    }
380    
381                    // Asset
382    
383                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
384    
385                    copyAssetTags(fileEntry, serviceContext);
386    
387                    updateAsset(
388                            userId, fileShortcut, serviceContext.getAssetCategoryIds(),
389                            serviceContext.getAssetTagNames());
390    
391                    return fileShortcut;
392            }
393    
394            @Override
395            public void updateFileShortcuts(
396                            long oldToFileEntryId, long newToFileEntryId)
397                    throws SystemException {
398    
399                    List<DLFileShortcut> fileShortcuts =
400                            dlFileShortcutPersistence.findByToFileEntryId(oldToFileEntryId);
401    
402                    for (DLFileShortcut fileShortcut : fileShortcuts) {
403                            fileShortcut.setToFileEntryId(newToFileEntryId);
404    
405                            dlFileShortcutPersistence.update(fileShortcut);
406                    }
407            }
408    
409            @Override
410            public void updateStatus(
411                            long userId, long fileShortcutId, int status,
412                            ServiceContext serviceContext)
413                    throws PortalException, SystemException {
414    
415                    User user = userPersistence.findByPrimaryKey(userId);
416    
417                    DLFileShortcut fileShortcut =
418                            dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
419    
420                    fileShortcut.setStatus(status);
421                    fileShortcut.setStatusByUserId(user.getUserId());
422                    fileShortcut.setStatusByUserName(user.getFullName());
423                    fileShortcut.setStatusDate(serviceContext.getModifiedDate(new Date()));
424    
425                    dlFileShortcutPersistence.update(fileShortcut);
426            }
427    
428            protected void copyAssetTags(
429                            FileEntry fileEntry, ServiceContext serviceContext)
430                    throws PortalException, SystemException {
431    
432                    String[] assetTagNames = assetTagLocalService.getTagNames(
433                            FileEntry.class.getName(), fileEntry.getFileEntryId());
434    
435                    assetTagLocalService.checkTags(
436                            serviceContext.getUserId(), serviceContext.getScopeGroupId(),
437                            assetTagNames);
438    
439                    serviceContext.setAssetTagNames(assetTagNames);
440            }
441    
442            protected long getFolderId(long companyId, long folderId)
443                    throws SystemException {
444    
445                    if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
446    
447                            // Ensure folder exists and belongs to the proper company
448    
449                            DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(folderId);
450    
451                            if ((dlFolder == null) || (companyId != dlFolder.getCompanyId())) {
452                                    folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
453                            }
454                    }
455    
456                    return folderId;
457            }
458    
459            protected void validate(User user, long toFileEntryId)
460                    throws PortalException, SystemException {
461    
462                    FileEntry fileEntry = dlAppLocalService.getFileEntry(toFileEntryId);
463    
464                    if (user.getCompanyId() != fileEntry.getCompanyId()) {
465                            throw new NoSuchFileEntryException(
466                                    "{fileEntryId=" + toFileEntryId + "}");
467                    }
468            }
469    
470    }