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