1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.social.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.User;
20  import com.liferay.portal.theme.ThemeDisplay;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.social.NoSuchRequestException;
23  import com.liferay.portlet.social.RequestUserIdException;
24  import com.liferay.portlet.social.model.SocialRequest;
25  import com.liferay.portlet.social.model.SocialRequestConstants;
26  import com.liferay.portlet.social.service.base.SocialRequestLocalServiceBaseImpl;
27  
28  import java.util.List;
29  
30  /**
31   * <a href="SocialRequestLocalServiceImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class SocialRequestLocalServiceImpl
37      extends SocialRequestLocalServiceBaseImpl {
38  
39      public SocialRequest addRequest(
40              long userId, long groupId, String className, long classPK,
41              int type, String extraData, long receiverUserId)
42          throws PortalException, SystemException {
43  
44          User user = userPersistence.findByPrimaryKey(userId);
45          long classNameId = PortalUtil.getClassNameId(className);
46          User receiverUser = userPersistence.findByPrimaryKey(receiverUserId);
47          long now = System.currentTimeMillis();
48  
49          if ((userId == receiverUserId) || (user.isDefaultUser()) ||
50              (receiverUser.isDefaultUser()) ||
51              (user.getCompanyId() != receiverUser.getCompanyId())) {
52  
53              throw new RequestUserIdException();
54          }
55  
56          try {
57              socialRequestPersistence.removeByU_C_C_T_R(
58                  userId, classNameId, classPK, type, receiverUserId);
59          }
60          catch (NoSuchRequestException nsre) {
61          }
62  
63          long requestId = counterLocalService.increment(
64              SocialRequest.class.getName());
65  
66          SocialRequest request = socialRequestPersistence.create(requestId);
67  
68          request.setGroupId(groupId);
69          request.setCompanyId(user.getCompanyId());
70          request.setUserId(user.getUserId());
71          request.setCreateDate(now);
72          request.setModifiedDate(now);
73          request.setClassNameId(classNameId);
74          request.setClassPK(classPK);
75          request.setType(type);
76          request.setExtraData(extraData);
77          request.setReceiverUserId(receiverUserId);
78          request.setStatus(SocialRequestConstants.STATUS_PENDING);
79  
80          socialRequestPersistence.update(request, false);
81  
82          return request;
83      }
84  
85      public void deleteReceiverUserRequests(long receiverUserId)
86          throws SystemException {
87  
88          socialRequestPersistence.removeByReceiverUserId(receiverUserId);
89      }
90  
91      public void deleteRequest(long requestId)
92          throws PortalException, SystemException {
93  
94          socialRequestPersistence.remove(requestId);
95      }
96  
97      public void deleteUserRequests(long userId) throws SystemException {
98          socialRequestPersistence.removeByUserId(userId);
99      }
100 
101     public List<SocialRequest> getReceiverUserRequests(
102             long receiverUserId, int start, int end)
103         throws SystemException {
104 
105         return socialRequestPersistence.findByReceiverUserId(
106             receiverUserId, start, end);
107     }
108 
109     public List<SocialRequest> getReceiverUserRequests(
110             long receiverUserId, int status, int start, int end)
111         throws SystemException {
112 
113         return socialRequestPersistence.findByR_S(
114             receiverUserId, status, start, end);
115     }
116 
117     public int getReceiverUserRequestsCount(long receiverUserId)
118         throws SystemException {
119 
120         return socialRequestPersistence.countByReceiverUserId(receiverUserId);
121     }
122 
123     public int getReceiverUserRequestsCount(long receiverUserId, int status)
124         throws SystemException {
125 
126         return socialRequestPersistence.countByR_S(receiverUserId, status);
127     }
128 
129     public List<SocialRequest> getUserRequests(long userId, int start, int end)
130         throws SystemException {
131 
132         return socialRequestPersistence.findByUserId(userId, start, end);
133     }
134 
135     public List<SocialRequest> getUserRequests(
136             long userId, int status, int start, int end)
137         throws SystemException {
138 
139         return socialRequestPersistence.findByU_S(userId, status, start, end);
140     }
141 
142     public int getUserRequestsCount(long userId) throws SystemException {
143         return socialRequestPersistence.countByUserId(userId);
144     }
145 
146     public int getUserRequestsCount(long userId, int status)
147         throws SystemException {
148 
149         return socialRequestPersistence.countByU_S(userId, status);
150     }
151 
152     public boolean hasRequest(
153             long userId, String className, long classPK, int type, int status)
154         throws SystemException {
155 
156         long classNameId = PortalUtil.getClassNameId(className);
157 
158         if (socialRequestPersistence.countByU_C_C_T_S(
159                 userId, classNameId, classPK, type, status) <= 0) {
160 
161             return false;
162         }
163         else {
164             return true;
165         }
166     }
167 
168     public boolean hasRequest(
169             long userId, String className, long classPK, int type,
170             long receiverUserId, int status)
171         throws SystemException {
172 
173         long classNameId = PortalUtil.getClassNameId(className);
174 
175         SocialRequest socialRequest =
176             socialRequestPersistence.fetchByU_C_C_T_R(
177                 userId, classNameId, classPK, type, receiverUserId);
178 
179         if ((socialRequest == null) || (socialRequest.getStatus() != status)) {
180             return false;
181         }
182         else {
183             return true;
184         }
185     }
186 
187     public SocialRequest updateRequest(
188             long requestId, int status, ThemeDisplay themeDisplay)
189         throws PortalException, SystemException {
190 
191         SocialRequest request = socialRequestPersistence.findByPrimaryKey(
192             requestId);
193 
194         request.setModifiedDate(System.currentTimeMillis());
195         request.setStatus(status);
196 
197         socialRequestPersistence.update(request, false);
198 
199         if (status == SocialRequestConstants.STATUS_CONFIRM) {
200             socialRequestInterpreterLocalService.processConfirmation(
201                 request, themeDisplay);
202         }
203         else if (status == SocialRequestConstants.STATUS_IGNORE) {
204             socialRequestInterpreterLocalService.processRejection(
205                 request, themeDisplay);
206         }
207 
208         return request;
209     }
210 
211 }