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