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