1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.social;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.blogs.model.BlogsEntry;
30  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
31  import com.liferay.portlet.messageboards.NoSuchMessageException;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
34  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
35  import com.liferay.portlet.social.model.SocialActivity;
36  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
37  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
38  
39  /**
40   * <a href="BlogsActivityInterpreter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
46  
47      public String[] getClassNames() {
48          return _CLASS_NAMES;
49      }
50  
51      protected SocialActivityFeedEntry doInterpret(
52              SocialActivity activity, ThemeDisplay themeDisplay)
53          throws Exception {
54  
55          String creatorUserName = getUserName(
56              activity.getUserId(), themeDisplay);
57          String receiverUserName = getUserName(
58              activity.getReceiverUserId(), themeDisplay);
59  
60          int activityType = activity.getType();
61  
62          JSONObject extraData = null;
63  
64          if (Validator.isNotNull(activity.getExtraData())) {
65              extraData = JSONFactoryUtil.createJSONObject(
66                  activity.getExtraData());
67          }
68  
69          // Link
70  
71          BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
72              activity.getClassPK());
73  
74          String link =
75              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
76                  "/blogs/find_entry?entryId=" + activity.getClassPK();
77  
78          // Title
79  
80          String groupName = StringPool.BLANK;
81  
82          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
83              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
84  
85              groupName = group.getDescriptiveName();
86          }
87  
88          String titlePattern = null;
89          Object[] titleArguments = null;
90  
91          if (activityType == BlogsActivityKeys.ADD_COMMENT) {
92              titlePattern = "activity-blogs-add-comment";
93  
94              if (Validator.isNotNull(groupName)) {
95                  titlePattern += "-in";
96              }
97  
98              titleArguments = new Object[] {
99                  creatorUserName, receiverUserName, groupName
100             };
101         }
102         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
103             titlePattern = "activity-blogs-add-entry";
104 
105             if (Validator.isNotNull(groupName)) {
106                 titlePattern += "-in";
107             }
108 
109             titleArguments = new Object[] {creatorUserName, groupName};
110         }
111 
112         String title = themeDisplay.translate(titlePattern, titleArguments);
113 
114         // Body
115 
116         StringBuilder sb = new StringBuilder();
117 
118         sb.append("<a href=\"");
119         sb.append(link);
120         sb.append("\">");
121 
122         if (activityType == BlogsActivityKeys.ADD_COMMENT) {
123             long messageId = extraData.getInt("messageId");
124 
125             try {
126                 MBMessage message = MBMessageLocalServiceUtil.getMessage(
127                     messageId);
128 
129                 sb.append(cleanContent(message.getBody()));
130             }
131             catch (NoSuchMessageException nsme) {
132                 SocialActivityLocalServiceUtil.deleteActivity(
133                     activity.getActivityId());
134 
135                 return null;
136             }
137         }
138         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
139             sb.append(entry.getTitle());
140         }
141 
142         sb.append("</a><br />");
143 
144         if (activityType == BlogsActivityKeys.ADD_ENTRY) {
145             sb.append(cleanContent(entry.getContent()));
146         }
147 
148         String body = sb.toString();
149 
150         return new SocialActivityFeedEntry(link, title, body);
151     }
152 
153     private static final String[] _CLASS_NAMES = new String[] {
154         BlogsEntry.class.getName()
155     };
156 
157 }