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 */
020package org.sonar.plugins.core.sensors;
021
022import org.sonar.api.BatchExtension;
023import org.sonar.api.database.model.User;
024import org.sonar.api.notifications.Notification;
025import org.sonar.api.notifications.NotificationManager;
026import org.sonar.api.resources.Project;
027import org.sonar.api.resources.Resource;
028import org.sonar.api.security.UserFinder;
029import org.sonar.core.review.ReviewDto;
030
031import javax.annotation.Nullable;
032
033public class ReviewNotifications implements BatchExtension {
034  private NotificationManager notificationManager;
035  private UserFinder userFinder;
036
037  public ReviewNotifications(NotificationManager notificationManager, UserFinder userFinder) {
038    this.notificationManager = notificationManager;
039    this.userFinder = userFinder;
040  }
041
042  void notifyReopened(ReviewDto review, Project project, Resource resource) {
043    Notification notification = createReviewNotification(review, project, resource)
044        .setFieldValue("old.status", review.getStatus())
045        .setFieldValue("new.status", ReviewDto.STATUS_REOPENED)
046        .setFieldValue("old.resolution", review.getResolution())
047        .setFieldValue("new.resolution", null);
048    notificationManager.scheduleForSending(notification);
049  }
050
051  void notifyClosed(ReviewDto review, Project project, @Nullable Resource resource) {
052    Notification notification = createReviewNotification(review, project, resource)
053        .setFieldValue("old.status", review.getStatus())
054        .setFieldValue("new.status", ReviewDto.STATUS_CLOSED);
055    notificationManager.scheduleForSending(notification);
056  }
057
058  private Notification createReviewNotification(ReviewDto review, Project project, @Nullable Resource resource) {
059    return new Notification("review-changed")
060        .setFieldValue("reviewId", String.valueOf(review.getId()))
061        .setFieldValue("project", project.getRoot().getLongName())
062        .setFieldValue("resource", resource != null ? resource.getLongName() : null)
063        .setFieldValue("title", review.getTitle())
064        .setFieldValue("creator", getCreator(review))
065        .setFieldValue("assignee", getAssignee(review));
066  }
067
068  private String getCreator(ReviewDto review) {
069    if (review.getUserId() == null) { // no creator and in fact this should never happen in real-life, however happens during unit tests
070      return null;
071    }
072    User user = userFinder.findById(review.getUserId());
073    return user != null ? user.getLogin() : null;
074  }
075
076  private String getAssignee(ReviewDto review) {
077    if (review.getAssigneeId() == null) { // not assigned
078      return null;
079    }
080    User user = userFinder.findById(review.getAssigneeId().intValue());
081    return user != null ? user.getLogin() : null;
082  }
083}