1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class BaseSocialRequestInterpreter
45      implements SocialRequestInterpreter {
46  
47      public String getUserName(long userId, ThemeDisplay themeDisplay) {
48          try {
49              if (userId <= 0) {
50                  return StringPool.BLANK;
51              }
52  
53              User user = UserLocalServiceUtil.getUserById(userId);
54  
55              if (user.getUserId() == themeDisplay.getUserId()) {
56                  return user.getFirstName();
57              }
58  
59              String userName = user.getFullName();
60  
61              Group group = user.getGroup();
62  
63              if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
64                  return userName;
65              }
66  
67              String userDisplayURL = user.getDisplayURL(themeDisplay);
68  
69              userName =
70                  "<a href=\"" + userDisplayURL + "\">" + userName + "</a>";
71  
72              return userName;
73          }
74          catch (Exception e) {
75              return StringPool.BLANK;
76          }
77      }
78  
79      public SocialRequestFeedEntry interpret(
80          SocialRequest request, ThemeDisplay themeDisplay) {
81  
82          try {
83              return doInterpret(request, themeDisplay);
84          }
85          catch (Exception e) {
86              _log.error("Unable to interpret request", e);
87          }
88  
89          return null;
90      }
91  
92      public boolean processConfirmation(
93          SocialRequest request, ThemeDisplay themeDisplay) {
94  
95          try {
96              return doProcessConfirmation(request, themeDisplay);
97          }
98          catch (Exception e) {
99              _log.error("Unable to process confirmation", e);
100         }
101 
102         return false;
103     }
104 
105     public void processDuplicateRequestsFromUser(
106             SocialRequest request, int oldStatus)
107         throws SystemException {
108 
109         List<SocialRequest> requests = SocialRequestUtil.findByU_C_C_T_S(
110             request.getUserId(), request.getClassNameId(), request.getClassPK(),
111             request.getType(), oldStatus);
112 
113         int newStatus = request.getStatus();
114 
115         for (SocialRequest curRequest : requests) {
116             curRequest.setStatus(newStatus);
117 
118             SocialRequestUtil.update(curRequest, false);
119         }
120     }
121 
122     public void processDuplicateRequestsToUser(
123             SocialRequest request, int oldStatus)
124         throws SystemException {
125 
126         List<SocialRequest> requests = SocialRequestUtil.findByC_C_T_R_S(
127             request.getClassNameId(), request.getClassPK(), request.getType(),
128             request.getReceiverUserId(), oldStatus);
129 
130         int newStatus = request.getStatus();
131 
132         for (SocialRequest curRequest : requests) {
133             curRequest.setStatus(newStatus);
134 
135             SocialRequestUtil.update(curRequest, false);
136         }
137     }
138 
139     public boolean processRejection(
140         SocialRequest request, ThemeDisplay themeDisplay) {
141 
142         try {
143             return doProcessRejection(request, themeDisplay);
144         }
145         catch (Exception e) {
146             _log.error("Unable to process rejection", e);
147         }
148 
149         return false;
150     }
151 
152     protected abstract SocialRequestFeedEntry doInterpret(
153             SocialRequest request, ThemeDisplay themeDisplay)
154         throws Exception;
155 
156     protected abstract boolean doProcessConfirmation(
157             SocialRequest request, ThemeDisplay themeDisplay)
158         throws Exception;
159 
160     protected boolean doProcessRejection(
161             SocialRequest request, ThemeDisplay themeDisplay)
162         throws Exception {
163 
164         return true;
165     }
166 
167     private static Log _log =
168         LogFactoryUtil.getLog(BaseSocialRequestInterpreter.class);
169 
170 }