001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.notifications;
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.entity.NotificationQueueElement;
028    import org.sonar.jpa.session.DatabaseSessionFactory;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @since 2.10
035     */
036    public class DefaultNotificationManager implements NotificationManager {
037    
038      private DatabaseSessionFactory sessionFactory;
039    
040      public DefaultNotificationManager(DatabaseSessionFactory sessionFactory) {
041        this.sessionFactory = sessionFactory;
042      }
043    
044      public void scheduleForSending(Notification notification) {
045        NotificationQueueElement notificationQueueElement = new NotificationQueueElement();
046        notificationQueueElement.setCreatedAt(new Date());
047        notificationQueueElement.setNotification(notification);
048        DatabaseSession session = sessionFactory.getSession();
049        session.save(notificationQueueElement);
050        session.commit();
051      }
052    
053      public NotificationQueueElement getFromQueue() {
054        DatabaseSession session = sessionFactory.getSession();
055        String hql = "FROM " + NotificationQueueElement.class.getSimpleName() + " ORDER BY createdAt ASC";
056        List<NotificationQueueElement> notifications = session.createQuery(hql).setMaxResults(1).getResultList();
057        if (notifications.isEmpty()) {
058          // UGLY - waiting for a clean way to manage JDBC connections without Hibernate - myBatis is coming soon
059          // This code is highly coupled to org.sonar.server.notifications.NotificationService, which periodically executes
060          // several times the methods getFromQueue() and isEnabled(). The session is closed only at the end of the task -
061          // when there are no more notifications to process - to ensure "better" performances.
062          sessionFactory.clear();
063          return null;
064        }
065        NotificationQueueElement notification = notifications.get(0);
066        session.removeWithoutFlush(notification);
067        session.commit();
068        return notification;
069    
070      }
071    
072      public boolean isEnabled(String username, String channelKey, String dispatcherKey) {
073        DatabaseSession session = sessionFactory.getSession();
074        User user = session.getSingleResult(User.class, "login", username);
075        String notificationKey = "notification." + dispatcherKey + "." + channelKey;
076        Property property = session.getSingleResult(Property.class, "userId", user.getId(), "key", notificationKey);
077        return property != null && "true".equals(property.getValue());
078      }
079    
080    }