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