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.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                            userName =
060                                    "<a href=\"" + userDisplayURL + "\">" +
061                                            HtmlUtil.escape(userName) + "</a>";
062    
063                            return userName;
064                    }
065                    catch (Exception e) {
066                            return StringPool.BLANK;
067                    }
068            }
069    
070            public SocialRequestFeedEntry interpret(
071                    SocialRequest request, ThemeDisplay themeDisplay) {
072    
073                    try {
074                            return doInterpret(request, themeDisplay);
075                    }
076                    catch (Exception e) {
077                            _log.error("Unable to interpret request", e);
078                    }
079    
080                    return null;
081            }
082    
083            public boolean processConfirmation(
084                    SocialRequest request, ThemeDisplay themeDisplay) {
085    
086                    try {
087                            return doProcessConfirmation(request, themeDisplay);
088                    }
089                    catch (Exception e) {
090                            _log.error("Unable to process confirmation", e);
091                    }
092    
093                    return false;
094            }
095    
096            public void processDuplicateRequestsFromUser(
097                            SocialRequest request, int oldStatus)
098                    throws SystemException {
099    
100                    List<SocialRequest> requests = SocialRequestUtil.findByU_C_C_T_S(
101                            request.getUserId(), request.getClassNameId(), request.getClassPK(),
102                            request.getType(), oldStatus);
103    
104                    int newStatus = request.getStatus();
105    
106                    for (SocialRequest curRequest : requests) {
107                            curRequest.setStatus(newStatus);
108    
109                            SocialRequestUtil.update(curRequest, false);
110                    }
111            }
112    
113            public void processDuplicateRequestsToUser(
114                            SocialRequest request, int oldStatus)
115                    throws SystemException {
116    
117                    List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
118                            request.getClassNameId(), request.getClassPK(), request.getType(),
119                            request.getReceiverUserId(), oldStatus);
120    
121                    int newStatus = request.getStatus();
122    
123                    for (SocialRequest curRequest : requests) {
124                            curRequest.setStatus(newStatus);
125    
126                            SocialRequestUtil.update(curRequest, false);
127                    }
128            }
129    
130            public boolean processRejection(
131                    SocialRequest request, ThemeDisplay themeDisplay) {
132    
133                    try {
134                            return doProcessRejection(request, themeDisplay);
135                    }
136                    catch (Exception e) {
137                            _log.error("Unable to process rejection", e);
138                    }
139    
140                    return false;
141            }
142    
143            protected abstract SocialRequestFeedEntry doInterpret(
144                            SocialRequest request, ThemeDisplay themeDisplay)
145                    throws Exception;
146    
147            protected abstract boolean doProcessConfirmation(
148                            SocialRequest request, ThemeDisplay themeDisplay)
149                    throws Exception;
150    
151            protected boolean doProcessRejection(
152                            SocialRequest request, ThemeDisplay themeDisplay)
153                    throws Exception {
154    
155                    return true;
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(
159                    BaseSocialRequestInterpreter.class);
160    
161    }