1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.json.JSONFactoryUtil;
28  import com.liferay.portal.kernel.messaging.DestinationNames;
29  import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
30  import com.liferay.portal.kernel.scheduler.TriggerExpression;
31  import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
32  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.User;
36  import com.liferay.portlet.messageboards.MailingListEmailAddressException;
37  import com.liferay.portlet.messageboards.MailingListInServerNameException;
38  import com.liferay.portlet.messageboards.MailingListInUserNameException;
39  import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
40  import com.liferay.portlet.messageboards.MailingListOutServerNameException;
41  import com.liferay.portlet.messageboards.MailingListOutUserNameException;
42  import com.liferay.portlet.messageboards.messaging.MailingListRequest;
43  import com.liferay.portlet.messageboards.model.MBCategory;
44  import com.liferay.portlet.messageboards.model.MBMailingList;
45  import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
46  
47  import java.util.Calendar;
48  import java.util.Date;
49  import java.util.List;
50  
51  /**
52   * <a href="MBMailingListLocalServiceImpl.java.html"><b><i>View Source</i></b>
53   * </a>
54   *
55   * @author Thiago Moreira
56   */
57  public class MBMailingListLocalServiceImpl
58      extends MBMailingListLocalServiceBaseImpl {
59  
60      public MBMailingList addMailingList(
61              String uuid, long userId, long categoryId, String emailAddress,
62              String inProtocol, String inServerName, int inServerPort,
63              boolean inUseSSL, String inUserName, String inPassword,
64              int inReadInterval, String outEmailAddress, boolean outCustom,
65              String outServerName, int outServerPort, boolean outUseSSL,
66              String outUserName, String outPassword, boolean active)
67          throws PortalException, SystemException {
68  
69          // Mailing list
70  
71          User user = userPersistence.findByPrimaryKey(userId);
72          MBCategory category = mbCategoryPersistence.findByPrimaryKey(
73              categoryId);
74          Date now = new Date();
75  
76          validate(
77              emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
78              outServerName, outUserName, active);
79  
80          long mailingListId = counterLocalService.increment();
81  
82          MBMailingList mailingList = mbMailingListPersistence.create(
83              mailingListId);
84  
85          mailingList.setUuid(uuid);
86          mailingList.setGroupId(category.getGroupId());
87          mailingList.setCompanyId(user.getCompanyId());
88          mailingList.setUserId(user.getUserId());
89          mailingList.setUserName(user.getFullName());
90          mailingList.setCreateDate(now);
91          mailingList.setModifiedDate(now);
92          mailingList.setCategoryId(categoryId);
93          mailingList.setEmailAddress(emailAddress);
94          mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
95          mailingList.setInServerName(inServerName);
96          mailingList.setInServerPort(inServerPort);
97          mailingList.setInUseSSL(inUseSSL);
98          mailingList.setInUserName(inUserName);
99          mailingList.setInPassword(inPassword);
100         mailingList.setInReadInterval(inReadInterval);
101         mailingList.setOutEmailAddress(outEmailAddress);
102         mailingList.setOutCustom(outCustom);
103         mailingList.setOutServerName(outServerName);
104         mailingList.setOutServerPort(outServerPort);
105         mailingList.setOutUseSSL(outUseSSL);
106         mailingList.setOutUserName(outUserName);
107         mailingList.setOutPassword(outPassword);
108         mailingList.setActive(active);
109 
110         mbMailingListPersistence.update(mailingList, false);
111 
112         // Scheduler
113 
114         if (active) {
115             scheduleMailingList(mailingList);
116         }
117 
118         return mailingList;
119     }
120 
121     public void deleteCategoryMailingList(long categoryId)
122         throws PortalException, SystemException {
123 
124         MBMailingList mailingList = mbMailingListPersistence.findByCategoryId(
125             categoryId);
126 
127         deleteMailingList(mailingList);
128     }
129 
130     public void deleteMailingList(long mailingListId)
131         throws PortalException, SystemException {
132 
133         MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
134             mailingListId);
135 
136         deleteMailingList(mailingList);
137     }
138 
139     public void deleteMailingList(MBMailingList mailingList)
140         throws PortalException, SystemException {
141 
142         unscheduleMailingList(mailingList);
143 
144         mbMailingListPersistence.remove(mailingList);
145     }
146 
147     public MBMailingList getCategoryMailingList(long categoryId)
148         throws PortalException, SystemException {
149 
150         return mbMailingListPersistence.findByCategoryId(categoryId);
151     }
152 
153     public MBMailingList updateMailingList(
154             long mailingListId, String emailAddress, String inProtocol,
155             String inServerName, int inServerPort, boolean inUseSSL,
156             String inUserName, String inPassword, int inReadInterval,
157             String outEmailAddress, boolean outCustom, String outServerName,
158             int outServerPort, boolean outUseSSL, String outUserName,
159             String outPassword, boolean active)
160         throws PortalException, SystemException {
161 
162         // Mailing list
163 
164         validate(
165             emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
166             outServerName, outUserName, active);
167 
168         MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
169             mailingListId);
170 
171         boolean oldActive = mailingList.isActive();
172 
173         mailingList.setModifiedDate(new Date());
174         mailingList.setEmailAddress(emailAddress);
175         mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
176         mailingList.setInServerName(inServerName);
177         mailingList.setInServerPort(inServerPort);
178         mailingList.setInUseSSL(inUseSSL);
179         mailingList.setInUserName(inUserName);
180         mailingList.setInPassword(inPassword);
181         mailingList.setInReadInterval(inReadInterval);
182         mailingList.setOutEmailAddress(outEmailAddress);
183         mailingList.setOutCustom(outCustom);
184         mailingList.setOutServerName(outServerName);
185         mailingList.setOutServerPort(outServerPort);
186         mailingList.setOutUseSSL(outUseSSL);
187         mailingList.setOutUserName(outUserName);
188         mailingList.setOutPassword(outPassword);
189         mailingList.setActive(active);
190 
191         mbMailingListPersistence.update(mailingList, false);
192 
193         // Scheduler
194 
195         if (oldActive) {
196             unscheduleMailingList(mailingList);
197         }
198 
199         if (active) {
200             scheduleMailingList(mailingList);
201         }
202 
203         return mailingList;
204     }
205 
206     protected String getSchedulerGroupName(MBMailingList mailingList) {
207         StringBuilder sb = new StringBuilder();
208 
209         sb.append(DestinationNames.MESSAGE_BOARDS_MAILING_LIST);
210         sb.append(StringPool.SLASH);
211         sb.append(mailingList.getMailingListId());
212 
213         return sb.toString();
214     }
215 
216     protected void scheduleMailingList(MBMailingList mailingList)
217         throws PortalException {
218 
219         unscheduleMailingList(mailingList);
220 
221         String groupName = getSchedulerGroupName(mailingList);
222 
223         Calendar startDate = CalendarFactoryUtil.getCalendar();
224 
225         TriggerExpression triggerExpression = new TriggerExpression(
226             startDate, TriggerExpression.MINUTELY_FREQUENCY,
227             mailingList.getInReadInterval());
228 
229         String cronText = triggerExpression.toCronText();
230 
231         MailingListRequest mailingListRequest = new MailingListRequest();
232 
233         mailingListRequest.setCompanyId(mailingList.getCompanyId());
234         mailingListRequest.setUserId(mailingList.getUserId());
235         mailingListRequest.setGroupId(mailingList.getGroupId());
236         mailingListRequest.setCategoryId(mailingList.getCategoryId());
237         mailingListRequest.setInProtocol(mailingList.getInProtocol());
238         mailingListRequest.setInServerName(mailingList.getInServerName());
239         mailingListRequest.setInServerPort(mailingList.getInServerPort());
240         mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
241         mailingListRequest.setInUserName(mailingList.getInUserName());
242         mailingListRequest.setInPassword(mailingList.getInPassword());
243 
244         SchedulerEngineUtil.schedule(
245             groupName, cronText, startDate.getTime(), null, null,
246             DestinationNames.MESSAGE_BOARDS_MAILING_LIST,
247             JSONFactoryUtil.serialize(mailingListRequest));
248     }
249 
250     protected void unscheduleMailingList(MBMailingList mailingList)
251         throws PortalException {
252 
253         String groupName = getSchedulerGroupName(mailingList);
254 
255         List<SchedulerRequest> schedulerRequests =
256             SchedulerEngineUtil.getScheduledJobs(groupName);
257 
258         for (SchedulerRequest schedulerRequest : schedulerRequests) {
259             SchedulerEngineUtil.unschedule(
260                 schedulerRequest.getJobName(), schedulerRequest.getGroupName());
261         }
262     }
263 
264     protected void validate(
265             String emailAddress, String inServerName, String inUserName,
266             String outEmailAddress, boolean outCustom, String outServerName,
267             String outUserName, boolean active)
268         throws PortalException {
269 
270         if (!active) {
271             return;
272         }
273 
274         if (!Validator.isEmailAddress(emailAddress)) {
275             throw new MailingListEmailAddressException();
276         }
277         else if (Validator.isNull(inServerName)) {
278             throw new MailingListInServerNameException();
279         }
280         else if (Validator.isNull(inUserName)) {
281             throw new MailingListInUserNameException();
282         }
283         else if (Validator.isNull(outEmailAddress)) {
284             throw new MailingListOutEmailAddressException();
285         }
286         else if (outCustom) {
287             if (Validator.isNull(outServerName)) {
288                 throw new MailingListOutServerNameException();
289             }
290             else if (Validator.isNull(outUserName)) {
291                 throw new MailingListOutUserNameException();
292             }
293         }
294     }
295 
296 }