001    /**
002     * Copyright (c) 2000-2010 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.social.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Ryan Park
031     */
032    public abstract class BaseSocialActivityInterpreter
033            implements SocialActivityInterpreter {
034    
035            public SocialActivityFeedEntry interpret(
036                    SocialActivity activity, ThemeDisplay themeDisplay) {
037    
038                    try {
039                            return doInterpret(activity, themeDisplay);
040                    }
041                    catch (Exception e) {
042                            _log.error("Unable to interpret activity", e);
043                    }
044    
045                    return null;
046            }
047    
048            protected String cleanContent(String content) {
049                    return StringUtil.shorten(HtmlUtil.extractText(content), 200);
050            }
051    
052            protected abstract SocialActivityFeedEntry doInterpret(
053                            SocialActivity activity, ThemeDisplay themeDisplay)
054                    throws Exception;
055    
056            protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
057                    try {
058                            if (groupId <= 0) {
059                                    return StringPool.BLANK;
060                            }
061    
062                            Group group = GroupLocalServiceUtil.getGroup(groupId);
063    
064                            String groupName = group.getDescriptiveName();
065    
066                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
067                                    return HtmlUtil.escape(groupName);
068                            }
069    
070                            String groupDisplayURL =
071                                    themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
072                                            "/my_places/view?groupId=" +  group.getGroupId();
073    
074                            if (group.hasPublicLayouts()) {
075                                    groupDisplayURL = groupDisplayURL + "&privateLayout=0";
076                            }
077                            else if (group.hasPrivateLayouts()) {
078                                    groupDisplayURL = groupDisplayURL + "&privateLayout=1";
079                            }
080                            else {
081                                    return HtmlUtil.escape(groupName);
082                            }
083    
084                            groupName =
085                                    "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
086                                            HtmlUtil.escape(groupName) + "</a>";
087    
088                            return groupName;
089                    }
090                    catch (Exception e) {
091                            return StringPool.BLANK;
092                    }
093            }
094    
095            protected String getUserName(long userId, ThemeDisplay themeDisplay) {
096                    try {
097                            if (userId <= 0) {
098                                    return StringPool.BLANK;
099                            }
100    
101                            User user = UserLocalServiceUtil.getUserById(userId);
102    
103                            if (user.getUserId() == themeDisplay.getUserId()) {
104                                    return HtmlUtil.escape(user.getFirstName());
105                            }
106    
107                            String userName = user.getFullName();
108    
109                            Group group = user.getGroup();
110    
111                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
112                                    return HtmlUtil.escape(userName);
113                            }
114    
115                            String userDisplayURL = user.getDisplayURL(themeDisplay);
116    
117                            userName =
118                                    "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
119                                            HtmlUtil.escape(userName) + "</a>";
120    
121                            return userName;
122                    }
123                    catch (Exception e) {
124                            return StringPool.BLANK;
125                    }
126            }
127    
128            protected String wrapLink(String link, String text) {
129                    StringBuilder sb = new StringBuilder();
130    
131                    sb.append("<a href=\"");
132                    sb.append(link);
133                    sb.append("\">");
134                    sb.append(text);
135                    sb.append("</a>");
136    
137                    return sb.toString();
138            }
139    
140            protected String wrapLink(
141                    String link, String key, ThemeDisplay themeDisplay) {
142    
143                    return wrapLink(link, themeDisplay.translate(key));
144            }
145    
146            private static Log _log = LogFactoryUtil.getLog(
147                    BaseSocialActivityInterpreter.class);
148    
149    }