001    /**
002     * Copyright (c) 2000-2010 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     * @author Brian Wing Shun Chan
032     */
033    public class SocialRequestLocalServiceImpl
034            extends SocialRequestLocalServiceBaseImpl {
035    
036            public SocialRequest addRequest(
037                            long userId, long groupId, String className, long classPK,
038                            int type, String extraData, long receiverUserId)
039                    throws PortalException, SystemException {
040    
041                    User user = userPersistence.findByPrimaryKey(userId);
042                    long classNameId = PortalUtil.getClassNameId(className);
043                    User receiverUser = userPersistence.findByPrimaryKey(receiverUserId);
044                    long now = System.currentTimeMillis();
045    
046                    if ((userId == receiverUserId) || (user.isDefaultUser()) ||
047                            (receiverUser.isDefaultUser()) ||
048                            (user.getCompanyId() != receiverUser.getCompanyId())) {
049    
050                            throw new RequestUserIdException();
051                    }
052    
053                    try {
054                            socialRequestPersistence.removeByU_C_C_T_R(
055                                    userId, classNameId, classPK, type, receiverUserId);
056                    }
057                    catch (NoSuchRequestException nsre) {
058                    }
059    
060                    long requestId = counterLocalService.increment(
061                            SocialRequest.class.getName());
062    
063                    SocialRequest request = socialRequestPersistence.create(requestId);
064    
065                    request.setGroupId(groupId);
066                    request.setCompanyId(user.getCompanyId());
067                    request.setUserId(user.getUserId());
068                    request.setCreateDate(now);
069                    request.setModifiedDate(now);
070                    request.setClassNameId(classNameId);
071                    request.setClassPK(classPK);
072                    request.setType(type);
073                    request.setExtraData(extraData);
074                    request.setReceiverUserId(receiverUserId);
075                    request.setStatus(SocialRequestConstants.STATUS_PENDING);
076    
077                    socialRequestPersistence.update(request, false);
078    
079                    return request;
080            }
081    
082            public void deleteReceiverUserRequests(long receiverUserId)
083                    throws SystemException {
084    
085                    socialRequestPersistence.removeByReceiverUserId(receiverUserId);
086            }
087    
088            public void deleteRequest(long requestId)
089                    throws PortalException, SystemException {
090    
091                    socialRequestPersistence.remove(requestId);
092            }
093    
094            public void deleteUserRequests(long userId) throws SystemException {
095                    socialRequestPersistence.removeByUserId(userId);
096            }
097    
098            public List<SocialRequest> getReceiverUserRequests(
099                            long receiverUserId, int start, int end)
100                    throws SystemException {
101    
102                    return socialRequestPersistence.findByReceiverUserId(
103                            receiverUserId, start, end);
104            }
105    
106            public List<SocialRequest> getReceiverUserRequests(
107                            long receiverUserId, int status, int start, int end)
108                    throws SystemException {
109    
110                    return socialRequestPersistence.findByR_S(
111                            receiverUserId, status, start, end);
112            }
113    
114            public int getReceiverUserRequestsCount(long receiverUserId)
115                    throws SystemException {
116    
117                    return socialRequestPersistence.countByReceiverUserId(receiverUserId);
118            }
119    
120            public int getReceiverUserRequestsCount(long receiverUserId, int status)
121                    throws SystemException {
122    
123                    return socialRequestPersistence.countByR_S(receiverUserId, status);
124            }
125    
126            public List<SocialRequest> getUserRequests(long userId, int start, int end)
127                    throws SystemException {
128    
129                    return socialRequestPersistence.findByUserId(userId, start, end);
130            }
131    
132            public List<SocialRequest> getUserRequests(
133                            long userId, int status, int start, int end)
134                    throws SystemException {
135    
136                    return socialRequestPersistence.findByU_S(userId, status, start, end);
137            }
138    
139            public int getUserRequestsCount(long userId) throws SystemException {
140                    return socialRequestPersistence.countByUserId(userId);
141            }
142    
143            public int getUserRequestsCount(long userId, int status)
144                    throws SystemException {
145    
146                    return socialRequestPersistence.countByU_S(userId, status);
147            }
148    
149            public boolean hasRequest(
150                            long userId, String className, long classPK, int type, int status)
151                    throws SystemException {
152    
153                    long classNameId = PortalUtil.getClassNameId(className);
154    
155                    if (socialRequestPersistence.countByU_C_C_T_S(
156                                    userId, classNameId, classPK, type, status) <= 0) {
157    
158                            return false;
159                    }
160                    else {
161                            return true;
162                    }
163            }
164    
165            public boolean hasRequest(
166                            long userId, String className, long classPK, int type,
167                            long receiverUserId, int status)
168                    throws SystemException {
169    
170                    long classNameId = PortalUtil.getClassNameId(className);
171    
172                    SocialRequest socialRequest =
173                            socialRequestPersistence.fetchByU_C_C_T_R(
174                                    userId, classNameId, classPK, type, receiverUserId);
175    
176                    if ((socialRequest == null) || (socialRequest.getStatus() != status)) {
177                            return false;
178                    }
179                    else {
180                            return true;
181                    }
182            }
183    
184            public SocialRequest updateRequest(
185                            long requestId, int status, ThemeDisplay themeDisplay)
186                    throws PortalException, SystemException {
187    
188                    SocialRequest request = socialRequestPersistence.findByPrimaryKey(
189                            requestId);
190    
191                    request.setModifiedDate(System.currentTimeMillis());
192                    request.setStatus(status);
193    
194                    socialRequestPersistence.update(request, false);
195    
196                    if (status == SocialRequestConstants.STATUS_CONFIRM) {
197                            socialRequestInterpreterLocalService.processConfirmation(
198                                    request, themeDisplay);
199                    }
200                    else if (status == SocialRequestConstants.STATUS_IGNORE) {
201                            socialRequestInterpreterLocalService.processRejection(
202                                    request, themeDisplay);
203                    }
204    
205                    return request;
206            }
207    
208    }