1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class BaseSocialActivityInterpreter
44      implements SocialActivityInterpreter {
45  
46      public SocialActivityFeedEntry interpret(
47          SocialActivity activity, ThemeDisplay themeDisplay) {
48  
49          try {
50              return doInterpret(activity, themeDisplay);
51          }
52          catch (Exception e) {
53              _log.error("Unable to interpret activity", e);
54          }
55  
56          return null;
57      }
58  
59      protected String cleanContent(String content) {
60          return StringUtil.shorten(HtmlUtil.extractText(content), 200);
61      }
62  
63      protected abstract SocialActivityFeedEntry doInterpret(
64              SocialActivity activity, ThemeDisplay themeDisplay)
65          throws Exception;
66  
67      protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
68          try {
69              if (groupId <= 0) {
70                  return StringPool.BLANK;
71              }
72  
73              Group group = GroupLocalServiceUtil.getGroup(groupId);
74  
75              String groupName = group.getDescriptiveName();
76  
77              if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
78                  return groupName;
79              }
80  
81              String groupDisplayURL =
82                  themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
83                      "/my_places/view?groupId=" +  group.getGroupId();
84  
85              if (group.hasPublicLayouts()) {
86                  groupDisplayURL = groupDisplayURL + "&privateLayout=0";
87              }
88              else if (group.hasPrivateLayouts()) {
89                  groupDisplayURL = groupDisplayURL + "&privateLayout=1";
90              }
91              else {
92                  return groupName;
93              }
94  
95              groupName =
96                  "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
97                      groupName + "</a>";
98  
99              return groupName;
100         }
101         catch (Exception e) {
102             return StringPool.BLANK;
103         }
104     }
105 
106     protected String getUserName(long userId, ThemeDisplay themeDisplay) {
107         try {
108             if (userId <= 0) {
109                 return StringPool.BLANK;
110             }
111 
112             User user = UserLocalServiceUtil.getUserById(userId);
113 
114             if (user.getUserId() == themeDisplay.getUserId()) {
115                 return user.getFirstName();
116             }
117 
118             String userName = user.getFullName();
119 
120             Group group = user.getGroup();
121 
122             if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
123                 return userName;
124             }
125 
126             String userDisplayURL = user.getDisplayURL(themeDisplay);
127 
128             userName =
129                 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
130                     userName + "</a>";
131 
132             return userName;
133         }
134         catch (Exception e) {
135             return StringPool.BLANK;
136         }
137     }
138 
139     protected String wrapLink(String link, String text) {
140         StringBuilder sb = new StringBuilder();
141 
142         sb.append("<a href=\"");
143         sb.append(link);
144         sb.append("\">");
145         sb.append(text);
146         sb.append("</a>");
147 
148         return sb.toString();
149     }
150 
151     protected String wrapLink(
152         String link, String key, ThemeDisplay themeDisplay) {
153 
154         return wrapLink(link, themeDisplay.translate(key));
155     }
156 
157     private static Log _log =
158         LogFactoryUtil.getLog(BaseSocialActivityInterpreter.class);
159 
160 }