1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.social.model;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.service.UserLocalServiceUtil;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portlet.social.service.persistence.SocialRequestUtil;
26  
27  import java.util.List;
28  
29  /**
30   * <a href="BaseSocialRequestInterpreter.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Amos Fong
35   */
36  public abstract class BaseSocialRequestInterpreter
37      implements SocialRequestInterpreter {
38  
39      public String getUserName(long userId, ThemeDisplay themeDisplay) {
40          try {
41              if (userId <= 0) {
42                  return StringPool.BLANK;
43              }
44  
45              User user = UserLocalServiceUtil.getUserById(userId);
46  
47              if (user.getUserId() == themeDisplay.getUserId()) {
48                  return user.getFirstName();
49              }
50  
51              String userName = user.getFullName();
52  
53              Group group = user.getGroup();
54  
55              if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
56                  return userName;
57              }
58  
59              String userDisplayURL = user.getDisplayURL(themeDisplay);
60  
61              userName =
62                  "<a href=\"" + userDisplayURL + "\">" + userName + "</a>";
63  
64              return userName;
65          }
66          catch (Exception e) {
67              return StringPool.BLANK;
68          }
69      }
70  
71      public SocialRequestFeedEntry interpret(
72          SocialRequest request, ThemeDisplay themeDisplay) {
73  
74          try {
75              return doInterpret(request, themeDisplay);
76          }
77          catch (Exception e) {
78              _log.error("Unable to interpret request", e);
79          }
80  
81          return null;
82      }
83  
84      public boolean processConfirmation(
85          SocialRequest request, ThemeDisplay themeDisplay) {
86  
87          try {
88              return doProcessConfirmation(request, themeDisplay);
89          }
90          catch (Exception e) {
91              _log.error("Unable to process confirmation", e);
92          }
93  
94          return false;
95      }
96  
97      public void processDuplicateRequestsFromUser(
98              SocialRequest request, int oldStatus)
99          throws SystemException {
100 
101         List<SocialRequest> requests = SocialRequestUtil.findByU_C_C_T_S(
102             request.getUserId(), request.getClassNameId(), request.getClassPK(),
103             request.getType(), oldStatus);
104 
105         int newStatus = request.getStatus();
106 
107         for (SocialRequest curRequest : requests) {
108             curRequest.setStatus(newStatus);
109 
110             SocialRequestUtil.update(curRequest, false);
111         }
112     }
113 
114     public void processDuplicateRequestsToUser(
115             SocialRequest request, int oldStatus)
116         throws SystemException {
117 
118         List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
119             request.getClassNameId(), request.getClassPK(), request.getType(),
120             request.getReceiverUserId(), oldStatus);
121 
122         int newStatus = request.getStatus();
123 
124         for (SocialRequest curRequest : requests) {
125             curRequest.setStatus(newStatus);
126 
127             SocialRequestUtil.update(curRequest, false);
128         }
129     }
130 
131     public boolean processRejection(
132         SocialRequest request, ThemeDisplay themeDisplay) {
133 
134         try {
135             return doProcessRejection(request, themeDisplay);
136         }
137         catch (Exception e) {
138             _log.error("Unable to process rejection", e);
139         }
140 
141         return false;
142     }
143 
144     protected abstract SocialRequestFeedEntry doInterpret(
145             SocialRequest request, ThemeDisplay themeDisplay)
146         throws Exception;
147 
148     protected abstract boolean doProcessConfirmation(
149             SocialRequest request, ThemeDisplay themeDisplay)
150         throws Exception;
151 
152     protected boolean doProcessRejection(
153             SocialRequest request, ThemeDisplay themeDisplay)
154         throws Exception {
155 
156         return true;
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(
160         BaseSocialRequestInterpreter.class);
161 
162 }