001    /**
002     * Copyright (c) 2000-present 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portlet.social.service.persistence.SocialRequestUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Amos Fong
032     */
033    public abstract class BaseSocialRequestInterpreter
034            implements SocialRequestInterpreter {
035    
036            public String getUserName(long userId, ThemeDisplay themeDisplay) {
037                    try {
038                            if (userId <= 0) {
039                                    return StringPool.BLANK;
040                            }
041    
042                            User user = UserLocalServiceUtil.getUserById(userId);
043    
044                            if (user.getUserId() == themeDisplay.getUserId()) {
045                                    return HtmlUtil.escape(user.getFirstName());
046                            }
047    
048                            String userName = user.getFullName();
049    
050                            Group group = user.getGroup();
051    
052                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
053                                    return HtmlUtil.escape(userName);
054                            }
055    
056                            String userDisplayURL = user.getDisplayURL(themeDisplay);
057    
058                            return "<a href=\"" + userDisplayURL + "\">" +
059                                    HtmlUtil.escape(userName) + "</a>";
060                    }
061                    catch (Exception e) {
062                            return StringPool.BLANK;
063                    }
064            }
065    
066            public String getUserNameLink(long userId, ThemeDisplay themeDisplay) {
067                    try {
068                            if (userId <= 0) {
069                                    return StringPool.BLANK;
070                            }
071    
072                            User user = UserLocalServiceUtil.getUserById(userId);
073    
074                            String userName = user.getFullName();
075    
076                            String userDisplayURL = user.getDisplayURL(themeDisplay);
077    
078                            return "<a href=\"" + userDisplayURL + "\">" +
079                                    HtmlUtil.escape(userName) + "</a>";
080                    }
081                    catch (Exception e) {
082                            return StringPool.BLANK;
083                    }
084            }
085    
086            @Override
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            @Override
101            public boolean processConfirmation(
102                    SocialRequest request, ThemeDisplay themeDisplay) {
103    
104                    try {
105                            return doProcessConfirmation(request, themeDisplay);
106                    }
107                    catch (Exception e) {
108                            _log.error("Unable to process confirmation", e);
109                    }
110    
111                    return false;
112            }
113    
114            public void processDuplicateRequestsFromUser(
115                    SocialRequest request, int oldStatus) {
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    
133                    List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
134                            request.getClassNameId(), request.getClassPK(), request.getType(),
135                            request.getReceiverUserId(), oldStatus);
136    
137                    int newStatus = request.getStatus();
138    
139                    for (SocialRequest curRequest : requests) {
140                            curRequest.setStatus(newStatus);
141    
142                            SocialRequestUtil.update(curRequest);
143                    }
144            }
145    
146            @Override
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 final Log _log = LogFactoryUtil.getLog(
176                    BaseSocialRequestInterpreter.class);
177    
178    }