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