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