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.social;
016    
017    import com.liferay.portal.kernel.repository.model.FileEntry;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
024    import com.liferay.portlet.asset.model.AssetRenderer;
025    import com.liferay.portlet.asset.model.AssetRendererFactory;
026    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
027    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
028    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
029    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
030    import com.liferay.portlet.social.model.SocialActivity;
031    import com.liferay.portlet.social.model.SocialActivityConstants;
032    import com.liferay.portlet.trash.util.TrashUtil;
033    
034    /**
035     * @author Ryan Park
036     * @author Zsolt Berentey
037     */
038    public class DLFileEntryActivityInterpreter
039            extends BaseSocialActivityInterpreter {
040    
041            @Override
042            public String[] getClassNames() {
043                    return _CLASS_NAMES;
044            }
045    
046            @Override
047            protected String getBody(
048                            SocialActivity activity, ServiceContext serviceContext)
049                    throws Exception {
050    
051                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(
052                            activity.getClassPK());
053    
054                    if (TrashUtil.isInTrash(
055                                    DLFileEntry.class.getName(), fileEntry.getFileEntryId())) {
056    
057                            return StringPool.BLANK;
058                    }
059    
060                    StringBundler sb = new StringBundler(3);
061    
062                    AssetRendererFactory assetRendererFactory =
063                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
064                                    DLFileEntry.class.getName());
065    
066                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
067                            fileEntry.getFileEntryId());
068    
069                    String fileEntryLink = assetRenderer.getURLDownload(
070                            serviceContext.getThemeDisplay());
071    
072                    sb.append(wrapLink(fileEntryLink, "download-file", serviceContext));
073    
074                    sb.append(StringPool.SPACE);
075    
076                    String folderLink = getFolderLink(fileEntry, serviceContext);
077    
078                    sb.append(wrapLink(folderLink, "go-to-folder", serviceContext));
079    
080                    return sb.toString();
081            }
082    
083            protected String getFolderLink(
084                    FileEntry fileEntry, ServiceContext serviceContext) {
085    
086                    StringBundler sb = new StringBundler(6);
087    
088                    sb.append(serviceContext.getPortalURL());
089                    sb.append(serviceContext.getPathMain());
090                    sb.append("/document_library/find_folder?groupId=");
091                    sb.append(fileEntry.getRepositoryId());
092                    sb.append("&folderId=");
093                    sb.append(fileEntry.getFolderId());
094    
095                    return sb.toString();
096            }
097    
098            @Override
099            protected String getPath(
100                    SocialActivity activity, ServiceContext serviceContext) {
101    
102                    return "/document_library/find_file_entry?fileEntryId=" +
103                            activity.getClassPK();
104            }
105    
106            @Override
107            protected String getTitlePattern(
108                    String groupName, SocialActivity activity) {
109    
110                    int activityType = activity.getType();
111    
112                    if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
113                            if (Validator.isNull(groupName)) {
114                                    return "activity-document-library-file-add-file";
115                            }
116                            else {
117                                    return "activity-document-library-file-add-file-in";
118                            }
119                    }
120                    else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
121                            if (Validator.isNull(groupName)) {
122                                    return "activity-document-library-file-update-file";
123                            }
124                            else {
125                                    return "activity-document-library-file-update-file-in";
126                            }
127                    }
128                    else if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
129                            if (Validator.isNull(groupName)) {
130                                    return "activity-document-library-file-move-to-trash";
131                            }
132                            else {
133                                    return "activity-document-library-file-move-to-trash-in";
134                            }
135                    }
136                    else if (activityType ==
137                                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
138    
139                            if (Validator.isNull(groupName)) {
140                                    return "activity-document-library-file-restore-from-trash";
141                            }
142                            else {
143                                    return "activity-document-library-file-restore-from-trash-in";
144                            }
145                    }
146    
147                    return null;
148            }
149    
150            @Override
151            protected boolean hasPermissions(
152                            PermissionChecker permissionChecker, SocialActivity activity,
153                            String actionId, ServiceContext serviceContext)
154                    throws Exception {
155    
156                    return DLFileEntryPermission.contains(
157                            permissionChecker, activity.getClassPK(), actionId);
158            }
159    
160            private static final String[] _CLASS_NAMES = {DLFileEntry.class.getName()};
161    
162    }