001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.Function;
025    import com.liferay.portal.kernel.util.Portal;
026    import com.liferay.portal.kernel.util.PortalUtil;
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.util.PropsValues;
031    import com.liferay.portlet.blogs.linkback.LinkbackConsumer;
032    import com.liferay.portlet.blogs.linkback.LinkbackConsumerUtil;
033    
034    /**
035     * @author Alexander Chow
036     * @author André de Oliveira
037     */
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,
079                            new String[] {StringPool.CLOSE_BRACKET, StringPool.OPEN_BRACKET},
080                            new String[] {"%5D", "%5B"});
081    
082                    StringBundler sb = new StringBundler(7);
083    
084                    sb.append("[...] ");
085                    sb.append(excerpt);
086                    sb.append(" [...] [url=");
087                    sb.append(url);
088                    sb.append("]");
089                    sb.append(themeDisplay.translate("read-more"));
090                    sb.append("[/url]");
091    
092                    return sb.toString();
093            }
094    
095            protected String buildBody(
096                    ThemeDisplay themeDisplay, String excerpt, String url) {
097    
098                    if (PropsValues.DISCUSSION_COMMENTS_FORMAT.equals("bbcode")) {
099                            return buildBBCodeBody(themeDisplay, excerpt, url);
100                    }
101    
102                    return buildHTMLBody(themeDisplay, excerpt, url);
103            }
104    
105            protected String buildEntryURL(BlogsEntry entry, ThemeDisplay themeDisplay)
106                    throws PortalException {
107    
108                    StringBundler sb = new StringBundler(4);
109    
110                    sb.append(PortalUtil.getLayoutFullURL(themeDisplay));
111                    sb.append(Portal.FRIENDLY_URL_SEPARATOR);
112                    sb.append("blogs/");
113                    sb.append(entry.getUrlTitle());
114    
115                    return sb.toString();
116            }
117    
118            protected String buildHTMLBody(
119                    ThemeDisplay themeDisplay, String excerpt, String url) {
120    
121                    StringBundler sb = new StringBundler(7);
122    
123                    sb.append("[...] ");
124                    sb.append(excerpt);
125                    sb.append(" [...] <a href=\"");
126                    sb.append(url);
127                    sb.append("\">");
128                    sb.append(themeDisplay.translate("read-more"));
129                    sb.append("</a>");
130    
131                    return sb.toString();
132            }
133    
134            private CommentManager _commentManager =
135                    CommentManagerUtil.getCommentManager();
136            private LinkbackConsumer _linkbackConsumer =
137                    LinkbackConsumerUtil.getLinkbackConsumer();
138    
139    }