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.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 Object[] getTitleArguments(
113                            String groupName, SocialActivity activity, String link,
114                            String title, ServiceContext serviceContext)
115                    throws Exception {
116    
117                    if (activity.getType() == SocialActivityConstants.TYPE_ADD_COMMENT) {
118                            String creatorUserName = getUserName(
119                                    activity.getUserId(), serviceContext);
120                            String receiverUserName = getUserName(
121                                    activity.getReceiverUserId(), serviceContext);
122    
123                            return new Object[] {
124                                    groupName, creatorUserName, receiverUserName,
125                                    wrapLink(link, title)
126                            };
127                    }
128                    else {
129                            return super.getTitleArguments(
130                                    groupName, activity, link, title, serviceContext);
131                    }
132            }
133    
134            @Override
135            protected String getTitlePattern(
136                    String groupName, SocialActivity activity) {
137    
138                    int activityType = activity.getType();
139    
140                    if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
141                            if (Validator.isNull(groupName)) {
142                                    return "activity-document-library-file-add-file";
143                            }
144                            else {
145                                    return "activity-document-library-file-add-file-in";
146                            }
147                    }
148                    else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
149                            if (Validator.isNull(groupName)) {
150                                    return "activity-document-library-file-update-file";
151                            }
152                            else {
153                                    return "activity-document-library-file-update-file-in";
154                            }
155                    }
156                    else if (activityType == SocialActivityConstants.TYPE_ADD_COMMENT) {
157                            if (Validator.isNull(groupName)) {
158                                    return "activity-document-library-file-add-comment";
159                            }
160                            else {
161                                    return "activity-document-library-file-add-comment-in";
162                            }
163                    }
164                    else if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
165                            if (Validator.isNull(groupName)) {
166                                    return "activity-document-library-file-move-to-trash";
167                            }
168                            else {
169                                    return "activity-document-library-file-move-to-trash-in";
170                            }
171                    }
172                    else if (activityType ==
173                                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
174    
175                            if (Validator.isNull(groupName)) {
176                                    return "activity-document-library-file-restore-from-trash";
177                            }
178                            else {
179                                    return "activity-document-library-file-restore-from-trash-in";
180                            }
181                    }
182    
183                    return null;
184            }
185    
186            @Override
187            protected boolean hasPermissions(
188                            PermissionChecker permissionChecker, SocialActivity activity,
189                            String actionId, ServiceContext serviceContext)
190                    throws Exception {
191    
192                    return DLFileEntryPermission.contains(
193                            permissionChecker, activity.getClassPK(), actionId);
194            }
195    
196            private static final String[] _CLASS_NAMES = {DLFileEntry.class.getName()};
197    
198    }