PageRenderTime 100ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/l15k4/liferay-plugins
Java | 362 lines | 260 code | 82 blank | 20 comment | 20 complexity | 758d0e814f1f0767998d4434da4af76e MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 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.kernel.exception.PortalException;
  20. import com.liferay.portal.kernel.exception.SystemException;
  21. import com.liferay.portal.kernel.mail.MailMessage;
  22. import com.liferay.portal.kernel.util.PrefsPropsUtil;
  23. import com.liferay.portal.kernel.util.PropsKeys;
  24. import com.liferay.portal.kernel.util.StringPool;
  25. import com.liferay.portal.kernel.util.StringUtil;
  26. import com.liferay.portal.kernel.util.Validator;
  27. import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
  28. import com.liferay.portal.model.Group;
  29. import com.liferay.portal.model.GroupConstants;
  30. import com.liferay.portal.model.Layout;
  31. import com.liferay.portal.model.User;
  32. import com.liferay.portal.theme.ThemeDisplay;
  33. import com.liferay.portal.util.PortalUtil;
  34. import com.liferay.portal.util.PortletKeys;
  35. import com.liferay.so.MemberRequestAlreadyUsedException;
  36. import com.liferay.so.MemberRequestInvalidUserException;
  37. import com.liferay.so.invitemembers.util.InviteMembersConstants;
  38. import com.liferay.so.model.MemberRequest;
  39. import com.liferay.so.service.base.MemberRequestLocalServiceBaseImpl;
  40. import java.util.Date;
  41. import java.util.List;
  42. import javax.mail.internet.InternetAddress;
  43. /**
  44. * @author Ryan Park
  45. */
  46. public class MemberRequestLocalServiceImpl
  47. extends MemberRequestLocalServiceBaseImpl {
  48. public MemberRequest addMemberRequest(
  49. long userId, long groupId, long receiverUserId,
  50. String receiverEmailAddress, long invitedRoleId, long invitedTeamId,
  51. ThemeDisplay themeDisplay)
  52. throws PortalException, SystemException {
  53. User user = userLocalService.getUserById(userId);
  54. Date now = new Date();
  55. long memberRequestId = counterLocalService.increment();
  56. MemberRequest memberRequest = memberRequestPersistence.create(
  57. memberRequestId);
  58. memberRequest.setGroupId(groupId);
  59. memberRequest.setCompanyId(user.getCompanyId());
  60. memberRequest.setUserId(userId);
  61. memberRequest.setUserName(user.getFullName());
  62. memberRequest.setCreateDate(now);
  63. memberRequest.setModifiedDate(now);
  64. memberRequest.setKey(PortalUUIDUtil.generate());
  65. memberRequest.setReceiverUserId(receiverUserId);
  66. memberRequest.setInvitedRoleId(invitedRoleId);
  67. memberRequest.setInvitedTeamId(invitedTeamId);
  68. memberRequest.setStatus(InviteMembersConstants.STATUS_PENDING);
  69. memberRequestPersistence.update(memberRequest, false);
  70. // Email
  71. try {
  72. sendEmail(receiverEmailAddress, memberRequest, themeDisplay);
  73. }
  74. catch (Exception e) {
  75. throw new SystemException(e);
  76. }
  77. return memberRequest;
  78. }
  79. public void addMemberRequests(
  80. long userId, long groupId, long[] receiverUserIds,
  81. long invitedRoleId, long invitedTeamId, ThemeDisplay themeDisplay)
  82. throws PortalException, SystemException {
  83. for (long receiverUserId : receiverUserIds) {
  84. if (hasPendingMemberRequest(groupId, receiverUserId)) {
  85. continue;
  86. }
  87. User user = userLocalService.getUser(receiverUserId);
  88. String emailAddress = user.getEmailAddress();
  89. addMemberRequest(
  90. userId, groupId, receiverUserId, emailAddress, invitedRoleId,
  91. invitedTeamId, themeDisplay);
  92. }
  93. }
  94. public void addMemberRequests(
  95. long userId, long groupId, String[] emailAddresses,
  96. long invitedRoleId, long invitedTeamId, ThemeDisplay themeDisplay)
  97. throws PortalException, SystemException {
  98. for (String emailAddress : emailAddresses) {
  99. if (!Validator.isEmailAddress(emailAddress)) {
  100. continue;
  101. }
  102. addMemberRequest(
  103. userId, groupId, 0, emailAddress, invitedRoleId, invitedTeamId,
  104. themeDisplay);
  105. }
  106. }
  107. public MemberRequest getMemberRequest(
  108. long groupId, long receiverUserId, int status)
  109. throws PortalException, SystemException {
  110. return memberRequestPersistence.findByG_R_S(
  111. groupId, receiverUserId, status);
  112. }
  113. public List<MemberRequest> getReceiverMemberRequest(
  114. long receiverUserId, int start, int end)
  115. throws SystemException {
  116. return memberRequestPersistence.findByReceiverUserId(receiverUserId);
  117. }
  118. public int getReceiverMemberRequestCount(long receiverUserId)
  119. throws SystemException {
  120. return memberRequestPersistence.countByReceiverUserId(receiverUserId);
  121. }
  122. public List<MemberRequest> getReceiverStatusMemberRequest(
  123. long receiverUserId, int status, int start, int end)
  124. throws SystemException {
  125. return memberRequestPersistence.findByR_S(
  126. receiverUserId, status, start, end);
  127. }
  128. public int getReceiverStatusMemberRequestCount(
  129. long receiverUserId, int status)
  130. throws SystemException {
  131. return memberRequestPersistence.countByR_S(receiverUserId, status);
  132. }
  133. public boolean hasPendingMemberRequest(long groupId, long receiverUserId)
  134. throws SystemException {
  135. MemberRequest memberRequest = memberRequestPersistence.fetchByG_R_S(
  136. groupId, receiverUserId, InviteMembersConstants.STATUS_PENDING);
  137. if (memberRequest != null) {
  138. return true;
  139. }
  140. else {
  141. return false;
  142. }
  143. }
  144. public MemberRequest updateMemberRequest(
  145. long userId, long memberRequestId, int status)
  146. throws Exception {
  147. MemberRequest memberRequest = memberRequestPersistence.findByPrimaryKey(
  148. memberRequestId);
  149. validate(memberRequest, userId);
  150. memberRequest.setModifiedDate(new Date());
  151. memberRequest.setStatus(status);
  152. memberRequestPersistence.update(memberRequest, false);
  153. if (status == InviteMembersConstants.STATUS_ACCEPTED) {
  154. userLocalService.addGroupUsers(
  155. memberRequest.getGroupId(),
  156. new long[] {memberRequest.getReceiverUserId()});
  157. if (memberRequest.getInvitedRoleId() > 0) {
  158. userGroupRoleLocalService.addUserGroupRoles(
  159. new long[] {memberRequest.getReceiverUserId()},
  160. memberRequest.getGroupId(),
  161. memberRequest.getInvitedRoleId());
  162. }
  163. if (memberRequest.getInvitedTeamId() > 0) {
  164. userLocalService.addTeamUsers(
  165. memberRequest.getInvitedTeamId(),
  166. new long[] {memberRequest.getReceiverUserId()});
  167. }
  168. }
  169. return memberRequest;
  170. }
  171. public MemberRequest updateMemberRequest(String key, long receiverUserId)
  172. throws PortalException, SystemException {
  173. MemberRequest memberRequest = memberRequestPersistence.findByKey(key);
  174. validate(memberRequest, 0);
  175. memberRequest.setModifiedDate(new Date());
  176. memberRequest.setReceiverUserId(receiverUserId);
  177. memberRequestPersistence.update(memberRequest, false);
  178. return memberRequest;
  179. }
  180. protected String getCreateAccountURL(String key, ThemeDisplay themeDisplay)
  181. throws Exception {
  182. return getLoginURL(key, themeDisplay) +
  183. "&p_p_id=58&_58_struts_action=%2Flogin%2Fcreate_account";
  184. }
  185. protected String getLoginURL(String key, ThemeDisplay themeDisplay)
  186. throws Exception {
  187. Group group = groupLocalService.getGroup(
  188. themeDisplay.getCompanyId(), GroupConstants.GUEST);
  189. long plid = PortalUtil.getPlidFromPortletId(
  190. group.getGroupId(), PortletKeys.LOGIN);
  191. Layout layout = layoutLocalService.getLayout(plid);
  192. return PortalUtil.getLayoutFullURL(layout, themeDisplay, false) +
  193. "?key=" + key;
  194. }
  195. protected void sendEmail(
  196. String emailAddress, MemberRequest memberRequest,
  197. ThemeDisplay themeDisplay)
  198. throws Exception {
  199. long companyId = memberRequest.getCompanyId();
  200. Group group = groupLocalService.getGroup(memberRequest.getGroupId());
  201. User user = userLocalService.getUser(memberRequest.getUserId());
  202. User receiverUser = null;
  203. if (memberRequest.getReceiverUserId() > 0) {
  204. receiverUser = userLocalService.getUser(
  205. memberRequest.getReceiverUserId());
  206. }
  207. String fromName = PrefsPropsUtil.getString(
  208. companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
  209. String fromAddress = PrefsPropsUtil.getString(
  210. companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
  211. String toName = StringPool.BLANK;
  212. String toAddress = emailAddress;
  213. if (receiverUser != null) {
  214. toName = receiverUser.getFullName();
  215. }
  216. ClassLoader classLoader = getClass().getClassLoader();
  217. String subject = StringUtil.read(
  218. classLoader,
  219. "com/liferay/so/invitemembers/dependencies/subject.tmpl");
  220. String body = StringPool.BLANK;
  221. if (memberRequest.getReceiverUserId() > 0) {
  222. body = StringUtil.read(
  223. classLoader,
  224. "com/liferay/so/invitemembers/dependencies/" +
  225. "existing_user_body.tmpl");
  226. }
  227. else {
  228. body = StringUtil.read(
  229. classLoader,
  230. "com/liferay/so/invitemembers/dependencies/" +
  231. "new_user_body.tmpl");
  232. }
  233. String createAccountURL = getCreateAccountURL(
  234. memberRequest.getKey(), themeDisplay);
  235. String loginURL = getLoginURL(memberRequest.getKey(), themeDisplay);
  236. subject = StringUtil.replace(
  237. subject,
  238. new String[] {
  239. "[$MEMBER_REQUEST_GROUP$]",
  240. "[$MEMBER_REQUEST_USER$]"
  241. },
  242. new String[] {
  243. group.getDescriptiveName(),
  244. user.getFullName()
  245. });
  246. body = StringUtil.replace(
  247. body,
  248. new String[] {
  249. "[$ADMIN_ADDRESS$]",
  250. "[$ADMIN_NAME$]",
  251. "[$MEMBER_REQUEST_CREATE_ACCOUNT_URL$]",
  252. "[$MEMBER_REQUEST_GROUP$]",
  253. "[$MEMBER_REQUEST_LOGIN_URL$]",
  254. "[$MEMBER_REQUEST_USER$]"
  255. },
  256. new String[] {
  257. fromAddress,
  258. fromName,
  259. createAccountURL,
  260. group.getDescriptiveName(),
  261. loginURL,
  262. user.getFullName()
  263. });
  264. InternetAddress from = new InternetAddress(fromAddress, fromName);
  265. InternetAddress to = new InternetAddress(toAddress, toName);
  266. MailMessage mailMessage = new MailMessage(
  267. from, to, subject, body, true);
  268. MailServiceUtil.sendEmail(mailMessage);
  269. }
  270. protected void validate(MemberRequest memberRequest, long userId)
  271. throws PortalException {
  272. if (memberRequest.getStatus() !=
  273. InviteMembersConstants.STATUS_PENDING) {
  274. throw new MemberRequestAlreadyUsedException();
  275. }
  276. else if (memberRequest.getReceiverUserId() != userId) {
  277. throw new MemberRequestInvalidUserException();
  278. }
  279. }
  280. }