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.portal.service.ServiceContext;
30  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
31  import com.liferay.portlet.messageboards.MailingListInServerNameException;
32  import com.liferay.portlet.messageboards.MailingListInUserNameException;
33  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
34  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
35  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
36  import com.liferay.portlet.messageboards.messaging.MailingListRequest;
37  import com.liferay.portlet.messageboards.model.MBMailingList;
38  import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
39  
40  import java.util.Calendar;
41  import java.util.Date;
42  import java.util.List;
43  
44  /**
45   * <a href="MBMailingListLocalServiceImpl.java.html"><b><i>View Source</i></b>
46   * </a>
47   *
48   * @author Thiago Moreira
49   */
50  public class MBMailingListLocalServiceImpl
51      extends MBMailingListLocalServiceBaseImpl {
52  
53      public MBMailingList addMailingList(
54              String uuid, long userId, long groupId, long categoryId,
55              String emailAddress, String inProtocol, String inServerName,
56              int inServerPort, boolean inUseSSL, String inUserName,
57              String inPassword, int inReadInterval, String outEmailAddress,
58              boolean outCustom, String outServerName, int outServerPort,
59              boolean outUseSSL, String outUserName, String outPassword,
60              boolean active, ServiceContext serviceContext)
61          throws PortalException, SystemException {
62  
63          // Mailing list
64  
65          User user = userPersistence.findByPrimaryKey(userId);
66          Date now = new Date();
67  
68          validate(
69              emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
70              outServerName, outUserName, active);
71  
72          long mailingListId = counterLocalService.increment();
73  
74          MBMailingList mailingList = mbMailingListPersistence.create(
75              mailingListId);
76  
77          mailingList.setUuid(uuid);
78          mailingList.setGroupId(groupId);
79          mailingList.setCompanyId(user.getCompanyId());
80          mailingList.setUserId(user.getUserId());
81          mailingList.setUserName(user.getFullName());
82          mailingList.setCreateDate(serviceContext.getCreateDate(now));
83          mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
84          mailingList.setCategoryId(categoryId);
85          mailingList.setEmailAddress(emailAddress);
86          mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
87          mailingList.setInServerName(inServerName);
88          mailingList.setInServerPort(inServerPort);
89          mailingList.setInUseSSL(inUseSSL);
90          mailingList.setInUserName(inUserName);
91          mailingList.setInPassword(inPassword);
92          mailingList.setInReadInterval(inReadInterval);
93          mailingList.setOutEmailAddress(outEmailAddress);
94          mailingList.setOutCustom(outCustom);
95          mailingList.setOutServerName(outServerName);
96          mailingList.setOutServerPort(outServerPort);
97          mailingList.setOutUseSSL(outUseSSL);
98          mailingList.setOutUserName(outUserName);
99          mailingList.setOutPassword(outPassword);
100         mailingList.setActive(active);
101 
102         mbMailingListPersistence.update(mailingList, false);
103 
104         // Scheduler
105 
106         if (active) {
107             scheduleMailingList(mailingList);
108         }
109 
110         return mailingList;
111     }
112 
113     public void deleteCategoryMailingList(long groupId, long categoryId)
114         throws PortalException, SystemException {
115 
116         MBMailingList mailingList = mbMailingListPersistence.findByG_C(
117             groupId, categoryId);
118 
119         deleteMailingList(mailingList);
120     }
121 
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     public void deleteMailingList(MBMailingList mailingList)
132         throws PortalException, SystemException {
133 
134         unscheduleMailingList(mailingList);
135 
136         mbMailingListPersistence.remove(mailingList);
137     }
138 
139     public MBMailingList getCategoryMailingList(long groupId, long categoryId)
140         throws PortalException, SystemException {
141 
142         return mbMailingListPersistence.findByG_C(groupId, categoryId);
143     }
144 
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 active, ServiceContext serviceContext)
152         throws PortalException, SystemException {
153 
154         // Mailing list
155 
156         validate(
157             emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
158             outServerName, outUserName, active);
159 
160         MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
161             mailingListId);
162 
163         boolean oldActive = mailingList.isActive();
164 
165         mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
166         mailingList.setEmailAddress(emailAddress);
167         mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
168         mailingList.setInServerName(inServerName);
169         mailingList.setInServerPort(inServerPort);
170         mailingList.setInUseSSL(inUseSSL);
171         mailingList.setInUserName(inUserName);
172         mailingList.setInPassword(inPassword);
173         mailingList.setInReadInterval(inReadInterval);
174         mailingList.setOutEmailAddress(outEmailAddress);
175         mailingList.setOutCustom(outCustom);
176         mailingList.setOutServerName(outServerName);
177         mailingList.setOutServerPort(outServerPort);
178         mailingList.setOutUseSSL(outUseSSL);
179         mailingList.setOutUserName(outUserName);
180         mailingList.setOutPassword(outPassword);
181         mailingList.setActive(active);
182 
183         mbMailingListPersistence.update(mailingList, false);
184 
185         // Scheduler
186 
187         if (oldActive) {
188             unscheduleMailingList(mailingList);
189         }
190 
191         if (active) {
192             scheduleMailingList(mailingList);
193         }
194 
195         return mailingList;
196     }
197 
198     protected String getSchedulerGroupName(MBMailingList mailingList) {
199         return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
200             StringPool.SLASH).concat(
201                 String.valueOf(mailingList.getMailingListId()));
202     }
203 
204     protected void scheduleMailingList(MBMailingList mailingList)
205         throws PortalException {
206 
207         unscheduleMailingList(mailingList);
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 
234         SchedulerEngineUtil.schedule(
235             trigger, null, DestinationNames.MESSAGE_BOARDS_MAILING_LIST,
236             mailingListRequest);
237     }
238 
239     protected void unscheduleMailingList(MBMailingList mailingList)
240         throws PortalException {
241 
242         String groupName = getSchedulerGroupName(mailingList);
243 
244         List<SchedulerRequest> schedulerRequests =
245             SchedulerEngineUtil.getScheduledJobs(groupName);
246 
247         for (SchedulerRequest schedulerRequest : schedulerRequests) {
248             SchedulerEngineUtil.unschedule(schedulerRequest.getTrigger());
249         }
250     }
251 
252     protected void validate(
253             String emailAddress, String inServerName, String inUserName,
254             String outEmailAddress, boolean outCustom, String outServerName,
255             String outUserName, boolean active)
256         throws PortalException {
257 
258         if (!active) {
259             return;
260         }
261 
262         if (!Validator.isEmailAddress(emailAddress)) {
263             throw new MailingListEmailAddressException();
264         }
265         else if (Validator.isNull(inServerName)) {
266             throw new MailingListInServerNameException();
267         }
268         else if (Validator.isNull(inUserName)) {
269             throw new MailingListInUserNameException();
270         }
271         else if (Validator.isNull(outEmailAddress)) {
272             throw new MailingListOutEmailAddressException();
273         }
274         else if (outCustom) {
275             if (Validator.isNull(outServerName)) {
276                 throw new MailingListOutServerNameException();
277             }
278             else if (Validator.isNull(outUserName)) {
279                 throw new MailingListOutUserNameException();
280             }
281         }
282     }
283 
284 }