1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.social.model;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.service.GroupLocalServiceUtil;
25  import com.liferay.portal.service.UserLocalServiceUtil;
26  import com.liferay.portal.theme.ThemeDisplay;
27  
28  /**
29   * <a href="BaseSocialActivityInterpreter.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Ryan Park
34   */
35  public abstract class BaseSocialActivityInterpreter
36      implements SocialActivityInterpreter {
37  
38      public SocialActivityFeedEntry interpret(
39          SocialActivity activity, ThemeDisplay themeDisplay) {
40  
41          try {
42              return doInterpret(activity, themeDisplay);
43          }
44          catch (Exception e) {
45              _log.error("Unable to interpret activity", e);
46          }
47  
48          return null;
49      }
50  
51      protected String cleanContent(String content) {
52          return StringUtil.shorten(HtmlUtil.extractText(content), 200);
53      }
54  
55      protected abstract SocialActivityFeedEntry doInterpret(
56              SocialActivity activity, ThemeDisplay themeDisplay)
57          throws Exception;
58  
59      protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
60          try {
61              if (groupId <= 0) {
62                  return StringPool.BLANK;
63              }
64  
65              Group group = GroupLocalServiceUtil.getGroup(groupId);
66  
67              String groupName = group.getDescriptiveName();
68  
69              if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
70                  return HtmlUtil.escape(groupName);
71              }
72  
73              String groupDisplayURL =
74                  themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
75                      "/my_places/view?groupId=" +  group.getGroupId();
76  
77              if (group.hasPublicLayouts()) {
78                  groupDisplayURL = groupDisplayURL + "&privateLayout=0";
79              }
80              else if (group.hasPrivateLayouts()) {
81                  groupDisplayURL = groupDisplayURL + "&privateLayout=1";
82              }
83              else {
84                  return HtmlUtil.escape(groupName);
85              }
86  
87              groupName =
88                  "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
89                      HtmlUtil.escape(groupName) + "</a>";
90  
91              return groupName;
92          }
93          catch (Exception e) {
94              return StringPool.BLANK;
95          }
96      }
97  
98      protected String getUserName(long userId, ThemeDisplay themeDisplay) {
99          try {
100             if (userId <= 0) {
101                 return StringPool.BLANK;
102             }
103 
104             User user = UserLocalServiceUtil.getUserById(userId);
105 
106             if (user.getUserId() == themeDisplay.getUserId()) {
107                 return HtmlUtil.escape(user.getFirstName());
108             }
109 
110             String userName = user.getFullName();
111 
112             Group group = user.getGroup();
113 
114             if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
115                 return HtmlUtil.escape(userName);
116             }
117 
118             String userDisplayURL = user.getDisplayURL(themeDisplay);
119 
120             userName =
121                 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
122                     HtmlUtil.escape(userName) + "</a>";
123 
124             return userName;
125         }
126         catch (Exception e) {
127             return StringPool.BLANK;
128         }
129     }
130 
131     protected String wrapLink(String link, String text) {
132         StringBuilder sb = new StringBuilder();
133 
134         sb.append("<a href=\"");
135         sb.append(link);
136         sb.append("\">");
137         sb.append(text);
138         sb.append("</a>");
139 
140         return sb.toString();
141     }
142 
143     protected String wrapLink(
144         String link, String key, ThemeDisplay themeDisplay) {
145 
146         return wrapLink(link, themeDisplay.translate(key));
147     }
148 
149     private static Log _log = LogFactoryUtil.getLog(
150         BaseSocialActivityInterpreter.class);
151 
152 }