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.SchedulerEngineHelperUtil;
020    import com.liferay.portal.kernel.scheduler.StorageType;
021    import com.liferay.portal.kernel.scheduler.TimeUnit;
022    import com.liferay.portal.kernel.scheduler.Trigger;
023    import com.liferay.portal.kernel.scheduler.TriggerFactoryUtil;
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.exception.MailingListEmailAddressException;
030    import com.liferay.portlet.messageboards.exception.MailingListInServerNameException;
031    import com.liferay.portlet.messageboards.exception.MailingListInUserNameException;
032    import com.liferay.portlet.messageboards.exception.MailingListOutEmailAddressException;
033    import com.liferay.portlet.messageboards.exception.MailingListOutServerNameException;
034    import com.liferay.portlet.messageboards.exception.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                    Trigger trigger = TriggerFactoryUtil.createTrigger(
210                            groupName, groupName, startDate.getTime(),
211                            mailingList.getInReadInterval(), TimeUnit.MINUTE);
212    
213                    MailingListRequest mailingListRequest = new MailingListRequest();
214    
215                    mailingListRequest.setCompanyId(mailingList.getCompanyId());
216                    mailingListRequest.setUserId(mailingList.getUserId());
217                    mailingListRequest.setGroupId(mailingList.getGroupId());
218                    mailingListRequest.setCategoryId(mailingList.getCategoryId());
219                    mailingListRequest.setInProtocol(mailingList.getInProtocol());
220                    mailingListRequest.setInServerName(mailingList.getInServerName());
221                    mailingListRequest.setInServerPort(mailingList.getInServerPort());
222                    mailingListRequest.setInUseSSL(mailingList.getInUseSSL());
223                    mailingListRequest.setInUserName(mailingList.getInUserName());
224                    mailingListRequest.setInPassword(mailingList.getInPassword());
225                    mailingListRequest.setAllowAnonymous(mailingList.getAllowAnonymous());
226    
227                    SchedulerEngineHelperUtil.schedule(
228                            trigger, StorageType.PERSISTED, null,
229                            DestinationNames.MESSAGE_BOARDS_MAILING_LIST, mailingListRequest,
230                            0);
231            }
232    
233            protected void unscheduleMailingList(MBMailingList mailingList)
234                    throws PortalException {
235    
236                    String groupName = getSchedulerGroupName(mailingList);
237    
238                    SchedulerEngineHelperUtil.unschedule(groupName, StorageType.PERSISTED);
239            }
240    
241            protected void validate(
242                            String emailAddress, String inServerName, String inUserName,
243                            String outEmailAddress, boolean outCustom, String outServerName,
244                            String outUserName, boolean active)
245                    throws PortalException {
246    
247                    if (!active) {
248                            return;
249                    }
250    
251                    if (!Validator.isEmailAddress(emailAddress)) {
252                            throw new MailingListEmailAddressException(emailAddress);
253                    }
254                    else if (Validator.isNull(inServerName)) {
255                            throw new MailingListInServerNameException(
256                                    "In server name is null");
257                    }
258                    else if (Validator.isNull(inUserName)) {
259                            throw new MailingListInUserNameException("In user name is null");
260                    }
261                    else if (Validator.isNull(outEmailAddress)) {
262                            throw new MailingListOutEmailAddressException(
263                                    "Out email address is null");
264                    }
265                    else if (outCustom) {
266                            if (Validator.isNull(outServerName)) {
267                                    throw new MailingListOutServerNameException(
268                                            "Out server name is null");
269                            }
270                            else if (Validator.isNull(outUserName)) {
271                                    throw new MailingListOutUserNameException(
272                                            "Out user name is null");
273                            }
274                    }
275            }
276    
277    }