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