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