2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.emailnotifications.reviews;
22 import org.apache.commons.lang.StringUtils;
23 import org.sonar.api.database.model.User;
24 import org.sonar.api.notifications.Notification;
25 import org.sonar.api.platform.EmailSettings;
26 import org.sonar.api.security.UserFinder;
27 import org.sonar.plugins.emailnotifications.api.EmailMessage;
28 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
31 * Creates email message for notification "review-changed".
35 public class ReviewEmailTemplate extends EmailTemplate {
37 private EmailSettings configuration;
38 private UserFinder userFinder;
40 public ReviewEmailTemplate(EmailSettings configuration, UserFinder userFinder) {
41 this.configuration = configuration;
42 this.userFinder = userFinder;
46 public EmailMessage format(Notification notification) {
47 if (!"review-changed".equals(notification.getType())) {
50 String reviewId = notification.getFieldValue("reviewId");
51 String author = notification.getFieldValue("author");
52 StringBuilder sb = new StringBuilder();
54 append(sb, "Project", null, notification.getFieldValue("project"));
55 append(sb, "Resource", null, notification.getFieldValue("resource"));
57 append(sb, null, null, notification.getFieldValue("title"));
59 append(sb, "Status", notification.getFieldValue("old.status"), notification.getFieldValue("new.status"));
60 append(sb, "Resolution", notification.getFieldValue("old.resolution"), notification.getFieldValue("new.resolution"));
61 append(sb, "Assignee", getUserFullName(notification.getFieldValue("old.assignee")), getUserFullName(notification.getFieldValue("new.assignee")));
62 appendComment(sb, notification);
63 appendFooter(sb, notification);
65 EmailMessage message = new EmailMessage()
66 .setMessageId("review/" + reviewId)
67 .setSubject("Review #" + reviewId)
68 .setMessage(sb.toString());
70 message.setFrom(getUserFullName(author));
75 private void append(StringBuilder sb, String name, String oldValue, String newValue) {
76 if (oldValue != null || newValue != null) {
78 sb.append(name).append(": ");
80 if (newValue != null) {
83 if (oldValue != null) {
84 sb.append(" (was ").append(oldValue).append(")");
90 private void appendComment(StringBuilder sb, Notification notification) {
91 String newComment = notification.getFieldValue("new.comment");
92 String oldComment = notification.getFieldValue("old.comment");
94 if (newComment != null) { // comment was added or modified
95 sb.append("Comment:\n ").append(newComment).append('\n');
96 if (oldComment != null) { // comment was modified
97 sb.append("Was:\n ").append(oldComment).append('\n');
99 } else if (oldComment != null) { // comment was deleted
100 sb.append("Comment deleted, was:\n ").append(oldComment).append('\n');
104 private void appendFooter(StringBuilder sb, Notification notification) {
105 String reviewId = notification.getFieldValue("reviewId");
107 .append("See it in Sonar: ").append(configuration.getServerBaseURL()).append("/reviews/view/").append(reviewId).append('\n');
111 * Visibility has been relaxed for tests.
113 String getUserFullName(String login) {
117 User user = userFinder.findByLogin(login);
118 if (user == null) { // most probably user was deleted
121 return StringUtils.defaultIfBlank(user.getName(), login);