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.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            @Override
037            public String[] getClassNames() {
038                    return _CLASS_NAMES;
039            }
040    
041            @Override
042            protected SocialActivityFeedEntry doInterpret(
043                            SocialActivity activity, ThemeDisplay themeDisplay)
044                    throws Exception {
045    
046                    PermissionChecker permissionChecker =
047                            themeDisplay.getPermissionChecker();
048    
049                    if (!MBMessagePermission.contains(
050                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
051    
052                            return null;
053                    }
054    
055                    String groupName = StringPool.BLANK;
056    
057                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
058                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
059                    }
060    
061                    String creatorUserName = getUserName(
062                            activity.getUserId(), themeDisplay);
063                    String receiverUserName = getUserName(
064                            activity.getReceiverUserId(), themeDisplay);
065    
066                    int activityType = activity.getType();
067    
068                    // Link
069    
070                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
071                            activity.getClassPK());
072    
073                    StringBundler sb = new StringBundler(4);
074    
075                    sb.append(themeDisplay.getPortalURL());
076                    sb.append(themeDisplay.getPathMain());
077                    sb.append("/message_boards/find_message?messageId=");
078                    sb.append(message.getMessageId());
079    
080                    String link = sb.toString();
081    
082                    // Title
083    
084                    String titlePattern = null;
085    
086                    if (activityType == MBActivityKeys.ADD_MESSAGE) {
087                            if (activity.getReceiverUserId() == 0) {
088                                    titlePattern = "activity-message-boards-add-message";
089                            }
090                            else {
091                                    titlePattern = "activity-message-boards-reply-message";
092                            }
093                    }
094                    else if ((activityType == MBActivityKeys.REPLY_MESSAGE) &&
095                                     (activity.getReceiverUserId() > 0)) {
096    
097                            titlePattern = "activity-message-boards-reply-message";
098                    }
099    
100                    if (Validator.isNotNull(groupName)) {
101                            titlePattern += "-in";
102                    }
103    
104                    String messageSubject = getValue(
105                            activity.getExtraData(), "title", message.getSubject());
106    
107                    Object[] titleArguments = new Object[] {
108                            groupName, creatorUserName, receiverUserName,
109                            wrapLink(link, messageSubject)
110                    };
111    
112                    String title = themeDisplay.translate(titlePattern, titleArguments);
113    
114                    // Body
115    
116                    String body = StringPool.BLANK;
117    
118                    if (message.getCategoryId() > 0) {
119                            sb.setIndex(0);
120    
121                            sb.append(themeDisplay.getPortalURL());
122                            sb.append(themeDisplay.getPathMain());
123                            sb.append("/message_boards/find_category?mbCategoryId=");
124                            sb.append(message.getCategoryId());
125    
126                            String categoryLink = sb.toString();
127    
128                            body = wrapLink(categoryLink, "go-to-category", themeDisplay);
129                    }
130    
131                    return new SocialActivityFeedEntry(link, title, body);
132            }
133    
134            private static final String[] _CLASS_NAMES = new String[] {
135                    MBMessage.class.getName()
136            };
137    
138    }