001
014
015 package com.liferay.portlet.blogs.trackback;
016
017 import com.liferay.portal.kernel.comment.CommentManager;
018 import com.liferay.portal.kernel.comment.CommentManagerUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.util.Function;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.UserLocalServiceUtil;
026 import com.liferay.portal.theme.ThemeDisplay;
027 import com.liferay.portal.util.Portal;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portlet.blogs.linkback.LinkbackConsumer;
030 import com.liferay.portlet.blogs.linkback.LinkbackConsumerUtil;
031 import com.liferay.portlet.blogs.model.BlogsEntry;
032
033
037 public class TrackbackImpl implements Trackback {
038
039 @Override
040 public void addTrackback(
041 BlogsEntry entry, ThemeDisplay themeDisplay, String excerpt,
042 String url, String blogName, String title,
043 Function<String, ServiceContext> serviceContextFunction)
044 throws PortalException {
045
046 long userId = UserLocalServiceUtil.getDefaultUserId(
047 themeDisplay.getCompanyId());
048 long groupId = entry.getGroupId();
049 String className = BlogsEntry.class.getName();
050 long classPK = entry.getEntryId();
051
052 String body = buildBody(themeDisplay, excerpt, url);
053
054 long commentId = _commentManager.addComment(
055 userId, groupId, className, classPK, blogName, title, body,
056 serviceContextFunction);
057
058 String entryURL = buildEntryURL(entry, themeDisplay);
059
060 _linkbackConsumer.addNewTrackback(commentId, url, entryURL);
061 }
062
063 @Override
064 public void setCommentManager(CommentManager commentManager) {
065 _commentManager = commentManager;
066 }
067
068 @Override
069 public void setLinkbackConsumer(LinkbackConsumer linkbackConsumer) {
070 _linkbackConsumer = linkbackConsumer;
071 }
072
073 protected String buildBody(
074 ThemeDisplay themeDisplay, String excerpt, String url) {
075
076 url = StringUtil.replace(
077 url,
078 new String[] {StringPool.CLOSE_BRACKET, StringPool.OPEN_BRACKET},
079 new String[] {"%5D", "%5B"});
080
081 StringBundler sb = new StringBundler(7);
082
083 sb.append("[...] ");
084 sb.append(excerpt);
085 sb.append(" [...] [url=");
086 sb.append(url);
087 sb.append("]");
088 sb.append(themeDisplay.translate("read-more"));
089 sb.append("[/url]");
090
091 return sb.toString();
092 }
093
094 protected String buildEntryURL(BlogsEntry entry, ThemeDisplay themeDisplay)
095 throws PortalException {
096
097 StringBundler sb = new StringBundler(4);
098
099 sb.append(PortalUtil.getLayoutFullURL(themeDisplay));
100 sb.append(Portal.FRIENDLY_URL_SEPARATOR);
101 sb.append("blogs/");
102 sb.append(entry.getUrlTitle());
103
104 return sb.toString();
105 }
106
107 private CommentManager _commentManager =
108 CommentManagerUtil.getCommentManager();
109 private LinkbackConsumer _linkbackConsumer =
110 LinkbackConsumerUtil.getLinkbackConsumer();
111
112 }