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.model.MBThread;
024    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
025    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
027    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
028    import com.liferay.portlet.social.model.SocialActivity;
029    import com.liferay.portlet.social.model.SocialActivityConstants;
030    
031    /**
032     * @author Zsolt Berentey
033     */
034    public class MBThreadActivityInterpreter 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 = getMessage(activity);
047    
048                    if (message.getCategoryId() <= 0) {
049                            return StringPool.BLANK;
050                    }
051    
052                    StringBundler sb = new StringBundler(4);
053    
054                    sb.append(serviceContext.getPortalURL());
055                    sb.append(serviceContext.getPathMain());
056                    sb.append("/message_boards/find_category?mbCategoryId=");
057                    sb.append(message.getCategoryId());
058    
059                    String categoryLink = sb.toString();
060    
061                    return wrapLink(categoryLink, "go-to-category", serviceContext);
062            }
063    
064            protected MBMessage getMessage(SocialActivity activity) throws Exception {
065                    MBThread thread = MBThreadLocalServiceUtil.getThread(
066                            activity.getClassPK());
067    
068                    return MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());
069            }
070    
071            @Override
072            protected String getPath(
073                            SocialActivity activity, ServiceContext serviceContext)
074                    throws Exception {
075    
076                    MBThread thread = MBThreadLocalServiceUtil.getThread(
077                            activity.getClassPK());
078    
079                    return "/message_boards/find_message?messageId=" +
080                            thread.getRootMessageId();
081            }
082    
083            @Override
084            protected Object[] getTitleArguments(
085                    String groupName, SocialActivity activity, String link, String title,
086                    ServiceContext serviceContext) {
087    
088                    String userName = getUserName(activity.getUserId(), serviceContext);
089                    String receiverUserName = StringPool.BLANK;
090    
091                    if (activity.getReceiverUserId() > 0) {
092                            receiverUserName = getUserName(
093                                    activity.getReceiverUserId(), serviceContext);
094                    }
095    
096                    return new Object[] {
097                            groupName, userName, receiverUserName, wrapLink(link, title)
098                    };
099            }
100    
101            @Override
102            protected String getTitlePattern(
103                    String groupName, SocialActivity activity) {
104    
105                    int activityType = activity.getType();
106    
107                    if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
108                            if (Validator.isNull(groupName)) {
109                                    return "activity-message-boards-thread-move-to-trash";
110                            }
111                            else {
112                                    return "activity-message-boards-thread-move-to-trash-in";
113                            }
114                    }
115                    else if (activityType ==
116                                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
117    
118                            if (Validator.isNull(groupName)) {
119                                    return "activity-message-boards-thread-restore-from-trash";
120                            }
121                            else {
122                                    return "activity-message-boards-thread-restore-from-trash-in";
123                            }
124                    }
125    
126                    return null;
127            }
128    
129            @Override
130            protected boolean hasPermissions(
131                            PermissionChecker permissionChecker, SocialActivity activity,
132                            String actionId, ServiceContext serviceContext)
133                    throws Exception {
134    
135                    MBMessage message = getMessage(activity);
136    
137                    return MBMessagePermission.contains(
138                            permissionChecker, message.getMessageId(), actionId);
139            }
140    
141            private static final String[] _CLASS_NAMES = {MBThread.class.getName()};
142    
143    }