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