PageRenderTime 104ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/portlets/so-portlet/docroot/WEB-INF/src/com/liferay/so/service/impl/MemberRequestLocalServiceImpl.java

https://github.com/christine-huang/liferay-plugins
Java | 426 lines | 304 code | 99 blank | 23 comment | 25 complexity | 1da97709a129bc6c725b8f38863433e9 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This file is part of Liferay Social Office. Liferay Social Office is free
  5. * software: you can redistribute it and/or modify it under the terms of the GNU
  6. * Affero General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * Liferay Social Office is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * Liferay Social Office. If not, see http://www.gnu.org/licenses/agpl-3.0.html.
  16. */
  17. package com.liferay.so.service.impl;
  18. import com.liferay.mail.service.MailServiceUtil;
  19. import com.liferay.portal.NoSuchUserException;
  20. import com.liferay.portal.kernel.exception.PortalException;
  21. import com.liferay.portal.kernel.exception.SystemException;
  22. import com.liferay.portal.kernel.json.JSONFactoryUtil;
  23. import com.liferay.portal.kernel.json.JSONObject;
  24. import com.liferay.portal.kernel.mail.MailMessage;
  25. import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
  26. import com.liferay.portal.kernel.notifications.NotificationEvent;
  27. import com.liferay.portal.kernel.notifications.NotificationEventFactoryUtil;
  28. import com.liferay.portal.kernel.util.HttpUtil;
  29. import com.liferay.portal.kernel.util.PrefsPropsUtil;
  30. import com.liferay.portal.kernel.util.PropsKeys;
  31. import com.liferay.portal.kernel.util.StringPool;
  32. import com.liferay.portal.kernel.util.StringUtil;
  33. import com.liferay.portal.kernel.util.Validator;
  34. import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
  35. import com.liferay.portal.model.Group;
  36. import com.liferay.portal.model.User;
  37. import com.liferay.portal.service.ServiceContext;
  38. import com.liferay.so.MemberRequestAlreadyUsedException;
  39. import com.liferay.so.MemberRequestInvalidUserException;
  40. import com.liferay.so.invitemembers.util.InviteMembersConstants;
  41. import com.liferay.so.model.MemberRequest;
  42. import com.liferay.so.service.base.MemberRequestLocalServiceBaseImpl;
  43. import com.liferay.so.util.PortletKeys;
  44. import java.util.Date;
  45. import java.util.List;
  46. import javax.mail.internet.InternetAddress;
  47. /**
  48. * @author Ryan Park
  49. * @author Jonathan Lee
  50. */
  51. public class MemberRequestLocalServiceImpl
  52. extends MemberRequestLocalServiceBaseImpl {
  53. public MemberRequest addMemberRequest(
  54. long userId, long groupId, long receiverUserId,
  55. String receiverEmailAddress, long invitedRoleId, long invitedTeamId,
  56. ServiceContext serviceContext)
  57. throws PortalException, SystemException {
  58. // Member request
  59. User user = userLocalService.getUserById(userId);
  60. try {
  61. User receiverUser = userLocalService.getUserByEmailAddress(
  62. serviceContext.getCompanyId(), receiverEmailAddress);
  63. receiverUserId = receiverUser.getUserId();
  64. }
  65. catch (NoSuchUserException nsue) {
  66. }
  67. Date now = new Date();
  68. long memberRequestId = counterLocalService.increment();
  69. MemberRequest memberRequest = memberRequestPersistence.create(
  70. memberRequestId);
  71. memberRequest.setGroupId(groupId);
  72. memberRequest.setCompanyId(user.getCompanyId());
  73. memberRequest.setUserId(userId);
  74. memberRequest.setUserName(user.getFullName());
  75. memberRequest.setCreateDate(now);
  76. memberRequest.setModifiedDate(now);
  77. memberRequest.setKey(PortalUUIDUtil.generate());
  78. memberRequest.setReceiverUserId(receiverUserId);
  79. memberRequest.setInvitedRoleId(invitedRoleId);
  80. memberRequest.setInvitedTeamId(invitedTeamId);
  81. memberRequest.setStatus(InviteMembersConstants.STATUS_PENDING);
  82. memberRequestPersistence.update(memberRequest);
  83. // Email
  84. try {
  85. sendEmail(receiverEmailAddress, memberRequest, serviceContext);
  86. }
  87. catch (Exception e) {
  88. throw new SystemException(e);
  89. }
  90. // Notifications
  91. if (receiverUserId > 0) {
  92. sendNotificationEvent(memberRequest);
  93. }
  94. return memberRequest;
  95. }
  96. public void addMemberRequests(
  97. long userId, long groupId, long[] receiverUserIds,
  98. long invitedRoleId, long invitedTeamId,
  99. ServiceContext serviceContext)
  100. throws PortalException, SystemException {
  101. for (long receiverUserId : receiverUserIds) {
  102. if (hasPendingMemberRequest(groupId, receiverUserId)) {
  103. continue;
  104. }
  105. User user = userLocalService.getUser(receiverUserId);
  106. String emailAddress = user.getEmailAddress();
  107. addMemberRequest(
  108. userId, groupId, receiverUserId, emailAddress, invitedRoleId,
  109. invitedTeamId, serviceContext);
  110. }
  111. }
  112. public void addMemberRequests(
  113. long userId, long groupId, String[] emailAddresses,
  114. long invitedRoleId, long invitedTeamId,
  115. ServiceContext serviceContext)
  116. throws PortalException, SystemException {
  117. for (String emailAddress : emailAddresses) {
  118. if (!Validator.isEmailAddress(emailAddress)) {
  119. continue;
  120. }
  121. addMemberRequest(
  122. userId, groupId, 0, emailAddress, invitedRoleId, invitedTeamId,
  123. serviceContext);
  124. }
  125. }
  126. public MemberRequest getMemberRequest(
  127. long groupId, long receiverUserId, int status)
  128. throws PortalException, SystemException {
  129. return memberRequestPersistence.findByG_R_S(
  130. groupId, receiverUserId, status);
  131. }
  132. public List<MemberRequest> getReceiverMemberRequest(
  133. long receiverUserId, int start, int end)
  134. throws SystemException {
  135. return memberRequestPersistence.findByReceiverUserId(receiverUserId);
  136. }
  137. public int getReceiverMemberRequestCount(long receiverUserId)
  138. throws SystemException {
  139. return memberRequestPersistence.countByReceiverUserId(receiverUserId);
  140. }
  141. public List<MemberRequest> getReceiverStatusMemberRequest(
  142. long receiverUserId, int status, int start, int end)
  143. throws SystemException {
  144. return memberRequestPersistence.findByR_S(
  145. receiverUserId, status, start, end);
  146. }
  147. public int getReceiverStatusMemberRequestCount(
  148. long receiverUserId, int status)
  149. throws SystemException {
  150. return memberRequestPersistence.countByR_S(receiverUserId, status);
  151. }
  152. public boolean hasPendingMemberRequest(long groupId, long receiverUserId)
  153. throws SystemException {
  154. MemberRequest memberRequest = memberRequestPersistence.fetchByG_R_S(
  155. groupId, receiverUserId, InviteMembersConstants.STATUS_PENDING);
  156. if (memberRequest != null) {
  157. return true;
  158. }
  159. else {
  160. return false;
  161. }
  162. }
  163. public MemberRequest updateMemberRequest(
  164. long userId, long memberRequestId, int status)
  165. throws Exception {
  166. MemberRequest memberRequest = memberRequestPersistence.findByPrimaryKey(
  167. memberRequestId);
  168. validate(memberRequest, userId);
  169. memberRequest.setModifiedDate(new Date());
  170. memberRequest.setStatus(status);
  171. memberRequestPersistence.update(memberRequest);
  172. if (status == InviteMembersConstants.STATUS_ACCEPTED) {
  173. userLocalService.addGroupUsers(
  174. memberRequest.getGroupId(),
  175. new long[] {memberRequest.getReceiverUserId()});
  176. if (memberRequest.getInvitedRoleId() > 0) {
  177. userGroupRoleLocalService.addUserGroupRoles(
  178. new long[] {memberRequest.getReceiverUserId()},
  179. memberRequest.getGroupId(),
  180. memberRequest.getInvitedRoleId());
  181. }
  182. if (memberRequest.getInvitedTeamId() > 0) {
  183. userLocalService.addTeamUsers(
  184. memberRequest.getInvitedTeamId(),
  185. new long[] {memberRequest.getReceiverUserId()});
  186. }
  187. }
  188. return memberRequest;
  189. }
  190. public MemberRequest updateMemberRequest(String key, long receiverUserId)
  191. throws PortalException, SystemException {
  192. MemberRequest memberRequest = memberRequestPersistence.findByKey(key);
  193. validate(memberRequest, 0);
  194. memberRequest.setModifiedDate(new Date());
  195. memberRequest.setReceiverUserId(receiverUserId);
  196. memberRequestPersistence.update(memberRequest);
  197. if (receiverUserId > 0) {
  198. sendNotificationEvent(memberRequest);
  199. }
  200. return memberRequest;
  201. }
  202. protected String getCreateAccountURL(
  203. MemberRequest memberRequest, ServiceContext serviceContext) {
  204. String createAccountURL = (String)serviceContext.getAttribute(
  205. "createAccountURL");
  206. if (Validator.isNull(createAccountURL)) {
  207. createAccountURL = serviceContext.getPortalURL();
  208. }
  209. String redirectURL = getRedirectURL(serviceContext);
  210. redirectURL = HttpUtil.addParameter(
  211. redirectURL, "key", memberRequest.getKey());
  212. createAccountURL = HttpUtil.addParameter(
  213. createAccountURL, "redirect", redirectURL);
  214. return createAccountURL;
  215. }
  216. protected String getLoginURL(ServiceContext serviceContext) {
  217. String loginURL = (String)serviceContext.getAttribute("loginURL");
  218. if (Validator.isNull(loginURL)) {
  219. loginURL = serviceContext.getPortalURL();
  220. }
  221. String redirectURL = getRedirectURL(serviceContext);
  222. return HttpUtil.addParameter(loginURL, "redirect", redirectURL);
  223. }
  224. protected String getRedirectURL(ServiceContext serviceContext) {
  225. String redirectURL = (String)serviceContext.getAttribute("redirectURL");
  226. if (Validator.isNull(redirectURL)) {
  227. redirectURL = serviceContext.getCurrentURL();
  228. }
  229. return redirectURL;
  230. }
  231. protected void sendEmail(
  232. String emailAddress, MemberRequest memberRequest,
  233. ServiceContext serviceContext)
  234. throws Exception {
  235. long companyId = memberRequest.getCompanyId();
  236. Group group = groupLocalService.getGroup(memberRequest.getGroupId());
  237. User user = userLocalService.getUser(memberRequest.getUserId());
  238. User receiverUser = null;
  239. if (memberRequest.getReceiverUserId() > 0) {
  240. receiverUser = userLocalService.getUser(
  241. memberRequest.getReceiverUserId());
  242. }
  243. String fromName = PrefsPropsUtil.getString(
  244. companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
  245. String fromAddress = PrefsPropsUtil.getString(
  246. companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
  247. String toName = StringPool.BLANK;
  248. String toAddress = emailAddress;
  249. if (receiverUser != null) {
  250. toName = receiverUser.getFullName();
  251. }
  252. String subject = StringUtil.read(
  253. getClassLoader(),
  254. "com/liferay/so/invitemembers/dependencies/subject.tmpl");
  255. String body = StringPool.BLANK;
  256. if (memberRequest.getReceiverUserId() > 0) {
  257. body = StringUtil.read(
  258. getClassLoader(),
  259. "com/liferay/so/invitemembers/dependencies/" +
  260. "existing_user_body.tmpl");
  261. }
  262. else {
  263. body = StringUtil.read(
  264. getClassLoader(),
  265. "com/liferay/so/invitemembers/dependencies/" +
  266. "new_user_body.tmpl");
  267. }
  268. subject = StringUtil.replace(
  269. subject,
  270. new String[] {
  271. "[$MEMBER_REQUEST_GROUP$]", "[$MEMBER_REQUEST_USER$]"
  272. },
  273. new String[] {
  274. group.getDescriptiveName(serviceContext.getLocale()),
  275. user.getFullName()
  276. });
  277. body = StringUtil.replace(
  278. body,
  279. new String[] {
  280. "[$ADMIN_ADDRESS$]", "[$ADMIN_NAME$]",
  281. "[$MEMBER_REQUEST_CREATE_ACCOUNT_URL$]",
  282. "[$MEMBER_REQUEST_GROUP$]", "[$MEMBER_REQUEST_LOGIN_URL$]",
  283. "[$MEMBER_REQUEST_USER$]"
  284. },
  285. new String[] {
  286. fromAddress, fromName,
  287. getCreateAccountURL(memberRequest, serviceContext),
  288. group.getDescriptiveName(serviceContext.getLocale()),
  289. getLoginURL(serviceContext), user.getFullName()
  290. });
  291. InternetAddress from = new InternetAddress(fromAddress, fromName);
  292. InternetAddress to = new InternetAddress(toAddress, toName);
  293. MailMessage mailMessage = new MailMessage(
  294. from, to, subject, body, true);
  295. MailServiceUtil.sendEmail(mailMessage);
  296. }
  297. protected void sendNotificationEvent(MemberRequest memberRequest)
  298. throws PortalException {
  299. JSONObject notificationEventJSONObject =
  300. JSONFactoryUtil.createJSONObject();
  301. notificationEventJSONObject.put("groupId", memberRequest.getGroupId());
  302. notificationEventJSONObject.put(
  303. "memberRequestId", memberRequest.getMemberRequestId());
  304. notificationEventJSONObject.put(
  305. "portletId", PortletKeys.SO_INVITE_MEMBERS);
  306. notificationEventJSONObject.put("title", "x-invited-you-to-join-x");
  307. notificationEventJSONObject.put("userId", memberRequest.getUserId());
  308. NotificationEvent notificationEvent =
  309. NotificationEventFactoryUtil.createNotificationEvent(
  310. System.currentTimeMillis(), PortletKeys.SO_NOTIFICATION,
  311. notificationEventJSONObject);
  312. notificationEvent.setDeliveryRequired(0);
  313. ChannelHubManagerUtil.sendNotificationEvent(
  314. memberRequest.getCompanyId(), memberRequest.getReceiverUserId(),
  315. notificationEvent);
  316. }
  317. protected void validate(MemberRequest memberRequest, long userId)
  318. throws PortalException {
  319. if (memberRequest.getStatus() !=
  320. InviteMembersConstants.STATUS_PENDING) {
  321. throw new MemberRequestAlreadyUsedException();
  322. }
  323. else if (memberRequest.getReceiverUserId() != userId) {
  324. throw new MemberRequestInvalidUserException();
  325. }
  326. }
  327. }