001    /**
002     * Copyright (c) 2000-2012 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.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                                    titlePattern = "activity-message-boards-add-message";
088                            }
089                            else {
090                                    titlePattern = "activity-message-boards-reply-message";
091                            }
092                    }
093                    else if ((activityType == MBActivityKeys.REPLY_MESSAGE) &&
094                                     (activity.getReceiverUserId() > 0)) {
095    
096                            titlePattern = "activity-message-boards-reply-message";
097                    }
098    
099                    if (Validator.isNotNull(groupName)) {
100                            titlePattern += "-in";
101                    }
102    
103                    String messageSubject = getValue(
104                            activity.getExtraData(), "title", message.getSubject());
105    
106                    Object[] titleArguments = new Object[] {
107                            groupName, creatorUserName, receiverUserName,
108                            wrapLink(link, messageSubject)
109                    };
110    
111                    String title = themeDisplay.translate(titlePattern, titleArguments);
112    
113                    // Body
114    
115                    String body = StringPool.BLANK;
116    
117                    if (message.getCategoryId() > 0) {
118                            sb.setIndex(0);
119    
120                            sb.append(themeDisplay.getPortalURL());
121                            sb.append(themeDisplay.getPathMain());
122                            sb.append("/message_boards/find_category?mbCategoryId=");
123                            sb.append(message.getCategoryId());
124    
125                            String categoryLink = sb.toString();
126    
127                            body = wrapLink(categoryLink, "go-to-category", themeDisplay);
128                    }
129    
130                    return new SocialActivityFeedEntry(link, title, body);
131            }
132    
133            private static final String[] _CLASS_NAMES = new String[] {
134                    MBMessage.class.getName()
135            };
136    
137    }