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