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.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portlet.social.service.persistence.SocialRequestUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Amos Fong
033     */
034    public abstract class BaseSocialRequestInterpreter
035            implements SocialRequestInterpreter {
036    
037            public String getUserName(long userId, ThemeDisplay themeDisplay) {
038                    try {
039                            if (userId <= 0) {
040                                    return StringPool.BLANK;
041                            }
042    
043                            User user = UserLocalServiceUtil.getUserById(userId);
044    
045                            if (user.getUserId() == themeDisplay.getUserId()) {
046                                    return HtmlUtil.escape(user.getFirstName());
047                            }
048    
049                            String userName = user.getFullName();
050    
051                            Group group = user.getGroup();
052    
053                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
054                                    return HtmlUtil.escape(userName);
055                            }
056    
057                            String userDisplayURL = user.getDisplayURL(themeDisplay);
058    
059                            return "<a href=\"" + userDisplayURL + "\">" +
060                                    HtmlUtil.escape(userName) + "</a>";
061                    }
062                    catch (Exception e) {
063                            return StringPool.BLANK;
064                    }
065            }
066    
067            public String getUserNameLink(long userId, ThemeDisplay themeDisplay) {
068                    try {
069                            if (userId <= 0) {
070                                    return StringPool.BLANK;
071                            }
072    
073                            User user = UserLocalServiceUtil.getUserById(userId);
074    
075                            String userName = user.getFullName();
076    
077                            String userDisplayURL = user.getDisplayURL(themeDisplay);
078    
079                            return "<a href=\"" + userDisplayURL + "\">" +
080                                    HtmlUtil.escape(userName) + "</a>";
081                    }
082                    catch (Exception e) {
083                            return StringPool.BLANK;
084                    }
085            }
086    
087            public SocialRequestFeedEntry interpret(
088                    SocialRequest request, ThemeDisplay themeDisplay) {
089    
090                    try {
091                            return doInterpret(request, themeDisplay);
092                    }
093                    catch (Exception e) {
094                            _log.error("Unable to interpret request", e);
095                    }
096    
097                    return null;
098            }
099    
100            public boolean processConfirmation(
101                    SocialRequest request, ThemeDisplay themeDisplay) {
102    
103                    try {
104                            return doProcessConfirmation(request, themeDisplay);
105                    }
106                    catch (Exception e) {
107                            _log.error("Unable to process confirmation", e);
108                    }
109    
110                    return false;
111            }
112    
113            public void processDuplicateRequestsFromUser(
114                            SocialRequest request, int oldStatus)
115                    throws SystemException {
116    
117                    List<SocialRequest> requests = SocialRequestUtil.findByU_C_C_T_S(
118                            request.getUserId(), request.getClassNameId(), request.getClassPK(),
119                            request.getType(), oldStatus);
120    
121                    int newStatus = request.getStatus();
122    
123                    for (SocialRequest curRequest : requests) {
124                            curRequest.setStatus(newStatus);
125    
126                            SocialRequestUtil.update(curRequest);
127                    }
128            }
129    
130            public void processDuplicateRequestsToUser(
131                            SocialRequest request, int oldStatus)
132                    throws SystemException {
133    
134                    List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
135                            request.getClassNameId(), request.getClassPK(), request.getType(),
136                            request.getReceiverUserId(), oldStatus);
137    
138                    int newStatus = request.getStatus();
139    
140                    for (SocialRequest curRequest : requests) {
141                            curRequest.setStatus(newStatus);
142    
143                            SocialRequestUtil.update(curRequest);
144                    }
145            }
146    
147            public boolean processRejection(
148                    SocialRequest request, ThemeDisplay themeDisplay) {
149    
150                    try {
151                            return doProcessRejection(request, themeDisplay);
152                    }
153                    catch (Exception e) {
154                            _log.error("Unable to process rejection", e);
155                    }
156    
157                    return false;
158            }
159    
160            protected abstract SocialRequestFeedEntry doInterpret(
161                            SocialRequest request, ThemeDisplay themeDisplay)
162                    throws Exception;
163    
164            protected abstract boolean doProcessConfirmation(
165                            SocialRequest request, ThemeDisplay themeDisplay)
166                    throws Exception;
167    
168            protected boolean doProcessRejection(
169                            SocialRequest request, ThemeDisplay themeDisplay)
170                    throws Exception {
171    
172                    return true;
173            }
174    
175            private static Log _log = LogFactoryUtil.getLog(
176                    BaseSocialRequestInterpreter.class);
177    
178    }