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.plugins.emailnotifications.newviolations;
021
022 import org.sonar.api.notifications.Notification;
023 import org.sonar.plugins.emailnotifications.EmailConfiguration;
024 import org.sonar.plugins.emailnotifications.api.EmailMessage;
025 import org.sonar.plugins.emailnotifications.api.EmailTemplate;
026
027 /**
028 * Creates email message for notification "new-violations".
029 *
030 * @since 2.10
031 */
032 public class NewViolationsEmailTemplate extends EmailTemplate {
033
034 private EmailConfiguration configuration;
035
036 public NewViolationsEmailTemplate(EmailConfiguration configuration) {
037 this.configuration = configuration;
038 }
039
040 @Override
041 public EmailMessage format(Notification notification) {
042 if (!"new-violations".equals(notification.getType())) {
043 return null;
044 }
045 StringBuilder sb = new StringBuilder();
046
047 String projectName = notification.getFieldValue("projectName");
048 String violationsCount = notification.getFieldValue("count");
049 String fromDate = notification.getFieldValue("fromDate");
050
051 sb.append("Project: ").append(projectName).append('\n');
052 sb.append(violationsCount).append(" new violations introduced since ").append(fromDate).append('\n');
053 appendFooter(sb, notification);
054
055 EmailMessage message = new EmailMessage()
056 .setMessageId("new-violations/" + notification.getFieldValue("projectId"))
057 .setSubject("New violations for project " + projectName)
058 .setMessage(sb.toString());
059
060 return message;
061 }
062
063 private void appendFooter(StringBuilder sb, Notification notification) {
064 String projectKey = notification.getFieldValue("projectKey");
065 sb.append("\n")
066 .append("See it in Sonar: ").append(configuration.getServerBaseURL()).append("/drilldown/measures/").append(projectKey)
067 .append("?metric=new_violations&period=1\n");
068 }
069
070 }