1
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
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 }