001
014
015 package com.liferay.portlet.social.model;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONException;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.trash.TrashHandler;
025 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
026 import com.liferay.portal.kernel.util.HtmlUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.model.Group;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.security.permission.ActionKeys;
034 import com.liferay.portal.security.permission.PermissionChecker;
035 import com.liferay.portal.service.GroupLocalServiceUtil;
036 import com.liferay.portal.service.ServiceContext;
037 import com.liferay.portal.service.UserLocalServiceUtil;
038 import com.liferay.portal.theme.ThemeDisplay;
039 import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
040 import com.liferay.portlet.social.service.SocialActivitySetLocalServiceUtil;
041 import com.liferay.portlet.social.service.persistence.SocialActivityUtil;
042 import com.liferay.portlet.trash.util.TrashUtil;
043
044 import java.util.List;
045
046 import javax.portlet.PortletURL;
047
048
052 public abstract class BaseSocialActivityInterpreter
053 implements SocialActivityInterpreter {
054
055 public String getSelector() {
056 return StringPool.BLANK;
057 }
058
059 public SocialActivityFeedEntry interpret(
060 SocialActivity activity, ServiceContext serviceContext) {
061
062 try {
063 return doInterpret(activity, serviceContext);
064 }
065 catch (Exception e) {
066 _log.error("Unable to interpret activity", e);
067 }
068
069 return null;
070 }
071
072 public SocialActivityFeedEntry interpret(
073 SocialActivitySet activitySet, ServiceContext serviceContext) {
074
075 try {
076 List<SocialActivity> activities =
077 SocialActivityLocalServiceUtil.getActivitySetActivities(
078 activitySet.getActivitySetId(), 0, 1);
079
080 if (!activities.isEmpty()) {
081 SocialActivity activity = activities.get(0);
082
083 return doInterpret(activity, serviceContext);
084 }
085 }
086 catch (Exception e) {
087 _log.error("Unable to interpret activity set", e);
088 }
089
090 return null;
091 }
092
093 public void updateActivitySet(long activityId)
094 throws PortalException, SystemException {
095
096 SocialActivity activity = SocialActivityUtil.fetchByPrimaryKey(
097 activityId);
098
099 if ((activity == null) || (activity.getActivitySetId() > 0)) {
100 return;
101 }
102
103 long activitySetId = getActivitySetId(activityId);
104
105 if (activitySetId > 0) {
106 SocialActivitySetLocalServiceUtil.incrementActivityCount(
107 activitySetId, activityId);
108 }
109 else {
110 SocialActivitySetLocalServiceUtil.addActivitySet(activityId);
111 }
112 }
113
114 protected String buildLink(String link, String text) {
115 StringBundler sb = new StringBundler(5);
116
117 sb.append("<a href=\"");
118 sb.append(link);
119 sb.append("\">");
120 sb.append(text);
121 sb.append("</a>");
122
123 return sb.toString();
124 }
125
126
129 protected String cleanContent(String content) {
130 return StringUtil.shorten(HtmlUtil.extractText(content), 200);
131 }
132
133 protected SocialActivityFeedEntry doInterpret(
134 SocialActivity activity, ServiceContext serviceContext)
135 throws Exception {
136
137 ThemeDisplay themeDisplay = serviceContext.getThemeDisplay();
138
139 SocialActivityFeedEntry socialActivityFeedEntry = doInterpret(
140 activity, themeDisplay);
141
142 if (socialActivityFeedEntry !=
143 _deprecatedMarkerSocialActivityFeedEntry) {
144
145 return socialActivityFeedEntry;
146 }
147
148 PermissionChecker permissionChecker =
149 themeDisplay.getPermissionChecker();
150
151 if (!hasPermissions(
152 permissionChecker, activity, ActionKeys.VIEW, serviceContext)) {
153
154 return null;
155 }
156
157 String link = getLink(activity, serviceContext);
158
159 String title = getTitle(activity, serviceContext);
160
161 if (Validator.isNull(title)) {
162 return null;
163 }
164
165 String body = getBody(activity, serviceContext);
166
167 return new SocialActivityFeedEntry(link, title, body);
168 }
169
170
173 protected SocialActivityFeedEntry doInterpret(
174 SocialActivity activity, ThemeDisplay themeDisplay)
175 throws Exception {
176
177 return _deprecatedMarkerSocialActivityFeedEntry;
178 }
179
180 protected long getActivitySetId(long activityId) {
181 return 0;
182 }
183
184 protected String getBody(
185 SocialActivity activity, ServiceContext serviceContext)
186 throws Exception {
187
188 return StringPool.BLANK;
189 }
190
191 protected String getEntryTitle(
192 SocialActivity activity, ServiceContext serviceContext)
193 throws Exception {
194
195 return StringPool.BLANK;
196 }
197
198 protected String getGroupName(long groupId, ServiceContext serviceContext) {
199 try {
200 if (groupId <= 0) {
201 return StringPool.BLANK;
202 }
203
204 Group group = GroupLocalServiceUtil.getGroup(groupId);
205
206 String groupName = group.getDescriptiveName();
207
208 if (group.getGroupId() == serviceContext.getScopeGroupId()) {
209 return HtmlUtil.escape(groupName);
210 }
211
212 String groupDisplayURL =
213 serviceContext.getPortalURL() + serviceContext.getPathMain() +
214 "/my_sites/view?groupId=" + group.getGroupId();
215
216 if (group.hasPublicLayouts()) {
217 groupDisplayURL = groupDisplayURL + "&privateLayout=0";
218 }
219 else if (group.hasPrivateLayouts()) {
220 groupDisplayURL = groupDisplayURL + "&privateLayout=1";
221 }
222 else {
223 return HtmlUtil.escape(groupName);
224 }
225
226 groupName =
227 "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
228 HtmlUtil.escape(groupName) + "</a>";
229
230 return groupName;
231 }
232 catch (Exception e) {
233 return StringPool.BLANK;
234 }
235 }
236
237
241 protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
242 try {
243 if (groupId <= 0) {
244 return StringPool.BLANK;
245 }
246
247 Group group = GroupLocalServiceUtil.getGroup(groupId);
248
249 String groupName = group.getDescriptiveName();
250
251 if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
252 return HtmlUtil.escape(groupName);
253 }
254
255 String groupDisplayURL =
256 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
257 "/my_sites/view?groupId=" + group.getGroupId();
258
259 if (group.hasPublicLayouts()) {
260 groupDisplayURL = groupDisplayURL + "&privateLayout=0";
261 }
262 else if (group.hasPrivateLayouts()) {
263 groupDisplayURL = groupDisplayURL + "&privateLayout=1";
264 }
265 else {
266 return HtmlUtil.escape(groupName);
267 }
268
269 groupName =
270 "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
271 HtmlUtil.escape(groupName) + "</a>";
272
273 return groupName;
274 }
275 catch (Exception e) {
276 return StringPool.BLANK;
277 }
278 }
279
280 protected String getJSONValue(String json, String key) {
281 return getJSONValue(json, key, StringPool.BLANK);
282 }
283
284 protected String getJSONValue(
285 String json, String key, String defaultValue) {
286
287 if (Validator.isNull(json)) {
288 return HtmlUtil.escape(defaultValue);
289 }
290
291 try {
292 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
293 json);
294
295 String value = extraDataJSONObject.getString(key);
296
297 if (Validator.isNotNull(value)) {
298 return HtmlUtil.escape(value);
299 }
300 }
301 catch (JSONException jsone) {
302 _log.error("Unable to create a JSON object from " + json);
303 }
304
305 return HtmlUtil.escape(defaultValue);
306 }
307
308 protected String getLink(
309 SocialActivity activity, ServiceContext serviceContext)
310 throws Exception {
311
312 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
313 activity.getClassName());
314
315 long classPK = activity.getClassPK();
316
317 if ((trashHandler != null) &&
318 (trashHandler.isInTrash(classPK) ||
319 trashHandler.isInTrashContainer(classPK))) {
320
321 PortletURL portletURL = TrashUtil.getViewContentURL(
322 serviceContext.getRequest(), activity.getClassName(), classPK);
323
324 if (portletURL == null) {
325 return null;
326 }
327
328 return portletURL.toString();
329 }
330
331 String path = getPath(activity, serviceContext);
332
333 if (Validator.isNull(path)) {
334 return null;
335 }
336
337 if (!path.startsWith(StringPool.SLASH)) {
338 return path;
339 }
340
341 return serviceContext.getPortalURL() + serviceContext.getPathMain() +
342 path;
343 }
344
345 protected String getPath(
346 SocialActivity activity, ServiceContext serviceContext)
347 throws Exception {
348
349 return StringPool.BLANK;
350 }
351
352 protected String getTitle(
353 SocialActivity activity, ServiceContext serviceContext)
354 throws Exception {
355
356 String groupName = StringPool.BLANK;
357
358 if (activity.getGroupId() != serviceContext.getScopeGroupId()) {
359 groupName = getGroupName(activity.getGroupId(), serviceContext);
360 }
361
362 String titlePattern = getTitlePattern(groupName, activity);
363
364 if (Validator.isNull(titlePattern)) {
365 return null;
366 }
367
368 String link = getLink(activity, serviceContext);
369
370 String entryTitle = getEntryTitle(activity, serviceContext);
371
372 Object[] titleArguments = getTitleArguments(
373 groupName, activity, link, entryTitle, serviceContext);
374
375 return serviceContext.translate(titlePattern, titleArguments);
376 }
377
378 protected Object[] getTitleArguments(
379 String groupName, SocialActivity activity, String link,
380 String title, ServiceContext serviceContext)
381 throws Exception {
382
383 String userName = getUserName(activity.getUserId(), serviceContext);
384
385 return new Object[] {groupName, userName, wrapLink(link, title)};
386 }
387
388 protected String getTitlePattern(String groupName, SocialActivity activity)
389 throws Exception {
390
391 return StringPool.BLANK;
392 }
393
394 protected String getUserName(long userId, ServiceContext serviceContext) {
395 try {
396 if (userId <= 0) {
397 return StringPool.BLANK;
398 }
399
400 User user = UserLocalServiceUtil.getUserById(userId);
401
402 if (user.getUserId() == serviceContext.getUserId()) {
403 return HtmlUtil.escape(user.getFirstName());
404 }
405
406 String userName = user.getFullName();
407
408 Group group = user.getGroup();
409
410 if (group.getGroupId() == serviceContext.getScopeGroupId()) {
411 return HtmlUtil.escape(userName);
412 }
413
414 String userDisplayURL = user.getDisplayURL(
415 serviceContext.getThemeDisplay());
416
417 userName =
418 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
419 HtmlUtil.escape(userName) + "</a>";
420
421 return userName;
422 }
423 catch (Exception e) {
424 return StringPool.BLANK;
425 }
426 }
427
428
432 protected String getUserName(long userId, ThemeDisplay themeDisplay) {
433 try {
434 if (userId <= 0) {
435 return StringPool.BLANK;
436 }
437
438 User user = UserLocalServiceUtil.getUserById(userId);
439
440 if (user.getUserId() == themeDisplay.getUserId()) {
441 return HtmlUtil.escape(user.getFirstName());
442 }
443
444 String userName = user.getFullName();
445
446 Group group = user.getGroup();
447
448 if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
449 return HtmlUtil.escape(userName);
450 }
451
452 String userDisplayURL = user.getDisplayURL(themeDisplay);
453
454 userName =
455 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
456 HtmlUtil.escape(userName) + "</a>";
457
458 return userName;
459 }
460 catch (Exception e) {
461 return StringPool.BLANK;
462 }
463 }
464
465
469 protected String getValue(String json, String key, String defaultValue) {
470 return getJSONValue(json, key, defaultValue);
471 }
472
473 protected boolean hasPermissions(
474 PermissionChecker permissionChecker, SocialActivity activity,
475 String actionId, ServiceContext serviceContext)
476 throws Exception {
477
478 return false;
479 }
480
481 protected String wrapLink(String link, String title) {
482 title = HtmlUtil.escape(title);
483
484 if (link == null) {
485 return title;
486 }
487
488 return buildLink(link, title);
489 }
490
491 protected String wrapLink(
492 String link, String key, ServiceContext serviceContext) {
493
494 String title = serviceContext.translate(HtmlUtil.escape(key));
495
496 if (link == null) {
497 return title;
498 }
499
500 return buildLink(link, title);
501 }
502
503 private static Log _log = LogFactoryUtil.getLog(
504 BaseSocialActivityInterpreter.class);
505
506 private SocialActivityFeedEntry _deprecatedMarkerSocialActivityFeedEntry =
507 new SocialActivityFeedEntry(StringPool.BLANK, StringPool.BLANK);
508
509 }