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