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.social.model;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.HtmlUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.model.Group;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.service.GroupLocalServiceUtil;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  
36  /**
37   * <a href="BaseSocialActivityInterpreter.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Ryan Park
42   *
43   */
44  public abstract class BaseSocialActivityInterpreter
45      implements SocialActivityInterpreter {
46  
47      public SocialActivityFeedEntry interpret(
48          SocialActivity activity, ThemeDisplay themeDisplay) {
49  
50          try {
51              return doInterpret(activity, themeDisplay);
52          }
53          catch (Exception e) {
54              _log.error("Unable to interpret activity", e);
55          }
56  
57          return null;
58      }
59  
60      protected String cleanContent(String content) {
61          return StringUtil.shorten(HtmlUtil.extractText(content), 200);
62      }
63  
64      protected abstract SocialActivityFeedEntry doInterpret(
65              SocialActivity activity, ThemeDisplay themeDisplay)
66          throws Exception;
67  
68      protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
69          try {
70              if (groupId <= 0) {
71                  return StringPool.BLANK;
72              }
73  
74              Group group = GroupLocalServiceUtil.getGroup(groupId);
75  
76              String groupName = group.getDescriptiveName();
77  
78              if ((group.getGroupId() == themeDisplay.getScopeGroupId()) ||
79                  !group.hasPublicLayouts()) {
80  
81                  return groupName;
82              }
83  
84              String groupDisplayURL =
85                  themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
86                      "/my_places/view?groupId=" +  group.getGroupId() +
87                          "&privateLayout=0";
88  
89              groupName =
90                  "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
91                      groupName + "</a>";
92  
93              return groupName;
94          }
95          catch (Exception e) {
96              return StringPool.BLANK;
97          }
98      }
99  
100     protected String getUserName(long userId, ThemeDisplay themeDisplay) {
101         try {
102             if (userId <= 0) {
103                 return StringPool.BLANK;
104             }
105 
106             User user = UserLocalServiceUtil.getUserById(userId);
107 
108             if (user.getUserId() == themeDisplay.getUserId()) {
109                 return user.getFirstName();
110             }
111 
112             String userName = user.getFullName();
113 
114             Group group = user.getGroup();
115 
116             if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
117                 return userName;
118             }
119 
120             String userDisplayURL = user.getDisplayURL(themeDisplay);
121 
122             userName =
123                 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
124                     userName + "</a>";
125 
126             return userName;
127         }
128         catch (Exception e) {
129             return StringPool.BLANK;
130         }
131     }
132 
133     protected String wrapLink(String link, String text) {
134         StringBuilder sb = new StringBuilder();
135 
136         sb.append("<a href=\"");
137         sb.append(link);
138         sb.append("\">");
139         sb.append(text);
140         sb.append("</a>");
141 
142         return sb.toString();
143     }
144 
145     protected String wrapLink(
146         String link, String key, ThemeDisplay themeDisplay) {
147 
148         return wrapLink(link, themeDisplay.translate(key));
149     }
150 
151     private static Log _log =
152         LogFactoryUtil.getLog(BaseSocialActivityInterpreter.class);
153 
154 }