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.emailnotifications.reviews;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.database.model.User;
024import org.sonar.api.notifications.Notification;
025import org.sonar.api.security.UserFinder;
026import org.sonar.plugins.emailnotifications.EmailConfiguration;
027import org.sonar.plugins.emailnotifications.api.EmailMessage;
028import org.sonar.plugins.emailnotifications.api.EmailTemplate;
029
030/**
031 * Creates email message for notification "review-changed".
032 * 
033 * @since 2.10
034 */
035public class ReviewEmailTemplate extends EmailTemplate {
036
037  private EmailConfiguration configuration;
038  private UserFinder userFinder;
039
040  public ReviewEmailTemplate(EmailConfiguration configuration, UserFinder userFinder) {
041    this.configuration = configuration;
042    this.userFinder = userFinder;
043  }
044
045  @Override
046  public EmailMessage format(Notification notification) {
047    if (!"review-changed".equals(notification.getType())) {
048      return null;
049    }
050    String reviewId = notification.getFieldValue("reviewId");
051    String author = notification.getFieldValue("author");
052    StringBuilder sb = new StringBuilder();
053
054    append(sb, "Project", null, notification.getFieldValue("project"));
055    append(sb, "Resource", null, notification.getFieldValue("resource"));
056    sb.append('\n');
057    append(sb, null, null, notification.getFieldValue("title"));
058    sb.append('\n');
059    append(sb, "Status", notification.getFieldValue("old.status"), notification.getFieldValue("new.status"));
060    append(sb, "Resolution", notification.getFieldValue("old.resolution"), notification.getFieldValue("new.resolution"));
061    append(sb, "Assignee", getUserFullName(notification.getFieldValue("old.assignee")), getUserFullName(notification.getFieldValue("new.assignee")));
062    appendComment(sb, notification);
063    appendFooter(sb, notification);
064
065    EmailMessage message = new EmailMessage()
066        .setMessageId("review/" + reviewId)
067        .setSubject("Review #" + reviewId)
068        .setMessage(sb.toString());
069    if (author != null) {
070      message.setFrom(getUserFullName(author));
071    }
072    return message;
073  }
074
075  private void append(StringBuilder sb, String name, String oldValue, String newValue) {
076    if (oldValue != null || newValue != null) {
077      if (name != null) {
078        sb.append(name).append(": ");
079      }
080      if (newValue != null) {
081        sb.append(newValue);
082      }
083      if (oldValue != null) {
084        sb.append(" (was ").append(oldValue).append(")");
085      }
086      sb.append('\n');
087    }
088  }
089
090  private void appendComment(StringBuilder sb, Notification notification) {
091    String newComment = notification.getFieldValue("new.comment");
092    String oldComment = notification.getFieldValue("old.comment");
093
094    if (newComment != null) { // comment was added or modified
095      sb.append("Comment:\n  ").append(newComment).append('\n');
096      if (oldComment != null) { // comment was modified
097        sb.append("Was:\n  ").append(oldComment).append('\n');
098      }
099    } else if (oldComment != null) { // comment was deleted
100      sb.append("Comment deleted, was:\n  ").append(oldComment).append('\n');
101    }
102  }
103
104  private void appendFooter(StringBuilder sb, Notification notification) {
105    String reviewId = notification.getFieldValue("reviewId");
106    sb.append("\n")
107        .append("See it in Sonar: ").append(configuration.getServerBaseURL()).append("/reviews/view/").append(reviewId).append('\n');
108  }
109
110  /**
111   * Visibility has been relaxed for tests.
112   */
113  String getUserFullName(String login) {
114    if (login == null) {
115      return null;
116    }
117    User user = userFinder.findByLogin(login);
118    if (user == null) { // most probably user was deleted
119      return login;
120    }
121    return StringUtils.defaultIfBlank(user.getName(), login);
122  }
123
124}