001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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    
029    import java.util.List;
030    
031    public class ProfileEventsSensor implements Sensor {
032    
033      private final RulesProfile profile;
034      private final TimeMachine timeMachine;
035    
036      public ProfileEventsSensor(RulesProfile profile, TimeMachine timeMachine) {
037        this.profile = profile;
038        this.timeMachine = timeMachine;
039      }
040    
041      public boolean shouldExecuteOnProject(Project project) {
042        return true;
043      }
044    
045      public void analyse(Project project, SensorContext context) {
046        if (profile == null) {
047          return;
048        }
049    
050        Measure pastProfileMeasure = getPreviousMeasure(project, CoreMetrics.PROFILE);
051        if (pastProfileMeasure == null) {
052          return; // first analysis
053        }
054        int pastProfileId = pastProfileMeasure.getIntValue();
055        Measure pastProfileVersionMeasure = getPreviousMeasure(project, CoreMetrics.PROFILE_VERSION);
056        final int pastProfileVersion;
057        if (pastProfileVersionMeasure == null) { // first analysis with versions
058          pastProfileVersion = 1;
059        } else {
060          pastProfileVersion = pastProfileVersionMeasure.getIntValue();
061        }
062        String pastProfile = formatProfileDescription(pastProfileMeasure.getData(), pastProfileVersion);
063    
064        int currentProfileId = profile.getId();
065        int currentProfileVersion = profile.getVersion();
066        String currentProfile = formatProfileDescription(profile.getName(), currentProfileVersion);
067    
068        if ((pastProfileId != currentProfileId) || (pastProfileVersion != currentProfileVersion)) {
069          // A different profile is used for this project or new version of same profile
070          context.createEvent(project, currentProfile, currentProfile + " is used instead of " + pastProfile, Event.CATEGORY_PROFILE, null);
071        }
072      }
073    
074      private static String formatProfileDescription(String name, int version) {
075        return name + " version " + version;
076      }
077    
078      private Measure getPreviousMeasure(Project project, Metric metric) {
079        TimeMachineQuery query = new TimeMachineQuery(project)
080            .setOnlyLastAnalysis(true)
081            .setMetrics(metric);
082        List<Measure> measures = timeMachine.getMeasures(query);
083        if (measures.isEmpty()) {
084          return null;
085        }
086        return measures.get(0);
087      }
088    
089      @Override
090      public String toString() {
091        return getClass().getSimpleName();
092      }
093    }