1   /**
2    * Copyright (c) 2000-2009 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.getScopeGroupId()) {
60                  return userName;
61              }
62  
63              String userDisplayURL = user.getDisplayURL(themeDisplay);
64  
65              userName =
66                  "<a href=\"" + userDisplayURL + "\">" + userName + "</a>";
67  
68              return userName;
69          }
70          catch (Exception e) {
71              return StringPool.BLANK;
72          }
73      }
74  
75      public SocialRequestFeedEntry interpret(
76          SocialRequest request, ThemeDisplay themeDisplay) {
77  
78          try {
79              return doInterpret(request, themeDisplay);
80          }
81          catch (Exception e) {
82              _log.error("Unable to interpret request", e);
83          }
84  
85          return null;
86      }
87  
88      public boolean processConfirmation(
89          SocialRequest request, ThemeDisplay themeDisplay) {
90  
91          try {
92              return doProcessConfirmation(request, themeDisplay);
93          }
94          catch (Exception e) {
95              _log.error("Unable to process confirmation", e);
96          }
97  
98          return false;
99      }
100 
101     public boolean processRejection(
102         SocialRequest request, ThemeDisplay themeDisplay) {
103 
104         try {
105             return doProcessRejection(request, themeDisplay);
106         }
107         catch (Exception e) {
108             _log.error("Unable to process rejection", e);
109         }
110 
111         return false;
112     }
113 
114     protected abstract SocialRequestFeedEntry doInterpret(
115             SocialRequest request, ThemeDisplay themeDisplay)
116         throws Exception;
117 
118     protected abstract boolean doProcessConfirmation(
119             SocialRequest request, ThemeDisplay themeDisplay)
120         throws Exception;
121 
122     protected boolean doProcessRejection(
123             SocialRequest request, ThemeDisplay themeDisplay)
124         throws Exception {
125 
126         return true;
127     }
128 
129     private static Log _log =
130         LogFactoryUtil.getLog(BaseSocialRequestInterpreter.class);
131 
132 }