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.messageboards.social;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.messageboards.model.MBCategory;
023    import com.liferay.portlet.messageboards.model.MBMessage;
024    import com.liferay.portlet.messageboards.model.MBThread;
025    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
027    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
028    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
029    import com.liferay.portlet.social.model.SocialActivity;
030    import com.liferay.portlet.social.model.SocialActivityConstants;
031    
032    /**
033     * @author Zsolt Berentey
034     */
035    public class MBThreadActivityInterpreter extends BaseSocialActivityInterpreter {
036    
037            @Override
038            public String[] getClassNames() {
039                    return _CLASS_NAMES;
040            }
041    
042            @Override
043            protected String getBody(
044                            SocialActivity activity, ServiceContext serviceContext)
045                    throws Exception {
046    
047                    MBMessage message = getMessage(activity);
048    
049                    if (message.getCategoryId() <= 0) {
050                            return StringPool.BLANK;
051                    }
052    
053                    StringBundler sb = new StringBundler(4);
054    
055                    sb.append(serviceContext.getPortalURL());
056                    sb.append(serviceContext.getPathMain());
057                    sb.append("/message_boards/find_category?mbCategoryId=");
058                    sb.append(message.getCategoryId());
059    
060                    String categoryLink = sb.toString();
061    
062                    categoryLink = addNoSuchEntryRedirect(
063                            categoryLink, MBCategory.class.getName(), message.getCategoryId(),
064                            serviceContext);
065    
066                    return wrapLink(categoryLink, "go-to-category", serviceContext);
067            }
068    
069            protected MBMessage getMessage(SocialActivity activity) throws Exception {
070                    MBThread thread = MBThreadLocalServiceUtil.getThread(
071                            activity.getClassPK());
072    
073                    return MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());
074            }
075    
076            @Override
077            protected String getPath(
078                            SocialActivity activity, ServiceContext serviceContext)
079                    throws Exception {
080    
081                    MBThread thread = MBThreadLocalServiceUtil.getThread(
082                            activity.getClassPK());
083    
084                    return "/message_boards/find_message?messageId=" +
085                            thread.getRootMessageId();
086            }
087    
088            @Override
089            protected Object[] getTitleArguments(
090                    String groupName, SocialActivity activity, String link, String title,
091                    ServiceContext serviceContext) {
092    
093                    String userName = getUserName(activity.getUserId(), serviceContext);
094                    String receiverUserName = StringPool.BLANK;
095    
096                    if (activity.getReceiverUserId() > 0) {
097                            receiverUserName = getUserName(
098                                    activity.getReceiverUserId(), serviceContext);
099                    }
100    
101                    return new Object[] {
102                            groupName, userName, receiverUserName, wrapLink(link, title)
103                    };
104            }
105    
106            @Override
107            protected String getTitlePattern(
108                    String groupName, SocialActivity activity) {
109    
110                    int activityType = activity.getType();
111    
112                    if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
113                            if (Validator.isNull(groupName)) {
114                                    return "activity-message-boards-thread-move-to-trash";
115                            }
116                            else {
117                                    return "activity-message-boards-thread-move-to-trash-in";
118                            }
119                    }
120                    else if (activityType ==
121                                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
122    
123                            if (Validator.isNull(groupName)) {
124                                    return "activity-message-boards-thread-restore-from-trash";
125                            }
126                            else {
127                                    return "activity-message-boards-thread-restore-from-trash-in";
128                            }
129                    }
130    
131                    return null;
132            }
133    
134            @Override
135            protected boolean hasPermissions(
136                            PermissionChecker permissionChecker, SocialActivity activity,
137                            String actionId, ServiceContext serviceContext)
138                    throws Exception {
139    
140                    MBMessage message = getMessage(activity);
141    
142                    return MBMessagePermission.contains(
143                            permissionChecker, message.getMessageId(), actionId);
144            }
145    
146            private static final String[] _CLASS_NAMES = {MBThread.class.getName()};
147    
148    }