1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.social.model;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  
30  /**
31   * <a href="BaseSocialRequestInterpreter.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public abstract class BaseSocialRequestInterpreter
38      implements SocialRequestInterpreter {
39  
40      public String getUserName(long userId, ThemeDisplay themeDisplay) {
41          try {
42              if (userId <= 0) {
43                  return StringPool.BLANK;
44              }
45  
46              User user = UserLocalServiceUtil.getUserById(userId);
47  
48              if (user.getUserId() == themeDisplay.getUserId()) {
49                  return user.getFirstName();
50              }
51  
52              String userName = user.getFullName();
53  
54              Group group = user.getGroup();
55  
56              if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
57                  return userName;
58              }
59  
60              String userDisplayURL = user.getDisplayURL(themeDisplay);
61  
62              userName =
63                  "<a href=\"" + userDisplayURL + "\">" + userName + "</a>";
64  
65              return userName;
66          }
67          catch (Exception e) {
68              return StringPool.BLANK;
69          }
70      }
71  
72      public SocialRequestFeedEntry interpret(
73          SocialRequest request, ThemeDisplay themeDisplay) {
74  
75          try {
76              return doInterpret(request, themeDisplay);
77          }
78          catch (Exception e) {
79              _log.error("Unable to interpret request", e);
80          }
81  
82          return null;
83      }
84  
85      public boolean processConfirmation(
86          SocialRequest request, ThemeDisplay themeDisplay) {
87  
88          try {
89              return doProcessConfirmation(request, themeDisplay);
90          }
91          catch (Exception e) {
92              _log.error("Unable to process confirmation", e);
93          }
94  
95          return false;
96      }
97  
98      public boolean processRejection(
99          SocialRequest request, ThemeDisplay themeDisplay) {
100 
101         try {
102             return doProcessRejection(request, themeDisplay);
103         }
104         catch (Exception e) {
105             _log.error("Unable to process rejection", e);
106         }
107 
108         return false;
109     }
110 
111     protected abstract SocialRequestFeedEntry doInterpret(
112             SocialRequest request, ThemeDisplay themeDisplay)
113         throws Exception;
114 
115     protected abstract boolean doProcessConfirmation(
116             SocialRequest request, ThemeDisplay themeDisplay)
117         throws Exception;
118 
119     protected boolean doProcessRejection(
120             SocialRequest request, ThemeDisplay themeDisplay)
121         throws Exception {
122 
123         return true;
124     }
125 
126     private static Log _log =
127         LogFactoryUtil.getLog(BaseSocialRequestInterpreter.class);
128 
129 }