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