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