1   /**
2    * Copyright (c) 2000-2008 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.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.social.NoSuchRequestException;
31  import com.liferay.portlet.social.RequestUserIdException;
32  import com.liferay.portlet.social.model.SocialRequest;
33  import com.liferay.portlet.social.model.SocialRequestConstants;
34  import com.liferay.portlet.social.service.base.SocialRequestLocalServiceBaseImpl;
35  
36  import java.util.Date;
37  import java.util.List;
38  
39  /**
40   * <a href="SocialRequestLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SocialRequestLocalServiceImpl
47      extends SocialRequestLocalServiceBaseImpl {
48  
49      public SocialRequest addRequest(
50              long userId, long groupId, String className, long classPK,
51              int type, String extraData, long receiverUserId)
52          throws PortalException, SystemException {
53  
54          User user = userPersistence.findByPrimaryKey(userId);
55          long classNameId = PortalUtil.getClassNameId(className);
56          User receiverUser = userPersistence.findByPrimaryKey(receiverUserId);
57          Date now = new Date();
58  
59          if ((userId == receiverUserId) || (user.isDefaultUser()) ||
60              (receiverUser.isDefaultUser()) ||
61              (user.getCompanyId() != receiverUser.getCompanyId())) {
62  
63              throw new RequestUserIdException();
64          }
65  
66          try {
67              socialRequestPersistence.removeByU_C_C_T_R(
68                  userId, classNameId, classPK, type, receiverUserId);
69          }
70          catch (NoSuchRequestException nsre) {
71          }
72  
73          long requestId = counterLocalService.increment(
74              SocialRequest.class.getName());
75  
76          SocialRequest request = socialRequestPersistence.create(requestId);
77  
78          request.setGroupId(groupId);
79          request.setCompanyId(user.getCompanyId());
80          request.setUserId(user.getUserId());
81          request.setCreateDate(now);
82          request.setModifiedDate(now);
83          request.setClassNameId(classNameId);
84          request.setClassPK(classPK);
85          request.setType(type);
86          request.setExtraData(extraData);
87          request.setReceiverUserId(receiverUserId);
88          request.setStatus(SocialRequestConstants.STATUS_PENDING);
89  
90          socialRequestPersistence.update(request, false);
91  
92          return request;
93      }
94  
95      public void deleteReceiverUserRequests(long receiverUserId)
96          throws SystemException {
97  
98          socialRequestPersistence.removeByReceiverUserId(receiverUserId);
99      }
100 
101     public void deleteRequest(long requestId)
102         throws PortalException, SystemException {
103 
104         socialRequestPersistence.remove(requestId);
105     }
106 
107     public void deleteUserRequests(long userId) throws SystemException {
108         socialRequestPersistence.removeByUserId(userId);
109     }
110 
111     public List<SocialRequest> getReceiverUserRequests(
112             long receiverUserId, int start, int end)
113         throws SystemException {
114 
115         return socialRequestPersistence.findByReceiverUserId(
116             receiverUserId, start, end);
117     }
118 
119     public List<SocialRequest> getReceiverUserRequests(
120             long receiverUserId, int status, int start, int end)
121         throws SystemException {
122 
123         return socialRequestPersistence.findByR_S(
124             receiverUserId, status, start, end);
125     }
126 
127     public int getReceiverUserRequestsCount(long receiverUserId)
128         throws SystemException {
129 
130         return socialRequestPersistence.countByReceiverUserId(receiverUserId);
131     }
132 
133     public int getReceiverUserRequestsCount(long receiverUserId, int status)
134         throws SystemException {
135 
136         return socialRequestPersistence.countByR_S(receiverUserId, status);
137     }
138 
139     public List<SocialRequest> getUserRequests(long userId, int start, int end)
140         throws SystemException {
141 
142         return socialRequestPersistence.findByUserId(userId, start, end);
143     }
144 
145     public List<SocialRequest> getUserRequests(
146             long userId, int status, int start, int end)
147         throws SystemException {
148 
149         return socialRequestPersistence.findByU_S(userId, status, start, end);
150     }
151 
152     public int getUserRequestsCount(long userId) throws SystemException {
153         return socialRequestPersistence.countByUserId(userId);
154     }
155 
156     public int getUserRequestsCount(long userId, int status)
157         throws SystemException {
158 
159         return socialRequestPersistence.countByU_S(userId, status);
160     }
161 
162     public boolean hasRequest(
163             long userId, String className, long classPK, int type, int status)
164         throws SystemException {
165 
166         long classNameId = PortalUtil.getClassNameId(className);
167 
168         if (socialRequestPersistence.countByU_C_C_T_S(
169                 userId, classNameId, classPK, type, status) <= 0) {
170 
171             return false;
172         }
173         else {
174             return true;
175         }
176     }
177 
178     public boolean hasRequest(
179             long userId, String className, long classPK, int type,
180             long receiverUserId, int status)
181         throws SystemException {
182 
183         long classNameId = PortalUtil.getClassNameId(className);
184 
185         SocialRequest socialRequest =
186             socialRequestPersistence.fetchByU_C_C_T_R_S(
187                 userId, classNameId, classPK, type, receiverUserId, status);
188 
189         if (socialRequest == null) {
190             return false;
191         }
192         else {
193             return true;
194         }
195     }
196 
197     public SocialRequest updateRequest(
198             long requestId, int status, ThemeDisplay themeDisplay)
199         throws PortalException, SystemException {
200 
201         SocialRequest request = socialRequestPersistence.findByPrimaryKey(
202             requestId);
203 
204         int oldStatus = request.getStatus();
205 
206         request.setModifiedDate(new Date());
207         request.setStatus(status);
208 
209         socialRequestPersistence.update(request, false);
210 
211         if (status == SocialRequestConstants.STATUS_CONFIRM) {
212             socialRequestInterpreterLocalService.processConfirmation(
213                 request, themeDisplay);
214         }
215         else if (status == SocialRequestConstants.STATUS_IGNORE) {
216             socialRequestInterpreterLocalService.processRejection(
217                 request, themeDisplay);
218         }
219 
220         List<SocialRequest> requests = socialRequestPersistence.findByU_C_C_T_S(
221             request.getUserId(), request.getClassNameId(), request.getClassPK(),
222             request.getType(), oldStatus);
223 
224         for (SocialRequest curRequest : requests) {
225             curRequest.setStatus(status);
226 
227             socialRequestPersistence.update(curRequest, false);
228         }
229 
230         return request;
231     }
232 
233 }