1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.social;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.service.GroupLocalServiceUtil;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portlet.messageboards.model.MBMessage;
31  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
32  import com.liferay.portlet.messageboards.util.BBCodeUtil;
33  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
34  import com.liferay.portlet.social.model.SocialActivity;
35  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
36  
37  /**
38   * <a href="MBActivityInterpreter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class MBActivityInterpreter extends BaseSocialActivityInterpreter {
44  
45      public String[] getClassNames() {
46          return _CLASS_NAMES;
47      }
48  
49      protected SocialActivityFeedEntry doInterpret(
50              SocialActivity activity, ThemeDisplay themeDisplay)
51          throws Exception {
52  
53          String creatorUserName = getUserName(
54              activity.getUserId(), themeDisplay);
55          String receiverUserName = getUserName(
56              activity.getReceiverUserId(), themeDisplay);
57  
58          int activityType = activity.getType();
59  
60          // Link
61  
62          MBMessage message = MBMessageLocalServiceUtil.getMessage(
63              activity.getClassPK());
64  
65          String link =
66              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
67                  "/message_boards/find_message?messageId=" +
68                      activity.getClassPK();
69  
70          // Title
71  
72          String groupName = StringPool.BLANK;
73  
74          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
75              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
76  
77              groupName = group.getDescriptiveName();
78          }
79  
80          String titlePattern = null;
81          Object[] titleArguments = null;
82  
83          if (activityType == MBActivityKeys.ADD_MESSAGE) {
84              titlePattern = "activity-message-boards-add-message";
85  
86              if (Validator.isNotNull(groupName)) {
87                  titlePattern += "-in";
88              }
89  
90              titleArguments = new Object[] {creatorUserName, groupName};
91          }
92          else if (activityType == MBActivityKeys.REPLY_MESSAGE) {
93              titlePattern = "activity-message-boards-reply-message";
94  
95              if (Validator.isNotNull(groupName)) {
96                  titlePattern += "-in";
97              }
98  
99              titleArguments = new Object[] {
100                 creatorUserName, receiverUserName, groupName
101             };
102         }
103 
104         String title = themeDisplay.translate(titlePattern, titleArguments);
105 
106         // Body
107 
108         StringBuilder sb = new StringBuilder();
109 
110         sb.append("<a href=\"");
111         sb.append(link);
112         sb.append("\">");
113         sb.append(cleanContent(message.getSubject()));
114         sb.append("</a><br />");
115         sb.append(cleanContent(BBCodeUtil.getHTML(message)));
116 
117         String body = sb.toString();
118 
119         return new SocialActivityFeedEntry(link, title, body);
120     }
121 
122     private static final String[] _CLASS_NAMES = new String[] {
123         MBMessage.class.getName()
124     };
125 
126 }