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