PageRenderTime 64ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portlet/messageboards/service/impl/MBMailingListLocalServiceImpl.java

https://github.com/acs/liferay-portal
Java | 272 lines | 201 code | 51 blank | 20 comment | 15 complexity | 82bf74ec326546049771e89f0a896c47 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.messageboards.service.impl;
  15. import com.liferay.portal.kernel.exception.PortalException;
  16. import com.liferay.portal.kernel.exception.SystemException;
  17. import com.liferay.portal.kernel.messaging.DestinationNames;
  18. import com.liferay.portal.kernel.scheduler.CronText;
  19. import com.liferay.portal.kernel.scheduler.CronTrigger;
  20. import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
  21. import com.liferay.portal.kernel.scheduler.StorageType;
  22. import com.liferay.portal.kernel.scheduler.Trigger;
  23. import com.liferay.portal.kernel.util.CalendarFactoryUtil;
  24. import com.liferay.portal.kernel.util.StringPool;
  25. import com.liferay.portal.kernel.util.Validator;
  26. import com.liferay.portal.model.User;
  27. import com.liferay.portal.service.ServiceContext;
  28. import com.liferay.portlet.messageboards.MailingListEmailAddressException;
  29. import com.liferay.portlet.messageboards.MailingListInServerNameException;
  30. import com.liferay.portlet.messageboards.MailingListInUserNameException;
  31. import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
  32. import com.liferay.portlet.messageboards.MailingListOutServerNameException;
  33. import com.liferay.portlet.messageboards.MailingListOutUserNameException;
  34. import com.liferay.portlet.messageboards.messaging.MailingListRequest;
  35. import com.liferay.portlet.messageboards.model.MBMailingList;
  36. import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
  37. import java.util.Calendar;
  38. import java.util.Date;
  39. /**
  40. * @author Thiago Moreira
  41. */
  42. public class MBMailingListLocalServiceImpl
  43. extends MBMailingListLocalServiceBaseImpl {
  44. public MBMailingList addMailingList(
  45. long userId, long groupId, long categoryId, String emailAddress,
  46. String inProtocol, String inServerName, int inServerPort,
  47. boolean inUseSSL, String inUserName, String inPassword,
  48. int inReadInterval, String outEmailAddress, boolean outCustom,
  49. String outServerName, int outServerPort, boolean outUseSSL,
  50. String outUserName, String outPassword, boolean allowAnonymous,
  51. boolean active, ServiceContext serviceContext)
  52. throws PortalException, SystemException {
  53. // Mailing list
  54. User user = userPersistence.findByPrimaryKey(userId);
  55. Date now = new Date();
  56. validate(
  57. emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
  58. outServerName, outUserName, active);
  59. long mailingListId = counterLocalService.increment();
  60. MBMailingList mailingList = mbMailingListPersistence.create(
  61. mailingListId);
  62. mailingList.setUuid(serviceContext.getUuid());
  63. mailingList.setGroupId(groupId);
  64. mailingList.setCompanyId(user.getCompanyId());
  65. mailingList.setUserId(user.getUserId());
  66. mailingList.setUserName(user.getFullName());
  67. mailingList.setCreateDate(serviceContext.getCreateDate(now));
  68. mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
  69. mailingList.setCategoryId(categoryId);
  70. mailingList.setEmailAddress(emailAddress);
  71. mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
  72. mailingList.setInServerName(inServerName);
  73. mailingList.setInServerPort(inServerPort);
  74. mailingList.setInUseSSL(inUseSSL);
  75. mailingList.setInUserName(inUserName);
  76. mailingList.setInPassword(inPassword);
  77. mailingList.setInReadInterval(inReadInterval);
  78. mailingList.setOutEmailAddress(outEmailAddress);
  79. mailingList.setOutCustom(outCustom);
  80. mailingList.setOutServerName(outServerName);
  81. mailingList.setOutServerPort(outServerPort);
  82. mailingList.setOutUseSSL(outUseSSL);
  83. mailingList.setOutUserName(outUserName);
  84. mailingList.setOutPassword(outPassword);
  85. mailingList.setAllowAnonymous(allowAnonymous);
  86. mailingList.setActive(active);
  87. mbMailingListPersistence.update(mailingList, false);
  88. // Scheduler
  89. if (active) {
  90. scheduleMailingList(mailingList);
  91. }
  92. return mailingList;
  93. }
  94. public void deleteCategoryMailingList(long groupId, long categoryId)
  95. throws PortalException, SystemException {
  96. MBMailingList mailingList = mbMailingListPersistence.findByG_C(
  97. groupId, categoryId);
  98. deleteMailingList(mailingList);
  99. }
  100. public void deleteMailingList(long mailingListId)
  101. throws PortalException, SystemException {
  102. MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
  103. mailingListId);
  104. deleteMailingList(mailingList);
  105. }
  106. public void deleteMailingList(MBMailingList mailingList)
  107. throws PortalException, SystemException {
  108. unscheduleMailingList(mailingList);
  109. mbMailingListPersistence.remove(mailingList);
  110. }
  111. public MBMailingList getCategoryMailingList(long groupId, long categoryId)
  112. throws PortalException, SystemException {
  113. return mbMailingListPersistence.findByG_C(groupId, categoryId);
  114. }
  115. public MBMailingList updateMailingList(
  116. long mailingListId, String emailAddress, String inProtocol,
  117. String inServerName, int inServerPort, boolean inUseSSL,
  118. String inUserName, String inPassword, int inReadInterval,
  119. String outEmailAddress, boolean outCustom, String outServerName,
  120. int outServerPort, boolean outUseSSL, String outUserName,
  121. String outPassword, boolean allowAnonymous, boolean active,
  122. ServiceContext serviceContext)
  123. throws PortalException, SystemException {
  124. // Mailing list
  125. validate(
  126. emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
  127. outServerName, outUserName, active);
  128. MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
  129. mailingListId);
  130. mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
  131. mailingList.setEmailAddress(emailAddress);
  132. mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
  133. mailingList.setInServerName(inServerName);
  134. mailingList.setInServerPort(inServerPort);
  135. mailingList.setInUseSSL(inUseSSL);
  136. mailingList.setInUserName(inUserName);
  137. mailingList.setInPassword(inPassword);
  138. mailingList.setInReadInterval(inReadInterval);
  139. mailingList.setOutEmailAddress(outEmailAddress);
  140. mailingList.setOutCustom(outCustom);
  141. mailingList.setOutServerName(outServerName);
  142. mailingList.setOutServerPort(outServerPort);
  143. mailingList.setOutUseSSL(outUseSSL);
  144. mailingList.setOutUserName(outUserName);
  145. mailingList.setOutPassword(outPassword);
  146. mailingList.setAllowAnonymous(allowAnonymous);
  147. mailingList.setActive(active);
  148. mbMailingListPersistence.update(mailingList, false);
  149. // Scheduler
  150. if (active) {
  151. scheduleMailingList(mailingList);
  152. }
  153. return mailingList;
  154. }
  155. protected String getSchedulerGroupName(MBMailingList mailingList) {
  156. return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
  157. StringPool.SLASH).concat(
  158. String.valueOf(mailingList.getMailingListId()));
  159. }
  160. protected void scheduleMailingList(MBMailingList mailingList)
  161. throws PortalException {
  162. String groupName = getSchedulerGroupName(mailingList);
  163. Calendar startDate = CalendarFactoryUtil.getCalendar();
  164. CronText cronText = new CronText(
  165. startDate, CronText.MINUTELY_FREQUENCY,
  166. mailingList.getInReadInterval());
  167. Trigger trigger = new CronTrigger(
  168. groupName, groupName, startDate.getTime(), null,
  169. cronText.toString());
  170. MailingListRequest mailingListRequest = new MailingListRequest();
  171. mailingListRequest.setCompanyId(mailingList.getCompanyId());
  172. mailingListRequest.setUserId(mailingList.getUserId());
  173. mailingListRequest.setGroupId(mailingList.getGroupId());
  174. mailingListRequest.setCategoryId(mailingList.getCategoryId());
  175. mailingListRequest.setInProtocol(mailingList.getInProtocol());
  176. mailingListRequest.setInServerName(mailingList.getInServerName());
  177. mailingListRequest.setInServerPort(mailingList.getInServerPort());
  178. mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
  179. mailingListRequest.setInUserName(mailingList.getInUserName());
  180. mailingListRequest.setInPassword(mailingList.getInPassword());
  181. mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
  182. SchedulerEngineUtil.schedule(
  183. trigger, StorageType.MEMORY_CLUSTERED, null,
  184. DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
  185. 0);
  186. }
  187. protected void unscheduleMailingList(MBMailingList mailingList)
  188. throws PortalException {
  189. String groupName = getSchedulerGroupName(mailingList);
  190. SchedulerEngineUtil.unschedule(groupName, StorageType.MEMORY_CLUSTERED);
  191. }
  192. protected void validate(
  193. String emailAddress, String inServerName, String inUserName,
  194. String outEmailAddress, boolean outCustom, String outServerName,
  195. String outUserName, boolean active)
  196. throws PortalException {
  197. if (!active) {
  198. return;
  199. }
  200. if (!Validator.isEmailAddress(emailAddress)) {
  201. throw new MailingListEmailAddressException();
  202. }
  203. else if (Validator.isNull(inServerName)) {
  204. throw new MailingListInServerNameException();
  205. }
  206. else if (Validator.isNull(inUserName)) {
  207. throw new MailingListInUserNameException();
  208. }
  209. else if (Validator.isNull(outEmailAddress)) {
  210. throw new MailingListOutEmailAddressException();
  211. }
  212. else if (outCustom) {
  213. if (Validator.isNull(outServerName)) {
  214. throw new MailingListOutServerNameException();
  215. }
  216. else if (Validator.isNull(outUserName)) {
  217. throw new MailingListOutUserNameException();
  218. }
  219. }
  220. }
  221. }