1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.messaging.DestinationNames;
21  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
22  import com.liferay.portal.kernel.scheduler.TriggerExpression;
23  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
24  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.User;
28  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
29  import com.liferay.portlet.messageboards.MailingListInServerNameException;
30  import com.liferay.portlet.messageboards.MailingListInUserNameException;
31  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
32  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
33  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
34  import com.liferay.portlet.messageboards.messaging.MailingListRequest;
35  import com.liferay.portlet.messageboards.model.MBCategory;
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 categoryId, String emailAddress,
54              String inProtocol, String inServerName, int inServerPort,
55              boolean inUseSSL, String inUserName, String inPassword,
56              int inReadInterval, String outEmailAddress, boolean outCustom,
57              String outServerName, int outServerPort, boolean outUseSSL,
58              String outUserName, String outPassword, boolean active)
59          throws PortalException, SystemException {
60  
61          // Mailing list
62  
63          User user = userPersistence.findByPrimaryKey(userId);
64          MBCategory category = mbCategoryPersistence.findByPrimaryKey(
65              categoryId);
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(category.getGroupId());
79          mailingList.setCompanyId(user.getCompanyId());
80          mailingList.setUserId(user.getUserId());
81          mailingList.setUserName(user.getFullName());
82          mailingList.setCreateDate(now);
83          mailingList.setModifiedDate(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 categoryId)
114         throws PortalException, SystemException {
115 
116         MBMailingList mailingList = mbMailingListPersistence.findByCategoryId(
117             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 categoryId)
140         throws PortalException, SystemException {
141 
142         return mbMailingListPersistence.findByCategoryId(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)
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(new Date());
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         TriggerExpression triggerExpression = new TriggerExpression(
214             startDate, TriggerExpression.MINUTELY_FREQUENCY,
215             mailingList.getInReadInterval());
216 
217         String cronText = triggerExpression.toCronText();
218 
219         MailingListRequest mailingListRequest = new MailingListRequest();
220 
221         mailingListRequest.setCompanyId(mailingList.getCompanyId());
222         mailingListRequest.setUserId(mailingList.getUserId());
223         mailingListRequest.setGroupId(mailingList.getGroupId());
224         mailingListRequest.setCategoryId(mailingList.getCategoryId());
225         mailingListRequest.setInProtocol(mailingList.getInProtocol());
226         mailingListRequest.setInServerName(mailingList.getInServerName());
227         mailingListRequest.setInServerPort(mailingList.getInServerPort());
228         mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
229         mailingListRequest.setInUserName(mailingList.getInUserName());
230         mailingListRequest.setInPassword(mailingList.getInPassword());
231 
232         SchedulerEngineUtil.schedule(
233             groupName, cronText, startDate.getTime(), null, null,
234             DestinationNames.MESSAGE_BOARDS_MAILING_LIST,
235             JSONFactoryUtil.serialize(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(
248                 schedulerRequest.getJobName(), schedulerRequest.getGroupName());
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 }