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.core.sensors;
021
022 import org.sonar.api.batch.*;
023 import org.sonar.api.measures.CoreMetrics;
024 import org.sonar.api.measures.Measure;
025 import org.sonar.api.measures.Metric;
026 import org.sonar.api.profiles.RulesProfile;
027 import org.sonar.api.resources.Project;
028 import org.sonar.api.resources.Resource;
029 import org.sonar.api.resources.ResourceUtils;
030
031 import java.util.List;
032
033 public class GenerateAlertEvents implements Decorator {
034
035 private final RulesProfile profile;
036 private final TimeMachine timeMachine;
037
038 public GenerateAlertEvents(RulesProfile profile, TimeMachine timeMachine) {
039 this.profile = profile;
040 this.timeMachine = timeMachine;
041 }
042
043 public boolean shouldExecuteOnProject(Project project) {
044 return profile != null && profile.getAlerts() != null && profile.getAlerts().size() > 0;
045 }
046
047
048 @DependsUpon
049 public Metric dependsUponAlertStatus() {
050 return CoreMetrics.ALERT_STATUS;
051 }
052
053 public void decorate(Resource resource, DecoratorContext context) {
054 if (!shouldDecorateResource(resource)) {
055 return;
056 }
057 Measure currentStatus = context.getMeasure(CoreMetrics.ALERT_STATUS);
058 if (currentStatus == null) {
059 return;
060 }
061
062 TimeMachineQuery query = new TimeMachineQuery(resource).setOnlyLastAnalysis(true).setMetrics(CoreMetrics.ALERT_STATUS);
063 List<Measure> measures = timeMachine.getMeasures(query);
064
065 Measure pastStatus = (measures != null && measures.size() == 1 ? measures.get(0) : null);
066 if (pastStatus != null && pastStatus.getDataAsLevel() != currentStatus.getDataAsLevel()) {
067 createEvent(context, getName(pastStatus, currentStatus), currentStatus.getAlertText());
068
069 } else if (pastStatus == null && currentStatus.getDataAsLevel() != Metric.Level.OK) {
070 createEvent(context, getName(currentStatus), currentStatus.getAlertText());
071 }
072
073 }
074
075 private boolean shouldDecorateResource(Resource resource) {
076 return ResourceUtils.isRootProject(resource);
077 }
078
079 private String getName(Measure pastStatus, Measure currentStatus) {
080 return getName(currentStatus) + " (was " + getName(pastStatus) + ")";
081
082 }
083
084 private String getName(Measure currentStatus) {
085 return currentStatus.getDataAsLevel().getColorName();
086 }
087
088 private void createEvent(DecoratorContext context, String name, String description) {
089 context.createEvent(name, description, Event.CATEGORY_ALERT, null);
090 }
091 }