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