001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.messaging.DestinationNames;
020    import com.liferay.portal.kernel.scheduler.CronText;
021    import com.liferay.portal.kernel.scheduler.CronTrigger;
022    import com.liferay.portal.kernel.scheduler.SchedulerEngineUtil;
023    import com.liferay.portal.kernel.scheduler.Trigger;
024    import com.liferay.portal.kernel.scheduler.messaging.SchedulerRequest;
025    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.messageboards.MailingListEmailAddressException;
031    import com.liferay.portlet.messageboards.MailingListInServerNameException;
032    import com.liferay.portlet.messageboards.MailingListInUserNameException;
033    import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
034    import com.liferay.portlet.messageboards.MailingListOutServerNameException;
035    import com.liferay.portlet.messageboards.MailingListOutUserNameException;
036    import com.liferay.portlet.messageboards.messaging.MailingListRequest;
037    import com.liferay.portlet.messageboards.model.MBMailingList;
038    import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
039    
040    import java.util.Calendar;
041    import java.util.Date;
042    import java.util.List;
043    
044    /**
045     * @author Thiago Moreira
046     */
047    public class MBMailingListLocalServiceImpl
048            extends MBMailingListLocalServiceBaseImpl {
049    
050            public MBMailingList addMailingList(
051                            long userId, long groupId, long categoryId, String emailAddress,
052                            String inProtocol, String inServerName, int inServerPort,
053                            boolean inUseSSL, String inUserName, String inPassword,
054                            int inReadInterval, String outEmailAddress, boolean outCustom,
055                            String outServerName, int outServerPort, boolean outUseSSL,
056                            String outUserName, String outPassword, boolean active,
057                            ServiceContext serviceContext)
058                    throws PortalException, SystemException {
059    
060                    // Mailing list
061    
062                    User user = userPersistence.findByPrimaryKey(userId);
063                    Date now = new Date();
064    
065                    validate(
066                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
067                            outServerName, outUserName, active);
068    
069                    long mailingListId = counterLocalService.increment();
070    
071                    MBMailingList mailingList = mbMailingListPersistence.create(
072                            mailingListId);
073    
074                    mailingList.setUuid(serviceContext.getUuid());
075                    mailingList.setGroupId(groupId);
076                    mailingList.setCompanyId(user.getCompanyId());
077                    mailingList.setUserId(user.getUserId());
078                    mailingList.setUserName(user.getFullName());
079                    mailingList.setCreateDate(serviceContext.getCreateDate(now));
080                    mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
081                    mailingList.setCategoryId(categoryId);
082                    mailingList.setEmailAddress(emailAddress);
083                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
084                    mailingList.setInServerName(inServerName);
085                    mailingList.setInServerPort(inServerPort);
086                    mailingList.setInUseSSL(inUseSSL);
087                    mailingList.setInUserName(inUserName);
088                    mailingList.setInPassword(inPassword);
089                    mailingList.setInReadInterval(inReadInterval);
090                    mailingList.setOutEmailAddress(outEmailAddress);
091                    mailingList.setOutCustom(outCustom);
092                    mailingList.setOutServerName(outServerName);
093                    mailingList.setOutServerPort(outServerPort);
094                    mailingList.setOutUseSSL(outUseSSL);
095                    mailingList.setOutUserName(outUserName);
096                    mailingList.setOutPassword(outPassword);
097                    mailingList.setActive(active);
098    
099                    mbMailingListPersistence.update(mailingList, false);
100    
101                    // Scheduler
102    
103                    if (active) {
104                            scheduleMailingList(mailingList);
105                    }
106    
107                    return mailingList;
108            }
109    
110            public void deleteCategoryMailingList(long groupId, long categoryId)
111                    throws PortalException, SystemException {
112    
113                    MBMailingList mailingList = mbMailingListPersistence.findByG_C(
114                            groupId, categoryId);
115    
116                    deleteMailingList(mailingList);
117            }
118    
119            public void deleteMailingList(long mailingListId)
120                    throws PortalException, SystemException {
121    
122                    MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
123                            mailingListId);
124    
125                    deleteMailingList(mailingList);
126            }
127    
128            public void deleteMailingList(MBMailingList mailingList)
129                    throws PortalException, SystemException {
130    
131                    unscheduleMailingList(mailingList);
132    
133                    mbMailingListPersistence.remove(mailingList);
134            }
135    
136            public MBMailingList getCategoryMailingList(long groupId, long categoryId)
137                    throws PortalException, SystemException {
138    
139                    return mbMailingListPersistence.findByG_C(groupId, categoryId);
140            }
141    
142            public MBMailingList updateMailingList(
143                            long mailingListId, String emailAddress, String inProtocol,
144                            String inServerName, int inServerPort, boolean inUseSSL,
145                            String inUserName, String inPassword, int inReadInterval,
146                            String outEmailAddress, boolean outCustom, String outServerName,
147                            int outServerPort, boolean outUseSSL, String outUserName,
148                            String outPassword, boolean active, ServiceContext serviceContext)
149                    throws PortalException, SystemException {
150    
151                    // Mailing list
152    
153                    validate(
154                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
155                            outServerName, outUserName, active);
156    
157                    MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
158                            mailingListId);
159    
160                    boolean oldActive = mailingList.isActive();
161    
162                    mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
163                    mailingList.setEmailAddress(emailAddress);
164                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
165                    mailingList.setInServerName(inServerName);
166                    mailingList.setInServerPort(inServerPort);
167                    mailingList.setInUseSSL(inUseSSL);
168                    mailingList.setInUserName(inUserName);
169                    mailingList.setInPassword(inPassword);
170                    mailingList.setInReadInterval(inReadInterval);
171                    mailingList.setOutEmailAddress(outEmailAddress);
172                    mailingList.setOutCustom(outCustom);
173                    mailingList.setOutServerName(outServerName);
174                    mailingList.setOutServerPort(outServerPort);
175                    mailingList.setOutUseSSL(outUseSSL);
176                    mailingList.setOutUserName(outUserName);
177                    mailingList.setOutPassword(outPassword);
178                    mailingList.setActive(active);
179    
180                    mbMailingListPersistence.update(mailingList, false);
181    
182                    // Scheduler
183    
184                    if (oldActive) {
185                            unscheduleMailingList(mailingList);
186                    }
187    
188                    if (active) {
189                            scheduleMailingList(mailingList);
190                    }
191    
192                    return mailingList;
193            }
194    
195            protected String getSchedulerGroupName(MBMailingList mailingList) {
196                    return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
197                            StringPool.SLASH).concat(
198                                    String.valueOf(mailingList.getMailingListId()));
199            }
200    
201            protected void scheduleMailingList(MBMailingList mailingList)
202                    throws PortalException {
203    
204                    unscheduleMailingList(mailingList);
205    
206                    String groupName = getSchedulerGroupName(mailingList);
207    
208                    Calendar startDate = CalendarFactoryUtil.getCalendar();
209    
210                    CronText cronText = new CronText(
211                            startDate, CronText.MINUTELY_FREQUENCY,
212                            mailingList.getInReadInterval());
213    
214                    Trigger trigger = new CronTrigger(
215                            groupName, groupName, startDate.getTime(), null,
216                            cronText.toString());
217    
218                    MailingListRequest mailingListRequest = new MailingListRequest();
219    
220                    mailingListRequest.setCompanyId(mailingList.getCompanyId());
221                    mailingListRequest.setUserId(mailingList.getUserId());
222                    mailingListRequest.setGroupId(mailingList.getGroupId());
223                    mailingListRequest.setCategoryId(mailingList.getCategoryId());
224                    mailingListRequest.setInProtocol(mailingList.getInProtocol());
225                    mailingListRequest.setInServerName(mailingList.getInServerName());
226                    mailingListRequest.setInServerPort(mailingList.getInServerPort());
227                    mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
228                    mailingListRequest.setInUserName(mailingList.getInUserName());
229                    mailingListRequest.setInPassword(mailingList.getInPassword());
230    
231                    SchedulerEngineUtil.schedule(
232                            trigger, null, DestinationNames.MESSAGE_BOARDS_MAILING_LIST,
233                            mailingListRequest);
234            }
235    
236            protected void unscheduleMailingList(MBMailingList mailingList)
237                    throws PortalException {
238    
239                    String groupName = getSchedulerGroupName(mailingList);
240    
241                    List<SchedulerRequest> schedulerRequests =
242                            SchedulerEngineUtil.getScheduledJobs(groupName);
243    
244                    for (SchedulerRequest schedulerRequest : schedulerRequests) {
245                            SchedulerEngineUtil.unschedule(schedulerRequest.getTrigger());
246                    }
247            }
248    
249            protected void validate(
250                            String emailAddress, String inServerName, String inUserName,
251                            String outEmailAddress, boolean outCustom, String outServerName,
252                            String outUserName, boolean active)
253                    throws PortalException {
254    
255                    if (!active) {
256                            return;
257                    }
258    
259                    if (!Validator.isEmailAddress(emailAddress)) {
260                            throw new MailingListEmailAddressException();
261                    }
262                    else if (Validator.isNull(inServerName)) {
263                            throw new MailingListInServerNameException();
264                    }
265                    else if (Validator.isNull(inUserName)) {
266                            throw new MailingListInUserNameException();
267                    }
268                    else if (Validator.isNull(outEmailAddress)) {
269                            throw new MailingListOutEmailAddressException();
270                    }
271                    else if (outCustom) {
272                            if (Validator.isNull(outServerName)) {
273                                    throw new MailingListOutServerNameException();
274                            }
275                            else if (Validator.isNull(outUserName)) {
276                                    throw new MailingListOutUserNameException();
277                            }
278                    }
279            }
280    
281    }