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            @Override
037            public String[] getClassNames() {
038                    return _CLASS_NAMES;
039            }
040    
041            @Override
042            protected String getBody(
043                            SocialActivity activity, ServiceContext serviceContext)
044                    throws Exception {
045    
046                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
047                            activity.getClassPK());
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                    return wrapLink(categoryLink, "go-to-category", serviceContext);
063            }
064    
065            @Override
066            protected String getPath(
067                    SocialActivity activity, ServiceContext serviceContext) {
068    
069                    return "/message_boards/find_message?messageId=" +
070                            activity.getClassPK();
071            }
072    
073            @Override
074            protected Object[] getTitleArguments(
075                    String groupName, SocialActivity activity, String link, String title,
076                    ServiceContext serviceContext) {
077    
078                    String userName = getUserName(activity.getUserId(), serviceContext);
079                    String receiverUserName = StringPool.BLANK;
080    
081                    if (activity.getReceiverUserId() > 0) {
082                            receiverUserName = getUserName(
083                                    activity.getReceiverUserId(), serviceContext);
084                    }
085    
086                    return new Object[] {
087                            groupName, userName, receiverUserName, wrapLink(link, title)
088                    };
089            }
090    
091            @Override
092            protected String getTitlePattern(
093                    String groupName, SocialActivity activity) {
094    
095                    int activityType = activity.getType();
096    
097                    long receiverUserId = activity.getReceiverUserId();
098    
099                    if (activityType == MBActivityKeys.ADD_MESSAGE) {
100                            if (receiverUserId == 0) {
101                                    if (Validator.isNull(groupName)) {
102                                            return "activity-message-boards-message-add-message";
103                                    }
104                                    else {
105                                            return "activity-message-boards-message-add-message-in";
106                                    }
107                            }
108                            else {
109                                    if (Validator.isNull(groupName)) {
110                                            return "activity-message-boards-message-reply-message";
111                                    }
112                                    else {
113                                            return "activity-message-boards-message-reply-message-in";
114                                    }
115                            }
116                    }
117                    else if ((activityType == MBActivityKeys.REPLY_MESSAGE) &&
118                                     (receiverUserId > 0)) {
119    
120                            if (Validator.isNull(groupName)) {
121                                    return "activity-message-boards-message-reply-message";
122                            }
123                            else {
124                                    return "activity-message-boards-message-reply-message-in";
125                            }
126                    }
127    
128                    return null;
129            }
130    
131            @Override
132            protected boolean hasPermissions(
133                            PermissionChecker permissionChecker, SocialActivity activity,
134                            String actionId, ServiceContext serviceContext)
135                    throws Exception {
136    
137                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
138                            activity.getClassPK());
139    
140                    return MBMessagePermission.contains(
141                            permissionChecker, message.getMessageId(), actionId);
142            }
143    
144            private static final String[] _CLASS_NAMES = {MBMessage.class.getName()};
145    
146    }