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