001
014
015 package com.liferay.portlet.blogs.trackback;
016
017 import com.liferay.blogs.kernel.model.BlogsEntry;
018 import com.liferay.portal.kernel.comment.CommentManager;
019 import com.liferay.portal.kernel.comment.CommentManagerUtil;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.service.ServiceContext;
022 import com.liferay.portal.kernel.service.UserLocalServiceUtil;
023 import com.liferay.portal.kernel.theme.ThemeDisplay;
024 import com.liferay.portal.kernel.util.CharPool;
025 import com.liferay.portal.kernel.util.Function;
026 import com.liferay.portal.kernel.util.Portal;
027 import com.liferay.portal.kernel.util.PortalUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringUtil;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.blogs.linkback.LinkbackConsumer;
032 import com.liferay.portlet.blogs.linkback.LinkbackConsumerUtil;
033
034
038 public class TrackbackImpl implements Trackback {
039
040 @Override
041 public void addTrackback(
042 BlogsEntry entry, ThemeDisplay themeDisplay, String excerpt,
043 String url, String blogName, String title,
044 Function<String, ServiceContext> serviceContextFunction)
045 throws PortalException {
046
047 long userId = UserLocalServiceUtil.getDefaultUserId(
048 themeDisplay.getCompanyId());
049 long groupId = entry.getGroupId();
050 String className = BlogsEntry.class.getName();
051 long classPK = entry.getEntryId();
052
053 String body = buildBody(themeDisplay, excerpt, url);
054
055 long commentId = _commentManager.addComment(
056 userId, groupId, className, classPK, blogName, title, body,
057 serviceContextFunction);
058
059 String entryURL = buildEntryURL(entry, themeDisplay);
060
061 _linkbackConsumer.addNewTrackback(commentId, url, entryURL);
062 }
063
064 @Override
065 public void setCommentManager(CommentManager commentManager) {
066 _commentManager = commentManager;
067 }
068
069 @Override
070 public void setLinkbackConsumer(LinkbackConsumer linkbackConsumer) {
071 _linkbackConsumer = linkbackConsumer;
072 }
073
074 protected String buildBBCodeBody(
075 ThemeDisplay themeDisplay, String excerpt, String url) {
076
077 url = StringUtil.replace(
078 url, new char[] {CharPool.CLOSE_BRACKET, CharPool.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 buildBody(
095 ThemeDisplay themeDisplay, String excerpt, String url) {
096
097 if (PropsValues.DISCUSSION_COMMENTS_FORMAT.equals("bbcode")) {
098 return buildBBCodeBody(themeDisplay, excerpt, url);
099 }
100
101 return buildHTMLBody(themeDisplay, excerpt, url);
102 }
103
104 protected String buildEntryURL(BlogsEntry entry, ThemeDisplay themeDisplay)
105 throws PortalException {
106
107 StringBundler sb = new StringBundler(4);
108
109 sb.append(PortalUtil.getLayoutFullURL(themeDisplay));
110 sb.append(Portal.FRIENDLY_URL_SEPARATOR);
111 sb.append("blogs/");
112 sb.append(entry.getUrlTitle());
113
114 return sb.toString();
115 }
116
117 protected String buildHTMLBody(
118 ThemeDisplay themeDisplay, String excerpt, String url) {
119
120 StringBundler sb = new StringBundler(7);
121
122 sb.append("[...] ");
123 sb.append(excerpt);
124 sb.append(" [...] <a href=\"");
125 sb.append(url);
126 sb.append("\">");
127 sb.append(themeDisplay.translate("read-more"));
128 sb.append("</a>");
129
130 return sb.toString();
131 }
132
133 private CommentManager _commentManager =
134 CommentManagerUtil.getCommentManager();
135 private LinkbackConsumer _linkbackConsumer =
136 LinkbackConsumerUtil.getLinkbackConsumer();
137
138 }