001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.core.notification;
021
022 import org.sonar.api.database.DatabaseSession;
023 import org.sonar.api.database.configuration.Property;
024 import org.sonar.api.database.model.User;
025 import org.sonar.api.notifications.Notification;
026 import org.sonar.api.notifications.NotificationManager;
027 import org.sonar.jpa.session.DatabaseSessionFactory;
028
029 import java.util.Date;
030 import java.util.List;
031
032 /**
033 * @since 2.10
034 */
035 public class DefaultNotificationManager implements NotificationManager {
036
037 private DatabaseSessionFactory sessionFactory;
038
039 public DefaultNotificationManager(DatabaseSessionFactory sessionFactory) {
040 this.sessionFactory = sessionFactory;
041 }
042
043 public void scheduleForSending(Notification notification) {
044 NotificationQueueElement notificationQueueElement = new NotificationQueueElement();
045 notificationQueueElement.setCreatedAt(new Date());
046 notificationQueueElement.setNotification(notification);
047 DatabaseSession session = sessionFactory.getSession();
048 session.save(notificationQueueElement);
049 session.commit();
050 }
051
052 public NotificationQueueElement getFromQueue() {
053 DatabaseSession session = sessionFactory.getSession();
054 String hql = "FROM " + NotificationQueueElement.class.getSimpleName() + " ORDER BY createdAt ASC";
055 List<NotificationQueueElement> notifications = session.createQuery(hql).setMaxResults(1).getResultList();
056 if (notifications.isEmpty()) {
057 // UGLY - waiting for a clean way to manage JDBC connections without Hibernate - myBatis is coming soon
058 // This code is highly coupled to org.sonar.server.notifications.NotificationService, which periodically executes
059 // several times the methods getFromQueue() and isEnabled(). The session is closed only at the end of the task -
060 // when there are no more notifications to process - to ensure "better" performances.
061 sessionFactory.clear();
062 return null;
063 }
064 NotificationQueueElement notification = notifications.get(0);
065 session.removeWithoutFlush(notification);
066 session.commit();
067 return notification;
068
069 }
070
071 public boolean isEnabled(String username, String channelKey, String dispatcherKey) {
072 DatabaseSession session = sessionFactory.getSession();
073 User user = session.getSingleResult(User.class, "login", username);
074 String notificationKey = "notification." + dispatcherKey + "." + channelKey;
075 Property property = session.getSingleResult(Property.class, "userId", user.getId(), "key", notificationKey);
076 return property != null && "true".equals(property.getValue());
077 }
078
079 }