1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.social.model;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  
33  /**
34   * <a href="BaseSocialRequestInterpreter.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public abstract class BaseSocialRequestInterpreter
41      implements SocialRequestInterpreter {
42  
43      public String getUserName(long userId, ThemeDisplay themeDisplay) {
44          try {
45              if (userId <= 0) {
46                  return StringPool.BLANK;
47              }
48  
49              User user = UserLocalServiceUtil.getUserById(userId);
50  
51              if (user.getUserId() == themeDisplay.getUserId()) {
52                  return user.getFirstName();
53              }
54  
55              String userName = user.getFullName();
56  
57              Group group = user.getGroup();
58  
59              if (group.getGroupId() == themeDisplay.getPortletGroupId()) {
60                  return userName;
61              }
62  
63              String userDisplayURL = user.getDisplayURL(
64                  themeDisplay.getURLPortal());
65  
66              userName =
67                  "<a href=\"" + userDisplayURL + "\">" + userName + "</a>";
68  
69              return userName;
70          }
71          catch (Exception e) {
72              return StringPool.BLANK;
73          }
74      }
75  
76      public SocialRequestFeedEntry interpret(
77          SocialRequest request, ThemeDisplay themeDisplay) {
78  
79          try {
80              return doInterpret(request, themeDisplay);
81          }
82          catch (Exception e) {
83              _log.error(e);
84          }
85  
86          return null;
87      }
88  
89      public boolean processConfirmation(
90          SocialRequest request, ThemeDisplay themeDisplay) {
91  
92          try {
93              return doProcessConfirmation(request, themeDisplay);
94          }
95          catch (Exception e) {
96              _log.error(e);
97          }
98  
99          return false;
100     }
101 
102     public boolean processRejection(
103         SocialRequest request, ThemeDisplay themeDisplay) {
104 
105         try {
106             return doProcessRejection(request, themeDisplay);
107         }
108         catch (Exception e) {
109             _log.error(e);
110         }
111 
112         return false;
113     }
114 
115     protected abstract SocialRequestFeedEntry doInterpret(
116             SocialRequest request, ThemeDisplay themeDisplay)
117         throws Exception;
118 
119     protected abstract boolean doProcessConfirmation(
120             SocialRequest request, ThemeDisplay themeDisplay)
121         throws Exception;
122 
123     protected boolean doProcessRejection(
124             SocialRequest request, ThemeDisplay themeDisplay)
125         throws Exception {
126 
127         return true;
128     }
129 
130     private static Log _log =
131         LogFactoryUtil.getLog(BaseSocialRequestInterpreter.class);
132 
133 }