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