001    /**
002     * Copyright (c) 2000-2013 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.SchedulerEngineHelperUtil;
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            @Override
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 allowAnonymous,
057                            boolean active, 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.setAllowAnonymous(allowAnonymous);
098                    mailingList.setActive(active);
099    
100                    mbMailingListPersistence.update(mailingList, false);
101    
102                    // Scheduler
103    
104                    if (active) {
105                            scheduleMailingList(mailingList);
106                    }
107    
108                    return mailingList;
109            }
110    
111            @Override
112            public void deleteCategoryMailingList(long groupId, long categoryId)
113                    throws PortalException, SystemException {
114    
115                    MBMailingList mailingList = mbMailingListPersistence.findByG_C(
116                            groupId, categoryId);
117    
118                    deleteMailingList(mailingList);
119            }
120    
121            @Override
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            @Override
132            public void deleteMailingList(MBMailingList mailingList)
133                    throws PortalException, SystemException {
134    
135                    unscheduleMailingList(mailingList);
136    
137                    mbMailingListPersistence.remove(mailingList);
138            }
139    
140            @Override
141            public MBMailingList getCategoryMailingList(long groupId, long categoryId)
142                    throws PortalException, SystemException {
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, SystemException {
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.setModifiedDate(serviceContext.getModifiedDate(null));
168                    mailingList.setEmailAddress(emailAddress);
169                    mailingList.setInProtocol(inUseSSL ? inProtocol + "s" : inProtocol);
170                    mailingList.setInServerName(inServerName);
171                    mailingList.setInServerPort(inServerPort);
172                    mailingList.setInUseSSL(inUseSSL);
173                    mailingList.setInUserName(inUserName);
174                    mailingList.setInPassword(inPassword);
175                    mailingList.setInReadInterval(inReadInterval);
176                    mailingList.setOutEmailAddress(outEmailAddress);
177                    mailingList.setOutCustom(outCustom);
178                    mailingList.setOutServerName(outServerName);
179                    mailingList.setOutServerPort(outServerPort);
180                    mailingList.setOutUseSSL(outUseSSL);
181                    mailingList.setOutUserName(outUserName);
182                    mailingList.setOutPassword(outPassword);
183                    mailingList.setAllowAnonymous(allowAnonymous);
184                    mailingList.setActive(active);
185    
186                    mbMailingListPersistence.update(mailingList, false);
187    
188                    // Scheduler
189    
190                    if (active) {
191                            scheduleMailingList(mailingList);
192                    }
193    
194                    return mailingList;
195            }
196    
197            protected String getSchedulerGroupName(MBMailingList mailingList) {
198                    return DestinationNames.MESSAGE_BOARDS_MAILING_LIST.concat(
199                            StringPool.SLASH).concat(
200                                    String.valueOf(mailingList.getMailingListId()));
201            }
202    
203            protected void scheduleMailingList(MBMailingList mailingList)
204                    throws PortalException {
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                    mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
231    
232                    SchedulerEngineHelperUtil.schedule(
233                            trigger, StorageType.PERSISTED, null,
234                            DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
235                            0);
236            }
237    
238            protected void unscheduleMailingList(MBMailingList mailingList)
239                    throws PortalException {
240    
241                    String groupName = getSchedulerGroupName(mailingList);
242    
243                    SchedulerEngineHelperUtil.unschedule(groupName, StorageType.PERSISTED);
244            }
245    
246            protected void validate(
247                            String emailAddress, String inServerName, String inUserName,
248                            String outEmailAddress, boolean outCustom, String outServerName,
249                            String outUserName, boolean active)
250                    throws PortalException {
251    
252                    if (!active) {
253                            return;
254                    }
255    
256                    if (!Validator.isEmailAddress(emailAddress)) {
257                            throw new MailingListEmailAddressException();
258                    }
259                    else if (Validator.isNull(inServerName)) {
260                            throw new MailingListInServerNameException();
261                    }
262                    else if (Validator.isNull(inUserName)) {
263                            throw new MailingListInUserNameException();
264                    }
265                    else if (Validator.isNull(outEmailAddress)) {
266                            throw new MailingListOutEmailAddressException();
267                    }
268                    else if (outCustom) {
269                            if (Validator.isNull(outServerName)) {
270                                    throw new MailingListOutServerNameException();
271                            }
272                            else if (Validator.isNull(outUserName)) {
273                                    throw new MailingListOutUserNameException();
274                            }
275                    }
276            }
277    
278    }