001    /**
002     * Copyright (c) 2000-present 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.messaging.DestinationNames;
019    import com.liferay.portal.kernel.scheduler.CronText;
020    import com.liferay.portal.kernel.scheduler.CronTrigger;
021    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
022    import com.liferay.portal.kernel.scheduler.StorageType;
023    import com.liferay.portal.kernel.scheduler.Trigger;
024    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.messageboards.MailingListEmailAddressException;
030    import com.liferay.portlet.messageboards.MailingListInServerNameException;
031    import com.liferay.portlet.messageboards.MailingListInUserNameException;
032    import com.liferay.portlet.messageboards.MailingListOutEmailAddressException;
033    import com.liferay.portlet.messageboards.MailingListOutServerNameException;
034    import com.liferay.portlet.messageboards.MailingListOutUserNameException;
035    import com.liferay.portlet.messageboards.messaging.MailingListRequest;
036    import com.liferay.portlet.messageboards.model.MBMailingList;
037    import com.liferay.portlet.messageboards.service.base.MBMailingListLocalServiceBaseImpl;
038    
039    import java.util.Calendar;
040    
041    /**
042     * @author Thiago Moreira
043     */
044    public class MBMailingListLocalServiceImpl
045            extends MBMailingListLocalServiceBaseImpl {
046    
047            @Override
048            public MBMailingList addMailingList(
049                            long userId, long groupId, long categoryId, String emailAddress,
050                            String inProtocol, String inServerName, int inServerPort,
051                            boolean inUseSSL, String inUserName, String inPassword,
052                            int inReadInterval, String outEmailAddress, boolean outCustom,
053                            String outServerName, int outServerPort, boolean outUseSSL,
054                            String outUserName, String outPassword, boolean allowAnonymous,
055                            boolean active, ServiceContext serviceContext)
056                    throws PortalException {
057    
058                    // Mailing list
059    
060                    User user = userPersistence.findByPrimaryKey(userId);
061    
062                    validate(
063                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
064                            outServerName, outUserName, active);
065    
066                    long mailingListId = counterLocalService.increment();
067    
068                    MBMailingList mailingList = mbMailingListPersistence.create(
069                            mailingListId);
070    
071                    mailingList.setUuid(serviceContext.getUuid());
072                    mailingList.setGroupId(groupId);
073                    mailingList.setCompanyId(user.getCompanyId());
074                    mailingList.setUserId(user.getUserId());
075                    mailingList.setUserName(user.getFullName());
076                    mailingList.setCategoryId(categoryId);
077                    mailingList.setEmailAddress(emailAddress);
078                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
079                    mailingList.setInServerName(inServerName);
080                    mailingList.setInServerPort(inServerPort);
081                    mailingList.setInUseSSL(inUseSSL);
082                    mailingList.setInUserName(inUserName);
083                    mailingList.setInPassword(inPassword);
084                    mailingList.setInReadInterval(inReadInterval);
085                    mailingList.setOutEmailAddress(outEmailAddress);
086                    mailingList.setOutCustom(outCustom);
087                    mailingList.setOutServerName(outServerName);
088                    mailingList.setOutServerPort(outServerPort);
089                    mailingList.setOutUseSSL(outUseSSL);
090                    mailingList.setOutUserName(outUserName);
091                    mailingList.setOutPassword(outPassword);
092                    mailingList.setAllowAnonymous(allowAnonymous);
093                    mailingList.setActive(active);
094    
095                    mbMailingListPersistence.update(mailingList);
096    
097                    // Scheduler
098    
099                    if (active) {
100                            scheduleMailingList(mailingList);
101                    }
102    
103                    return mailingList;
104            }
105    
106            @Override
107            public void deleteCategoryMailingList(long groupId, long categoryId)
108                    throws PortalException {
109    
110                    MBMailingList mailingList = mbMailingListPersistence.findByG_C(
111                            groupId, categoryId);
112    
113                    deleteMailingList(mailingList);
114            }
115    
116            @Override
117            public void deleteMailingList(long mailingListId) throws PortalException {
118                    MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
119                            mailingListId);
120    
121                    deleteMailingList(mailingList);
122            }
123    
124            @Override
125            public void deleteMailingList(MBMailingList mailingList)
126                    throws PortalException {
127    
128                    unscheduleMailingList(mailingList);
129    
130                    mbMailingListPersistence.remove(mailingList);
131            }
132    
133            @Override
134            public MBMailingList fetchCategoryMailingList(
135                    long groupId, long categoryId) {
136    
137                    return mbMailingListPersistence.fetchByG_C(groupId, categoryId);
138            }
139    
140            @Override
141            public MBMailingList getCategoryMailingList(long groupId, long categoryId)
142                    throws PortalException {
143    
144                    return mbMailingListPersistence.findByG_C(groupId, categoryId);
145            }
146    
147            @Override
148            public MBMailingList updateMailingList(
149                            long mailingListId, String emailAddress, String inProtocol,
150                            String inServerName, int inServerPort, boolean inUseSSL,
151                            String inUserName, String inPassword, int inReadInterval,
152                            String outEmailAddress, boolean outCustom, String outServerName,
153                            int outServerPort, boolean outUseSSL, String outUserName,
154                            String outPassword, boolean allowAnonymous, boolean active,
155                            ServiceContext serviceContext)
156                    throws PortalException {
157    
158                    // Mailing list
159    
160                    validate(
161                            emailAddress, inServerName, inUserName, outEmailAddress, outCustom,
162                            outServerName, outUserName, active);
163    
164                    MBMailingList mailingList = mbMailingListPersistence.findByPrimaryKey(
165                            mailingListId);
166    
167                    mailingList.setEmailAddress(emailAddress);
168                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
169                    mailingList.setInServerName(inServerName);
170                    mailingList.setInServerPort(inServerPort);
171                    mailingList.setInUseSSL(inUseSSL);
172                    mailingList.setInUserName(inUserName);
173                    mailingList.setInPassword(inPassword);
174                    mailingList.setInReadInterval(inReadInterval);
175                    mailingList.setOutEmailAddress(outEmailAddress);
176                    mailingList.setOutCustom(outCustom);
177                    mailingList.setOutServerName(outServerName);
178                    mailingList.setOutServerPort(outServerPort);
179                    mailingList.setOutUseSSL(outUseSSL);
180                    mailingList.setOutUserName(outUserName);
181                    mailingList.setOutPassword(outPassword);
182                    mailingList.setAllowAnonymous(allowAnonymous);
183                    mailingList.setActive(active);
184    
185                    mbMailingListPersistence.update(mailingList);
186    
187                    // Scheduler
188    
189                    if (active) {
190                            scheduleMailingList(mailingList);
191                    }
192    
193                    return mailingList;
194            }
195    
196            protected String getSchedulerGroupName(MBMailingList mailingList) {
197                    return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
198                            StringPool.SLASH).concat(
199                                    String.valueOf(mailingList.getMailingListId()));
200            }
201    
202            protected void scheduleMailingList(MBMailingList mailingList)
203                    throws PortalException {
204    
205                    String groupName = getSchedulerGroupName(mailingList);
206    
207                    Calendar startDate = CalendarFactoryUtil.getCalendar();
208    
209                    CronText cronText = new CronText(
210                            startDate, CronText.MINUTELY_FREQUENCY,
211                            mailingList.getInReadInterval());
212    
213                    Trigger trigger = new CronTrigger(
214                            groupName, groupName, startDate.getTime(), null,
215                            cronText.toString());
216    
217                    MailingListRequest mailingListRequest = new MailingListRequest();
218    
219                    mailingListRequest.setCompanyId(mailingList.getCompanyId());
220                    mailingListRequest.setUserId(mailingList.getUserId());
221                    mailingListRequest.setGroupId(mailingList.getGroupId());
222                    mailingListRequest.setCategoryId(mailingList.getCategoryId());
223                    mailingListRequest.setInProtocol(mailingList.getInProtocol());
224                    mailingListRequest.setInServerName(mailingList.getInServerName());
225                    mailingListRequest.setInServerPort(mailingList.getInServerPort());
226                    mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
227                    mailingListRequest.setInUserName(mailingList.getInUserName());
228                    mailingListRequest.setInPassword(mailingList.getInPassword());
229                    mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
230    
231                    SchedulerEngineHelperUtil.schedule(
232                            trigger, StorageType.PERSISTED, null,
233                            DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
234                            0);
235            }
236    
237            protected void unscheduleMailingList(MBMailingList mailingList)
238                    throws PortalException {
239    
240                    String groupName = getSchedulerGroupName(mailingList);
241    
242                    SchedulerEngineHelperUtil.unschedule(groupName, StorageType.PERSISTED);
243            }
244    
245            protected void validate(
246                            String emailAddress, String inServerName, String inUserName,
247                            String outEmailAddress, boolean outCustom, String outServerName,
248                            String outUserName, boolean active)
249                    throws PortalException {
250    
251                    if (!active) {
252                            return;
253                    }
254    
255                    if (!Validator.isEmailAddress(emailAddress)) {
256                            throw new MailingListEmailAddressException();
257                    }
258                    else if (Validator.isNull(inServerName)) {
259                            throw new MailingListInServerNameException();
260                    }
261                    else if (Validator.isNull(inUserName)) {
262                            throw new MailingListInUserNameException();
263                    }
264                    else if (Validator.isNull(outEmailAddress)) {
265                            throw new MailingListOutEmailAddressException();
266                    }
267                    else if (outCustom) {
268                            if (Validator.isNull(outServerName)) {
269                                    throw new MailingListOutServerNameException();
270                            }
271                            else if (Validator.isNull(outUserName)) {
272                                    throw new MailingListOutUserNameException();
273                            }
274                    }
275            }
276    
277    }