001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.messaging.DestinationNames;
020 import com.liferay.portal.kernel.scheduler.CronText;
021 import com.liferay.portal.kernel.scheduler.CronTrigger;
022 import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
023 import com.liferay.portal.kernel.scheduler.StorageType;
024 import com.liferay.portal.kernel.scheduler.Trigger;
025 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portlet.messageboards.MailingListEmailAddressException;
031 import com.liferay.portlet.messageboards.MailingListInServerNameException;
032 import com.liferay.portlet.messageboards.MailingListInUserNameException;
033 import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
034 import com.liferay.portlet.messageboards.MailingListOutServerNameException;
035 import com.liferay.portlet.messageboards.MailingListOutUserNameException;
036 import com.liferay.portlet.messageboards.messaging.MailingListRequest;
037 import com.liferay.portlet.messageboards.model.MBMailingList;
038 import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
039
040 import java.util.Calendar;
041 import java.util.Date;
042
043
046 public class MBMailingListLocalServiceImpl
047 extends MBMailingListLocalServiceBaseImpl {
048
049 @Override
050 public MBMailingList addMailingList(
051 long userId, long groupId, long categoryId, String emailAddress,
052 String inProtocol, String inServerName, int inServerPort,
053 boolean inUseSSL, String inUserName, String inPassword,
054 int inReadInterval, String outEmailAddress, boolean outCustom,
055 String outServerName, int outServerPort, boolean outUseSSL,
056 String outUserName, String outPassword, boolean allowAnonymous,
057 boolean active, ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060
061
062 User user = userPersistence.findByPrimaryKey(userId);
063 Date now = new Date();
064
065 validate(
066 emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
067 outServerName, outUserName, active);
068
069 long mailingListId = counterLocalService.increment();
070
071 MBMailingList mailingList = mbMailingListPersistence.create(
072 mailingListId);
073
074 mailingList.setUuid(serviceContext.getUuid());
075 mailingList.setGroupId(groupId);
076 mailingList.setCompanyId(user.getCompanyId());
077 mailingList.setUserId(user.getUserId());
078 mailingList.setUserName(user.getFullName());
079 mailingList.setCreateDate(serviceContext.getCreateDate(now));
080 mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
081 mailingList.setCategoryId(categoryId);
082 mailingList.setEmailAddress(emailAddress);
083 mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
084 mailingList.setInServerName(inServerName);
085 mailingList.setInServerPort(inServerPort);
086 mailingList.setInUseSSL(inUseSSL);
087 mailingList.setInUserName(inUserName);
088 mailingList.setInPassword(inPassword);
089 mailingList.setInReadInterval(inReadInterval);
090 mailingList.setOutEmailAddress(outEmailAddress);
091 mailingList.setOutCustom(outCustom);
092 mailingList.setOutServerName(outServerName);
093 mailingList.setOutServerPort(outServerPort);
094 mailingList.setOutUseSSL(outUseSSL);
095 mailingList.setOutUserName(outUserName);
096 mailingList.setOutPassword(outPassword);
097 mailingList.setAllowAnonymous(allowAnonymous);
098 mailingList.setActive(active);
099
100 mbMailingListPersistence.update(mailingList);
101
102
103
104 if (active) {
105 scheduleMailingList(mailingList);
106 }
107
108 return mailingList;
109 }
110
111 @Override
112 public void deleteCategoryMailingList(long groupId, long categoryId)
113 throws PortalException, SystemException {
114
115 MBMailingList mailingList = mbMailingListPersistence.findByG_C(
116 groupId, categoryId);
117
118 deleteMailingList(mailingList);
119 }
120
121 @Override
122 public void deleteMailingList(long mailingListId)
123 throws PortalException, SystemException {
124
125 MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
126 mailingListId);
127
128 deleteMailingList(mailingList);
129 }
130
131 @Override
132 public void deleteMailingList(MBMailingList mailingList)
133 throws PortalException, SystemException {
134
135 unscheduleMailingList(mailingList);
136
137 mbMailingListPersistence.remove(mailingList);
138 }
139
140 @Override
141 public MBMailingList getCategoryMailingList(long groupId, long categoryId)
142 throws PortalException, SystemException {
143
144 return mbMailingListPersistence.findByG_C(groupId, categoryId);
145 }
146
147 @Override
148 public MBMailingList updateMailingList(
149 long mailingListId, String emailAddress, String inProtocol,
150 String inServerName, int inServerPort, boolean inUseSSL,
151 String inUserName, String inPassword, int inReadInterval,
152 String outEmailAddress, boolean outCustom, String outServerName,
153 int outServerPort, boolean outUseSSL, String outUserName,
154 String outPassword, boolean allowAnonymous, boolean active,
155 ServiceContext serviceContext)
156 throws PortalException, SystemException {
157
158
159
160 validate(
161 emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
162 outServerName, outUserName, active);
163
164 MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
165 mailingListId);
166
167 mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
168 mailingList.setEmailAddress(emailAddress);
169 mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
170 mailingList.setInServerName(inServerName);
171 mailingList.setInServerPort(inServerPort);
172 mailingList.setInUseSSL(inUseSSL);
173 mailingList.setInUserName(inUserName);
174 mailingList.setInPassword(inPassword);
175 mailingList.setInReadInterval(inReadInterval);
176 mailingList.setOutEmailAddress(outEmailAddress);
177 mailingList.setOutCustom(outCustom);
178 mailingList.setOutServerName(outServerName);
179 mailingList.setOutServerPort(outServerPort);
180 mailingList.setOutUseSSL(outUseSSL);
181 mailingList.setOutUserName(outUserName);
182 mailingList.setOutPassword(outPassword);
183 mailingList.setAllowAnonymous(allowAnonymous);
184 mailingList.setActive(active);
185
186 mbMailingListPersistence.update(mailingList);
187
188
189
190 if (active) {
191 scheduleMailingList(mailingList);
192 }
193 else {
194 unscheduleMailingList(mailingList);
195 }
196
197 return mailingList;
198 }
199
200 protected String getSchedulerGroupName(MBMailingList mailingList) {
201 return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
202 StringPool.SLASH).concat(
203 String.valueOf(mailingList.getMailingListId()));
204 }
205
206 protected void scheduleMailingList(MBMailingList mailingList)
207 throws PortalException {
208
209 String groupName = getSchedulerGroupName(mailingList);
210
211 Calendar startDate = CalendarFactoryUtil.getCalendar();
212
213 CronText cronText = new CronText(
214 startDate, CronText.MINUTELY_FREQUENCY,
215 mailingList.getInReadInterval());
216
217 Trigger trigger = new CronTrigger(
218 groupName, groupName, startDate.getTime(), null,
219 cronText.toString());
220
221 MailingListRequest mailingListRequest = new MailingListRequest();
222
223 mailingListRequest.setCompanyId(mailingList.getCompanyId());
224 mailingListRequest.setUserId(mailingList.getUserId());
225 mailingListRequest.setGroupId(mailingList.getGroupId());
226 mailingListRequest.setCategoryId(mailingList.getCategoryId());
227 mailingListRequest.setInProtocol(mailingList.getInProtocol());
228 mailingListRequest.setInServerName(mailingList.getInServerName());
229 mailingListRequest.setInServerPort(mailingList.getInServerPort());
230 mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
231 mailingListRequest.setInUserName(mailingList.getInUserName());
232 mailingListRequest.setInPassword(mailingList.getInPassword());
233 mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
234
235 SchedulerEngineHelperUtil.schedule(
236 trigger, StorageType.PERSISTED, null,
237 DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
238 0);
239 }
240
241 protected void unscheduleMailingList(MBMailingList mailingList)
242 throws PortalException {
243
244 String groupName = getSchedulerGroupName(mailingList);
245
246 SchedulerEngineHelperUtil.unschedule(groupName, StorageType.PERSISTED);
247 }
248
249 protected void validate(
250 String emailAddress, String inServerName, String inUserName,
251 String outEmailAddress, boolean outCustom, String outServerName,
252 String outUserName, boolean active)
253 throws PortalException {
254
255 if (!active) {
256 return;
257 }
258
259 if (!Validator.isEmailAddress(emailAddress)) {
260 throw new MailingListEmailAddressException();
261 }
262 else if (Validator.isNull(inServerName)) {
263 throw new MailingListInServerNameException();
264 }
265 else if (Validator.isNull(inUserName)) {
266 throw new MailingListInUserNameException();
267 }
268 else if (Validator.isNull(outEmailAddress)) {
269 throw new MailingListOutEmailAddressException();
270 }
271 else if (outCustom) {
272 if (Validator.isNull(outServerName)) {
273 throw new MailingListOutServerNameException();
274 }
275 else if (Validator.isNull(outUserName)) {
276 throw new MailingListOutUserNameException();
277 }
278 }
279 }
280
281 }