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            @Override
088            public SocialRequestFeedEntry interpret(
089                    SocialRequest request, ThemeDisplay themeDisplay) {
090    
091                    try {
092                            return doInterpret(request, themeDisplay);
093                    }
094                    catch (Exception e) {
095                            _log.error("Unable to interpret request", e);
096                    }
097    
098                    return null;
099            }
100    
101            @Override
102            public boolean processConfirmation(
103                    SocialRequest request, ThemeDisplay themeDisplay) {
104    
105                    try {
106                            return doProcessConfirmation(request, themeDisplay);
107                    }
108                    catch (Exception e) {
109                            _log.error("Unable to process confirmation", e);
110                    }
111    
112                    return false;
113            }
114    
115            public void processDuplicateRequestsFromUser(
116                            SocialRequest request, int oldStatus)
117                    throws SystemException {
118    
119                    List<SocialRequest> requests = SocialRequestUtil.findByU_C_C_T_S(
120                            request.getUserId(), request.getClassNameId(), request.getClassPK(),
121                            request.getType(), oldStatus);
122    
123                    int newStatus = request.getStatus();
124    
125                    for (SocialRequest curRequest : requests) {
126                            curRequest.setStatus(newStatus);
127    
128                            SocialRequestUtil.update(curRequest, false);
129                    }
130            }
131    
132            public void processDuplicateRequestsToUser(
133                            SocialRequest request, int oldStatus)
134                    throws SystemException {
135    
136                    List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
137                            request.getClassNameId(), request.getClassPK(), request.getType(),
138                            request.getReceiverUserId(), oldStatus);
139    
140                    int newStatus = request.getStatus();
141    
142                    for (SocialRequest curRequest : requests) {
143                            curRequest.setStatus(newStatus);
144    
145                            SocialRequestUtil.update(curRequest, false);
146                    }
147            }
148    
149            @Override
150            public boolean processRejection(
151                    SocialRequest request, ThemeDisplay themeDisplay) {
152    
153                    try {
154                            return doProcessRejection(request, themeDisplay);
155                    }
156                    catch (Exception e) {
157                            _log.error("Unable to process rejection", e);
158                    }
159    
160                    return false;
161            }
162    
163            protected abstract SocialRequestFeedEntry doInterpret(
164                            SocialRequest request, ThemeDisplay themeDisplay)
165                    throws Exception;
166    
167            protected abstract boolean doProcessConfirmation(
168                            SocialRequest request, ThemeDisplay themeDisplay)
169                    throws Exception;
170    
171            protected boolean doProcessRejection(
172                            SocialRequest request, ThemeDisplay themeDisplay)
173                    throws Exception {
174    
175                    return true;
176            }
177    
178            private static Log _log = LogFactoryUtil.getLog(
179                    BaseSocialRequestInterpreter.class);
180    
181    }