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