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