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