1   /**
2    * Copyright (c) 2000-2008 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;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.ActivityFeedEntry;
30  import com.liferay.portal.model.ActivityTracker;
31  import com.liferay.portal.model.ActivityTrackerInterpreter;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portlet.messageboards.model.MBMessage;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  /**
42   * <a href="MBActivityTrackerInterpreter.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class MBActivityTrackerInterpreter
49      implements ActivityTrackerInterpreter {
50  
51      public String[] getClassNames() {
52          return _CLASS_NAMES;
53      }
54  
55      public ActivityFeedEntry interpret(
56          ActivityTracker activityTracker, ThemeDisplay themeDisplay) {
57  
58          try {
59              return doInterpret(activityTracker, themeDisplay);
60          }
61          catch (Exception e) {
62              if (_log.isWarnEnabled()) {
63                  _log.warn(e);
64              }
65          }
66  
67          return null;
68      }
69  
70      protected ActivityFeedEntry doInterpret(
71              ActivityTracker activityTracker, ThemeDisplay themeDisplay)
72          throws Exception {
73  
74          User creatorUser = UserLocalServiceUtil.getUserById(
75              activityTracker.getUserId());
76  
77          String creatorUserName = creatorUser.getFullName();
78          String creatorUserDisplayURL = creatorUser.getDisplayURL(
79              themeDisplay.getURLPortal());
80  
81          creatorUserName = "<a href=\"" + creatorUserDisplayURL + "\">" + creatorUserName + "</a>";
82  
83          String activity = activityTracker.getActivity();
84  
85          String receiverUserName = activityTracker.getReceiverUserName();
86  
87          if (activityTracker.getReceiverUserId() > 0) {
88              User receiverUser = UserLocalServiceUtil.getUserById(
89                  activityTracker.getReceiverUserId());
90  
91              receiverUserName = receiverUser.getFullName();
92          }
93  
94          // Title
95  
96          String title = StringPool.BLANK;
97  
98          if (activity.equals(MBActivityKeys.ADD)) {
99              title = LanguageUtil.format(
100                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
101                 "activity-message-boards-add",
102                 new Object[] {creatorUserName});
103         }
104         else if (activity.equals(MBActivityKeys.REPLY)) {
105             title = LanguageUtil.format(
106                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
107                 "activity-message-boards-reply",
108                 new Object[] {creatorUserName, receiverUserName});
109         }
110 
111         // Body
112 
113         MBMessage message = MBMessageLocalServiceUtil.getMessage(
114             activityTracker.getClassPK());
115 
116         StringMaker sm = new StringMaker();
117 
118         sm.append("<b>");
119         sm.append(message.getSubject());
120         sm.append("</b><br />");
121         sm.append(StringUtil.shorten(message.getBody(), 200));
122 
123         String body = sm.toString();
124 
125         return new ActivityFeedEntry(title, body);
126     }
127 
128     private static final String[] _CLASS_NAMES = new String[] {
129         MBMessage.class.getName()
130     };
131 
132     private static Log _log =
133         LogFactory.getLog(MBActivityTrackerInterpreter.class);
134 
135 }