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