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