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