001    /**
002     * Copyright (c) 2000-2012 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.StorageType;
024    import com.liferay.portal.kernel.scheduler.Trigger;
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    
043    /**
044     * @author Thiago Moreira
045     */
046    public class MBMailingListLocalServiceImpl
047            extends MBMailingListLocalServiceBaseImpl {
048    
049            public MBMailingList addMailingList(
050                            long userId, long groupId, long categoryId, String emailAddress,
051                            String inProtocol, String inServerName, int inServerPort,
052                            boolean inUseSSL, String inUserName, String inPassword,
053                            int inReadInterval, String outEmailAddress, boolean outCustom,
054                            String outServerName, int outServerPort, boolean outUseSSL,
055                            String outUserName, String outPassword, boolean allowAnonymous,
056                            boolean active, ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    // Mailing list
060    
061                    User user = userPersistence.findByPrimaryKey(userId);
062                    Date now = new Date();
063    
064                    validate(
065                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
066                            outServerName, outUserName, active);
067    
068                    long mailingListId = counterLocalService.increment();
069    
070                    MBMailingList mailingList = mbMailingListPersistence.create(
071                            mailingListId);
072    
073                    mailingList.setUuid(serviceContext.getUuid());
074                    mailingList.setGroupId(groupId);
075                    mailingList.setCompanyId(user.getCompanyId());
076                    mailingList.setUserId(user.getUserId());
077                    mailingList.setUserName(user.getFullName());
078                    mailingList.setCreateDate(serviceContext.getCreateDate(now));
079                    mailingList.setModifiedDate(serviceContext.getModifiedDate(now));
080                    mailingList.setCategoryId(categoryId);
081                    mailingList.setEmailAddress(emailAddress);
082                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
083                    mailingList.setInServerName(inServerName);
084                    mailingList.setInServerPort(inServerPort);
085                    mailingList.setInUseSSL(inUseSSL);
086                    mailingList.setInUserName(inUserName);
087                    mailingList.setInPassword(inPassword);
088                    mailingList.setInReadInterval(inReadInterval);
089                    mailingList.setOutEmailAddress(outEmailAddress);
090                    mailingList.setOutCustom(outCustom);
091                    mailingList.setOutServerName(outServerName);
092                    mailingList.setOutServerPort(outServerPort);
093                    mailingList.setOutUseSSL(outUseSSL);
094                    mailingList.setOutUserName(outUserName);
095                    mailingList.setOutPassword(outPassword);
096                    mailingList.setAllowAnonymous(allowAnonymous);
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 allowAnonymous, boolean active,
149                            ServiceContext serviceContext)
150                    throws PortalException, SystemException {
151    
152                    // Mailing list
153    
154                    validate(
155                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
156                            outServerName, outUserName, active);
157    
158                    MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
159                            mailingListId);
160    
161                    mailingList.setModifiedDate(serviceContext.getModifiedDate(null));
162                    mailingList.setEmailAddress(emailAddress);
163                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
164                    mailingList.setInServerName(inServerName);
165                    mailingList.setInServerPort(inServerPort);
166                    mailingList.setInUseSSL(inUseSSL);
167                    mailingList.setInUserName(inUserName);
168                    mailingList.setInPassword(inPassword);
169                    mailingList.setInReadInterval(inReadInterval);
170                    mailingList.setOutEmailAddress(outEmailAddress);
171                    mailingList.setOutCustom(outCustom);
172                    mailingList.setOutServerName(outServerName);
173                    mailingList.setOutServerPort(outServerPort);
174                    mailingList.setOutUseSSL(outUseSSL);
175                    mailingList.setOutUserName(outUserName);
176                    mailingList.setOutPassword(outPassword);
177                    mailingList.setAllowAnonymous(allowAnonymous);
178                    mailingList.setActive(active);
179    
180                    mbMailingListPersistence.update(mailingList, false);
181    
182                    // Scheduler
183    
184                    if (active) {
185                            scheduleMailingList(mailingList);
186                    }
187    
188                    return mailingList;
189            }
190    
191            protected String getSchedulerGroupName(MBMailingList mailingList) {
192                    return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
193                            StringPool.SLASH).concat(
194                                    String.valueOf(mailingList.getMailingListId()));
195            }
196    
197            protected void scheduleMailingList(MBMailingList mailingList)
198                    throws PortalException {
199    
200                    String groupName = getSchedulerGroupName(mailingList);
201    
202                    Calendar startDate = CalendarFactoryUtil.getCalendar();
203    
204                    CronText cronText = new CronText(
205                            startDate, CronText.MINUTELY_FREQUENCY,
206                            mailingList.getInReadInterval());
207    
208                    Trigger trigger = new CronTrigger(
209                            groupName, groupName, startDate.getTime(), null,
210                            cronText.toString());
211    
212                    MailingListRequest mailingListRequest = new MailingListRequest();
213    
214                    mailingListRequest.setCompanyId(mailingList.getCompanyId());
215                    mailingListRequest.setUserId(mailingList.getUserId());
216                    mailingListRequest.setGroupId(mailingList.getGroupId());
217                    mailingListRequest.setCategoryId(mailingList.getCategoryId());
218                    mailingListRequest.setInProtocol(mailingList.getInProtocol());
219                    mailingListRequest.setInServerName(mailingList.getInServerName());
220                    mailingListRequest.setInServerPort(mailingList.getInServerPort());
221                    mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
222                    mailingListRequest.setInUserName(mailingList.getInUserName());
223                    mailingListRequest.setInPassword(mailingList.getInPassword());
224                    mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
225    
226                    SchedulerEngineUtil.schedule(
227                            trigger, StorageType.MEMORY_CLUSTERED, null,
228                            DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
229                            0);
230            }
231    
232            protected void unscheduleMailingList(MBMailingList mailingList)
233                    throws PortalException {
234    
235                    String groupName = getSchedulerGroupName(mailingList);
236    
237                    SchedulerEngineUtil.unschedule(groupName, StorageType.MEMORY_CLUSTERED);
238            }
239    
240            protected void validate(
241                            String emailAddress, String inServerName, String inUserName,
242                            String outEmailAddress, boolean outCustom, String outServerName,
243                            String outUserName, boolean active)
244                    throws PortalException {
245    
246                    if (!active) {
247                            return;
248                    }
249    
250                    if (!Validator.isEmailAddress(emailAddress)) {
251                            throw new MailingListEmailAddressException();
252                    }
253                    else if (Validator.isNull(inServerName)) {
254                            throw new MailingListInServerNameException();
255                    }
256                    else if (Validator.isNull(inUserName)) {
257                            throw new MailingListInUserNameException();
258                    }
259                    else if (Validator.isNull(outEmailAddress)) {
260                            throw new MailingListOutEmailAddressException();
261                    }
262                    else if (outCustom) {
263                            if (Validator.isNull(outServerName)) {
264                                    throw new MailingListOutServerNameException();
265                            }
266                            else if (Validator.isNull(outUserName)) {
267                                    throw new MailingListOutUserNameException();
268                            }
269                    }
270            }
271    
272    }