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