PageRenderTime 2ms CodeModel.GetById 35ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/lululiferay/liferay-portal
Java | 272 lines | 201 code | 51 blank | 20 comment | 15 complexity | cb125af6c883592f02b613cadbc4869c MD5 | raw file
  1/**
  2 * Copyright (c) 2000-2012 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
 15package com.liferay.portlet.messageboards.service.impl;
 16
 17import com.liferay.portal.kernel.exception.PortalException;
 18import com.liferay.portal.kernel.exception.SystemException;
 19import com.liferay.portal.kernel.messaging.DestinationNames;
 20import com.liferay.portal.kernel.scheduler.CronText;
 21import com.liferay.portal.kernel.scheduler.CronTrigger;
 22import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
 23import com.liferay.portal.kernel.scheduler.StorageType;
 24import com.liferay.portal.kernel.scheduler.Trigger;
 25import com.liferay.portal.kernel.util.CalendarFactoryUtil;
 26import com.liferay.portal.kernel.util.StringPool;
 27import com.liferay.portal.kernel.util.Validator;
 28import com.liferay.portal.model.User;
 29import com.liferay.portal.service.ServiceContext;
 30import com.liferay.portlet.messageboards.MailingListEmailAddressException;
 31import com.liferay.portlet.messageboards.MailingListInServerNameException;
 32import com.liferay.portlet.messageboards.MailingListInUserNameException;
 33import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
 34import com.liferay.portlet.messageboards.MailingListOutServerNameException;
 35import com.liferay.portlet.messageboards.MailingListOutUserNameException;
 36import com.liferay.portlet.messageboards.messaging.MailingListRequest;
 37import com.liferay.portlet.messageboards.model.MBMailingList;
 38import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
 39
 40import java.util.Calendar;
 41import java.util.Date;
 42
 43/**
 44 * @author Thiago Moreira
 45 */
 46public class MBMailingListLocalServiceImpl
 47	extends MBMailingListLocalServiceBaseImpl {
 48
 49	public MBMailingList addMailingList(
 50			long userId, long groupId, long categoryId, String emailAddress,
 51			String inProtocol, String inServerName, int inServerPort,
 52			boolean inUseSSL, String inUserName, String inPassword,
 53			int inReadInterval, String outEmailAddress, boolean outCustom,
 54			String outServerName, int outServerPort, boolean outUseSSL,
 55			String outUserName, String outPassword, boolean allowAnonymous,
 56			boolean active, ServiceContext serviceContext)
 57		throws PortalException, SystemException {
 58
 59		// Mailing list
 60
 61		User user = userPersistence.findByPrimaryKey(userId);
 62		Date now = new Date();
 63
 64		validate(
 65			emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
 66			outServerName, outUserName, active);
 67
 68		long mailingListId = counterLocalService.increment();
 69
 70		MBMailingList mailingList = mbMailingListPersistence.create(
 71			mailingListId);
 72
 73		mailingList.setUuid(serviceContext.getUuid());
 74		mailingList.setGroupId(groupId);
 75		mailingList.setCompanyId(user.getCompanyId());
 76		mailingList.setUserId(user.getUserId());
 77		mailingList.setUserName(user.getFullName());
 78		mailingList.setCreateDate(serviceContext.getCreateDate(now));
 79		mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
 80		mailingList.setCategoryId(categoryId);
 81		mailingList.setEmailAddress(emailAddress);
 82		mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
 83		mailingList.setInServerName(inServerName);
 84		mailingList.setInServerPort(inServerPort);
 85		mailingList.setInUseSSL(inUseSSL);
 86		mailingList.setInUserName(inUserName);
 87		mailingList.setInPassword(inPassword);
 88		mailingList.setInReadInterval(inReadInterval);
 89		mailingList.setOutEmailAddress(outEmailAddress);
 90		mailingList.setOutCustom(outCustom);
 91		mailingList.setOutServerName(outServerName);
 92		mailingList.setOutServerPort(outServerPort);
 93		mailingList.setOutUseSSL(outUseSSL);
 94		mailingList.setOutUserName(outUserName);
 95		mailingList.setOutPassword(outPassword);
 96		mailingList.setAllowAnonymous(allowAnonymous);
 97		mailingList.setActive(active);
 98
 99		mbMailingListPersistence.update(mailingList, false);
100
101		// Scheduler
102
103		if (active) {
104			scheduleMailingList(mailingList);
105		}
106
107		return mailingList;
108	}
109
110	public void deleteCategoryMailingList(long groupId, long categoryId)
111		throws PortalException, SystemException {
112
113		MBMailingList mailingList = mbMailingListPersistence.findByG_C(
114			groupId, categoryId);
115
116		deleteMailingList(mailingList);
117	}
118
119	public void deleteMailingList(long mailingListId)
120		throws PortalException, SystemException {
121
122		MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
123			mailingListId);
124
125		deleteMailingList(mailingList);
126	}
127
128	public void deleteMailingList(MBMailingList mailingList)
129		throws PortalException, SystemException {
130
131		unscheduleMailingList(mailingList);
132
133		mbMailingListPersistence.remove(mailingList);
134	}
135
136	public MBMailingList getCategoryMailingList(long groupId, long categoryId)
137		throws PortalException, SystemException {
138
139		return mbMailingListPersistence.findByG_C(groupId, categoryId);
140	}
141
142	public MBMailingList updateMailingList(
143			long mailingListId, String emailAddress, String inProtocol,
144			String inServerName, int inServerPort, boolean inUseSSL,
145			String inUserName, String inPassword, int inReadInterval,
146			String outEmailAddress, boolean outCustom, String outServerName,
147			int outServerPort, boolean outUseSSL, String outUserName,
148			String outPassword, boolean allowAnonymous, boolean active,
149			ServiceContext serviceContext)
150		throws PortalException, SystemException {
151
152		// Mailing list
153
154		validate(
155			emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
156			outServerName, outUserName, active);
157
158		MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
159			mailingListId);
160
161		mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
162		mailingList.setEmailAddress(emailAddress);
163		mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
164		mailingList.setInServerName(inServerName);
165		mailingList.setInServerPort(inServerPort);
166		mailingList.setInUseSSL(inUseSSL);
167		mailingList.setInUserName(inUserName);
168		mailingList.setInPassword(inPassword);
169		mailingList.setInReadInterval(inReadInterval);
170		mailingList.setOutEmailAddress(outEmailAddress);
171		mailingList.setOutCustom(outCustom);
172		mailingList.setOutServerName(outServerName);
173		mailingList.setOutServerPort(outServerPort);
174		mailingList.setOutUseSSL(outUseSSL);
175		mailingList.setOutUserName(outUserName);
176		mailingList.setOutPassword(outPassword);
177		mailingList.setAllowAnonymous(allowAnonymous);
178		mailingList.setActive(active);
179
180		mbMailingListPersistence.update(mailingList, false);
181
182		// Scheduler
183
184		if (active) {
185			scheduleMailingList(mailingList);
186		}
187
188		return mailingList;
189	}
190
191	protected String getSchedulerGroupName(MBMailingList mailingList) {
192		return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
193			StringPool.SLASH).concat(
194				String.valueOf(mailingList.getMailingListId()));
195	}
196
197	protected void scheduleMailingList(MBMailingList mailingList)
198		throws PortalException {
199
200		String groupName = getSchedulerGroupName(mailingList);
201
202		Calendar startDate = CalendarFactoryUtil.getCalendar();
203
204		CronText cronText = new CronText(
205			startDate, CronText.MINUTELY_FREQUENCY,
206			mailingList.getInReadInterval());
207
208		Trigger trigger = new CronTrigger(
209			groupName, groupName, startDate.getTime(), null,
210			cronText.toString());
211
212		MailingListRequest mailingListRequest = new MailingListRequest();
213
214		mailingListRequest.setCompanyId(mailingList.getCompanyId());
215		mailingListRequest.setUserId(mailingList.getUserId());
216		mailingListRequest.setGroupId(mailingList.getGroupId());
217		mailingListRequest.setCategoryId(mailingList.getCategoryId());
218		mailingListRequest.setInProtocol(mailingList.getInProtocol());
219		mailingListRequest.setInServerName(mailingList.getInServerName());
220		mailingListRequest.setInServerPort(mailingList.getInServerPort());
221		mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
222		mailingListRequest.setInUserName(mailingList.getInUserName());
223		mailingListRequest.setInPassword(mailingList.getInPassword());
224		mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
225
226		SchedulerEngineUtil.schedule(
227			trigger, StorageType.MEMORY_CLUSTERED, null,
228			DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
229			0);
230	}
231
232	protected void unscheduleMailingList(MBMailingList mailingList)
233		throws PortalException {
234
235		String groupName = getSchedulerGroupName(mailingList);
236
237		SchedulerEngineUtil.unschedule(groupName, StorageType.MEMORY_CLUSTERED);
238	}
239
240	protected void validate(
241			String emailAddress, String inServerName, String inUserName,
242			String outEmailAddress, boolean outCustom, String outServerName,
243			String outUserName, boolean active)
244		throws PortalException {
245
246		if (!active) {
247			return;
248		}
249
250		if (!Validator.isEmailAddress(emailAddress)) {
251			throw new MailingListEmailAddressException();
252		}
253		else if (Validator.isNull(inServerName)) {
254			throw new MailingListInServerNameException();
255		}
256		else if (Validator.isNull(inUserName)) {
257			throw new MailingListInUserNameException();
258		}
259		else if (Validator.isNull(outEmailAddress)) {
260			throw new MailingListOutEmailAddressException();
261		}
262		else if (outCustom) {
263			if (Validator.isNull(outServerName)) {
264				throw new MailingListOutServerNameException();
265			}
266			else if (Validator.isNull(outUserName)) {
267				throw new MailingListOutUserNameException();
268			}
269		}
270	}
271
272}