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.blogs.social;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portlet.blogs.model.BlogsEntry;
31  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32  import com.liferay.portlet.messageboards.NoSuchMessageException;
33  import com.liferay.portlet.messageboards.model.MBMessage;
34  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
35  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
36  import com.liferay.portlet.social.model.SocialActivity;
37  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
38  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
39  
40  /**
41   * <a href="BlogsActivityInterpreter.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
47  
48      public String[] getClassNames() {
49          return _CLASS_NAMES;
50      }
51  
52      protected SocialActivityFeedEntry doInterpret(
53              SocialActivity activity, ThemeDisplay themeDisplay)
54          throws Exception {
55  
56          String creatorUserName = getUserName(
57              activity.getUserId(), themeDisplay);
58          String receiverUserName = getUserName(
59              activity.getReceiverUserId(), themeDisplay);
60  
61          int activityType = activity.getType();
62  
63          JSONObject extraData = null;
64  
65          if (Validator.isNotNull(activity.getExtraData())) {
66              extraData = JSONFactoryUtil.createJSONObject(
67                  activity.getExtraData());
68          }
69  
70          // Link
71  
72          BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
73              activity.getClassPK());
74  
75          String link =
76              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
77                  "/blogs/find_entry?entryId=" + activity.getClassPK();
78  
79          // Title
80  
81          String title = StringPool.BLANK;
82  
83          if (activityType == BlogsActivityKeys.ADD_COMMENT) {
84              title = themeDisplay.translate(
85                  "activity-blogs-add-comment",
86                  new Object[] {creatorUserName, receiverUserName});
87          }
88          else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
89              title = themeDisplay.translate(
90                  "activity-blogs-add-entry", creatorUserName);
91          }
92  
93          // Body
94  
95          StringBuilder sb = new StringBuilder();
96  
97          sb.append("<a href=\"");
98          sb.append(link);
99          sb.append("\">");
100 
101         if (activityType == BlogsActivityKeys.ADD_COMMENT) {
102             long messageId = extraData.getInt("messageId");
103 
104             try {
105                 MBMessage message = MBMessageLocalServiceUtil.getMessage(
106                     messageId);
107 
108                 sb.append(cleanContent(message.getBody()));
109             }
110             catch (NoSuchMessageException nsme) {
111                 SocialActivityLocalServiceUtil.deleteActivity(
112                     activity.getActivityId());
113 
114                 return null;
115             }
116         }
117         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
118             sb.append(entry.getTitle());
119         }
120 
121         sb.append("</a><br />");
122 
123         if (activityType == BlogsActivityKeys.ADD_ENTRY) {
124             sb.append(cleanContent(entry.getContent()));
125         }
126 
127         String body = sb.toString();
128 
129         return new SocialActivityFeedEntry(link, title, body);
130     }
131 
132     private static final String[] _CLASS_NAMES = new String[] {
133         BlogsEntry.class.getName()
134     };
135 
136 }