001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.social.NoSuchRequestException;
023    import com.liferay.portlet.social.RequestUserIdException;
024    import com.liferay.portlet.social.model.SocialRequest;
025    import com.liferay.portlet.social.model.SocialRequestConstants;
026    import com.liferay.portlet.social.service.base.SocialRequestLocalServiceBaseImpl;
027    
028    import java.util.List;
029    
030    /**
031     * The social request local service responsible for handling social requests
032     * (e.g. friend requests).
033     *
034     * @author Brian Wing Shun Chan
035     */
036    public class SocialRequestLocalServiceImpl
037            extends SocialRequestLocalServiceBaseImpl {
038    
039            /**
040             * Adds a social request to the database.
041             *
042             * <p>
043             * In order to add a social request, both the requesting user and the
044             * receiving user must be from the same company and neither of them can be
045             * the default user.
046             * </p>
047             *
048             * @param  userId the primary key of the requesting user
049             * @param  groupId the primary key of the group
050             * @param  className the class name of the asset that is the subject of the
051             *         request
052             * @param  classPK the primary key of the asset that is the subject of the
053             *         request
054             * @param  type the request's type
055             * @param  extraData the extra data regarding the request
056             * @param  receiverUserId the primary key of the user receiving the request
057             * @return the social request
058             * @throws PortalException if the users could not be found, if the users
059             *         were not from the same company, or if either of the users was the
060             *         default user
061             * @throws SystemException if a system exception occurred
062             */
063            @Override
064            public SocialRequest addRequest(
065                            long userId, long groupId, String className, long classPK, int type,
066                            String extraData, long receiverUserId)
067                    throws PortalException, SystemException {
068    
069                    User user = userPersistence.findByPrimaryKey(userId);
070                    long classNameId = PortalUtil.getClassNameId(className);
071                    User receiverUser = userPersistence.findByPrimaryKey(receiverUserId);
072                    long now = System.currentTimeMillis();
073    
074                    if ((userId == receiverUserId) || user.isDefaultUser() ||
075                            receiverUser.isDefaultUser() ||
076                            (user.getCompanyId() != receiverUser.getCompanyId())) {
077    
078                            throw new RequestUserIdException();
079                    }
080    
081                    try {
082                            socialRequestPersistence.removeByU_C_C_T_R(
083                                    userId, classNameId, classPK, type, receiverUserId);
084                    }
085                    catch (NoSuchRequestException nsre) {
086                    }
087    
088                    long requestId = counterLocalService.increment(
089                            SocialRequest.class.getName());
090    
091                    SocialRequest request = socialRequestPersistence.create(requestId);
092    
093                    request.setGroupId(groupId);
094                    request.setCompanyId(user.getCompanyId());
095                    request.setUserId(user.getUserId());
096                    request.setCreateDate(now);
097                    request.setModifiedDate(now);
098                    request.setClassNameId(classNameId);
099                    request.setClassPK(classPK);
100                    request.setType(type);
101                    request.setExtraData(extraData);
102                    request.setReceiverUserId(receiverUserId);
103                    request.setStatus(SocialRequestConstants.STATUS_PENDING);
104    
105                    socialRequestPersistence.update(request, false);
106    
107                    return request;
108            }
109    
110            /**
111             * Removes all the social requests for the receiving user.
112             *
113             * @param  receiverUserId the primary key of the receiving user
114             * @throws SystemException if a system exception occurred
115             */
116            @Override
117            public void deleteReceiverUserRequests(long receiverUserId)
118                    throws SystemException {
119    
120                    List<SocialRequest> requests =
121                            socialRequestPersistence.findByReceiverUserId(receiverUserId);
122    
123                    for (SocialRequest request : requests) {
124                            deleteRequest(request);
125                    }
126            }
127    
128            /**
129             * Removes the social request identified by its primary key from the
130             * database.
131             *
132             * @param  requestId the primary key of the social request
133             * @throws PortalException if the social request could not be found
134             * @throws SystemException if a system exception occurred
135             */
136            @Override
137            public void deleteRequest(long requestId)
138                    throws PortalException, SystemException {
139    
140                    SocialRequest request = socialRequestPersistence.findByPrimaryKey(
141                            requestId);
142    
143                    deleteRequest(request);
144            }
145    
146            /**
147             * Removes the social request from the database.
148             *
149             * @param  request the social request to be removed
150             * @throws SystemException if a system exception occurred
151             */
152            @Override
153            public void deleteRequest(SocialRequest request) throws SystemException {
154                    socialRequestPersistence.remove(request);
155            }
156    
157            /**
158             * Removes all the social requests for the requesting user.
159             *
160             * @param  userId the primary key of the requesting user
161             * @throws SystemException if a system exception occurred
162             */
163            @Override
164            public void deleteUserRequests(long userId) throws SystemException {
165                    List<SocialRequest> requests = socialRequestPersistence.findByUserId(
166                            userId);
167    
168                    for (SocialRequest request : requests) {
169                            deleteRequest(request);
170                    }
171            }
172    
173            /**
174             * Returns a range of all the social requests for the receiving user.
175             *
176             * <p>
177             * Useful when paginating results. Returns a maximum of <code>end -
178             * start</code> instances. <code>start</code> and <code>end</code> are not
179             * primary keys, they are indexes in the result set. Thus, <code>0</code>
180             * refers to the first result in the set. Setting both <code>start</code>
181             * and <code>end</code> to {@link
182             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
183             * result set.
184             * </p>
185             *
186             * @param  receiverUserId the primary key of the receiving user
187             * @param  start the lower bound of the range of results
188             * @param  end the upper bound of the range of results (not inclusive)
189             * @return the range of matching social requests
190             * @throws SystemException if a system exception occurred
191             */
192            @Override
193            public List<SocialRequest> getReceiverUserRequests(
194                            long receiverUserId, int start, int end)
195                    throws SystemException {
196    
197                    return socialRequestPersistence.findByReceiverUserId(
198                            receiverUserId, start, end);
199            }
200    
201            /**
202             * Returns a range of all the social requests with the given status for the
203             * receiving user.
204             *
205             * <p>
206             * Useful when paginating results. Returns a maximum of <code>end -
207             * start</code> instances. <code>start</code> and <code>end</code> are not
208             * primary keys, they are indexes in the result set. Thus, <code>0</code>
209             * refers to the first result in the set. Setting both <code>start</code>
210             * and <code>end</code> to {@link
211             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
212             * result set.
213             * </p>
214             *
215             * @param  receiverUserId the primary key of the receiving user
216             * @param  status the social request's status
217             * @param  start the lower bound of the range of results
218             * @param  end the upper bound of the range of results (not inclusive)
219             * @return the range of matching social requests
220             * @throws SystemException if a system exception occurred
221             */
222            @Override
223            public List<SocialRequest> getReceiverUserRequests(
224                            long receiverUserId, int status, int start, int end)
225                    throws SystemException {
226    
227                    return socialRequestPersistence.findByR_S(
228                            receiverUserId, status, start, end);
229            }
230    
231            /**
232             * Returns the number of social requests for the receiving user.
233             *
234             * @param  receiverUserId the primary key of the receiving user
235             * @return the number of matching social requests
236             * @throws SystemException if a system exception occurred
237             */
238            @Override
239            public int getReceiverUserRequestsCount(long receiverUserId)
240                    throws SystemException {
241    
242                    return socialRequestPersistence.countByReceiverUserId(receiverUserId);
243            }
244    
245            /**
246             * Returns the number of social requests with the given status for the
247             * receiving user.
248             *
249             * @param  receiverUserId the primary key of the receiving user
250             * @param  status the social request's status
251             * @return the number of matching social requests
252             * @throws SystemException if a system exception occurred
253             */
254            @Override
255            public int getReceiverUserRequestsCount(long receiverUserId, int status)
256                    throws SystemException {
257    
258                    return socialRequestPersistence.countByR_S(receiverUserId, status);
259            }
260    
261            /**
262             * Returns a range of all the social requests for the requesting user.
263             *
264             * <p>
265             * Useful when paginating results. Returns a maximum of <code>end -
266             * start</code> instances. <code>start</code> and <code>end</code> are not
267             * primary keys, they are indexes in the result set. Thus, <code>0</code>
268             * refers to the first result in the set. Setting both <code>start</code>
269             * and <code>end</code> to {@link
270             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
271             * result set.
272             * </p>
273             *
274             * @param  userId the primary key of the requesting user
275             * @param  start the lower bound of the range of results
276             * @param  end the upper bound of the range of results (not inclusive)
277             * @return the range of matching social requests
278             * @throws SystemException if a system exception occurred
279             */
280            @Override
281            public List<SocialRequest> getUserRequests(long userId, int start, int end)
282                    throws SystemException {
283    
284                    return socialRequestPersistence.findByUserId(userId, start, end);
285            }
286    
287            /**
288             * Returns a range of all the social requests with the given status for the
289             * requesting user.
290             *
291             * <p>
292             * Useful when paginating results. Returns a maximum of <code>end -
293             * start</code> instances. <code>start</code> and <code>end</code> are not
294             * primary keys, they are indexes in the result set. Thus, <code>0</code>
295             * refers to the first result in the set. Setting both <code>start</code>
296             * and <code>end</code> to {@link
297             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
298             * result set.
299             * </p>
300             *
301             * @param  userId the primary key of the requesting user
302             * @param  status the social request's status
303             * @param  start the lower bound of the range of results
304             * @param  end the upper bound of the range of results (not inclusive)
305             * @return the range of matching social requests
306             * @throws SystemException if a system exception occurred
307             */
308            @Override
309            public List<SocialRequest> getUserRequests(
310                            long userId, int status, int start, int end)
311                    throws SystemException {
312    
313                    return socialRequestPersistence.findByU_S(userId, status, start, end);
314            }
315    
316            /**
317             * Returns the number of social requests for the requesting user.
318             *
319             * @param  userId the primary key of the requesting user
320             * @return the number of matching social requests
321             * @throws SystemException if a system exception occurred
322             */
323            @Override
324            public int getUserRequestsCount(long userId) throws SystemException {
325                    return socialRequestPersistence.countByUserId(userId);
326            }
327    
328            /**
329             * Returns the number of social requests with the given status for the
330             * requesting user.
331             *
332             * @param  userId the primary key of the requesting user
333             * @param  status the social request's status
334             * @return the number of matching social request
335             * @throws SystemException if a system exception occurred
336             */
337            @Override
338            public int getUserRequestsCount(long userId, int status)
339                    throws SystemException {
340    
341                    return socialRequestPersistence.countByU_S(userId, status);
342            }
343    
344            /**
345             * Returns <code>true</code> if a matching social requests exists in the
346             * database.
347             *
348             * @param  userId the primary key of the requesting user
349             * @param  className the class name of the asset that is the subject of the
350             *         request
351             * @param  classPK the primary key of the asset that is the subject of the
352             *         request
353             * @param  type the request's type
354             * @param  status the social request's status
355             * @return <code>true</code> if the request exists; <code>false</code>
356             *         otherwise
357             * @throws SystemException if a system exception occurred
358             */
359            @Override
360            public boolean hasRequest(
361                            long userId, String className, long classPK, int type, int status)
362                    throws SystemException {
363    
364                    long classNameId = PortalUtil.getClassNameId(className);
365    
366                    if (socialRequestPersistence.countByU_C_C_T_S(
367                                    userId, classNameId, classPK, type, status) <= 0) {
368    
369                            return false;
370                    }
371                    else {
372                            return true;
373                    }
374            }
375    
376            /**
377             * Returns <code>true</code> if a matching social request exists in the
378             * database.
379             *
380             * @param  userId the primary key of the requesting user
381             * @param  className the class name of the asset that is the subject of the
382             *         request
383             * @param  classPK the primary key of the asset that is the subject of the
384             *         request
385             * @param  type the request's type
386             * @param  receiverUserId the primary key of the receiving user
387             * @param  status the social request's status
388             * @return <code>true</code> if the social request exists;
389             *         <code>false</code> otherwise
390             * @throws SystemException if a system exception occurred
391             */
392            @Override
393            public boolean hasRequest(
394                            long userId, String className, long classPK, int type,
395                            long receiverUserId, int status)
396                    throws SystemException {
397    
398                    long classNameId = PortalUtil.getClassNameId(className);
399    
400                    SocialRequest socialRequest = socialRequestPersistence.fetchByU_C_C_T_R(
401                            userId, classNameId, classPK, type, receiverUserId);
402    
403                    if ((socialRequest == null) || (socialRequest.getStatus() != status)) {
404                            return false;
405                    }
406                    else {
407                            return true;
408                    }
409            }
410    
411            /**
412             * Updates the social request replacing its status.
413             *
414             * <p>
415             * If the status is updated to {@link
416             * com.liferay.portlet.social.model.SocialRequestConstants#STATUS_CONFIRM}
417             * then {@link
418             * com.liferay.portlet.social.service.SocialRequestInterpreterLocalService#processConfirmation(
419             * SocialRequest, ThemeDisplay)} is called. If the status is updated to
420             * {@link
421             * com.liferay.portlet.social.model.SocialRequestConstants#STATUS_IGNORE}
422             * then {@link
423             * com.liferay.portlet.social.service.SocialRequestInterpreterLocalService#processRejection(
424             * SocialRequest, ThemeDisplay)} is called.
425             * </p>
426             *
427             * @param  requestId the primary key of the social request
428             * @param  status the new status
429             * @param  themeDisplay the theme display
430             * @return the updated social request
431             * @throws PortalException if the social request could not be found
432             * @throws SystemException if a system exception occurred
433             */
434            @Override
435            public SocialRequest updateRequest(
436                            long requestId, int status, ThemeDisplay themeDisplay)
437                    throws PortalException, SystemException {
438    
439                    SocialRequest request = socialRequestPersistence.findByPrimaryKey(
440                            requestId);
441    
442                    request.setModifiedDate(System.currentTimeMillis());
443                    request.setStatus(status);
444    
445                    socialRequestPersistence.update(request, false);
446    
447                    if (status == SocialRequestConstants.STATUS_CONFIRM) {
448                            socialRequestInterpreterLocalService.processConfirmation(
449                                    request, themeDisplay);
450                    }
451                    else if (status == SocialRequestConstants.STATUS_IGNORE) {
452                            socialRequestInterpreterLocalService.processRejection(
453                                    request, themeDisplay);
454                    }
455    
456                    return request;
457            }
458    
459    }