001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.rule;
021    
022    import com.google.common.collect.ImmutableSortedMap;
023    import org.apache.commons.lang.time.DateUtils;
024    import org.sonar.api.batch.Decorator;
025    import org.sonar.api.batch.DecoratorContext;
026    import org.sonar.api.batch.DependsUpon;
027    import org.sonar.api.batch.Event;
028    import org.sonar.api.batch.TimeMachine;
029    import org.sonar.api.batch.TimeMachineQuery;
030    import org.sonar.api.measures.CoreMetrics;
031    import org.sonar.api.measures.Measure;
032    import org.sonar.api.measures.Metric;
033    import org.sonar.api.resources.Language;
034    import org.sonar.api.resources.Languages;
035    import org.sonar.api.resources.Project;
036    import org.sonar.api.resources.Qualifiers;
037    import org.sonar.api.resources.Resource;
038    import org.sonar.api.utils.KeyValueFormat;
039    import org.sonar.batch.index.PersistenceManager;
040    import org.sonar.core.UtcDateUtils;
041    
042    import javax.annotation.CheckForNull;
043    
044    import java.util.Date;
045    import java.util.List;
046    import java.util.Map;
047    
048    public class QProfileEventsDecorator implements Decorator {
049    
050      private final TimeMachine timeMachine;
051      private final Languages languages;
052      private final PersistenceManager persistenceManager;
053    
054      public QProfileEventsDecorator(TimeMachine timeMachine, Languages languages, PersistenceManager pm) {
055        this.timeMachine = timeMachine;
056        this.languages = languages;
057        this.persistenceManager = pm;
058      }
059    
060      @DependsUpon
061      public Metric dependsUpon() {
062        return CoreMetrics.QUALITY_PROFILES;
063      }
064    
065      public boolean shouldExecuteOnProject(Project project) {
066        return true;
067      }
068    
069      @Override
070      public void decorate(Resource resource, DecoratorContext context) {
071        if (!Qualifiers.isProject(resource, true)) {
072          return;
073        }
074    
075        // Load previous profiles
076        Measure previousMeasure = getPreviousMeasure(resource, CoreMetrics.QUALITY_PROFILES);
077        if (previousMeasure == null || previousMeasure.getData() == null) {
078          // first analysis -> do not generate events
079          return;
080        }
081        Map<String, QProfile> previousProfiles = UsedQProfiles.fromJson(previousMeasure.getData()).profilesByKey();
082    
083        // Load current profiles
084        Measure currentMeasure = context.getMeasure(CoreMetrics.QUALITY_PROFILES);
085        Map<String, QProfile> currentProfiles = UsedQProfiles.fromJson(currentMeasure.getData()).profilesByKey();
086    
087        detectNewOrUpdatedProfiles(context, previousProfiles, currentProfiles);
088    
089        detectNoMoreUsedProfiles(context, previousProfiles, currentProfiles);
090      }
091    
092      private void detectNoMoreUsedProfiles(DecoratorContext context, Map<String, QProfile> previousProfiles, Map<String, QProfile> currentProfiles) {
093        for (QProfile previousProfile : previousProfiles.values()) {
094          if (!currentProfiles.containsKey(previousProfile.getKey())) {
095            markAsRemoved(context, previousProfile);
096          }
097        }
098      }
099    
100      private void detectNewOrUpdatedProfiles(DecoratorContext context, Map<String, QProfile> previousProfiles, Map<String, QProfile> currentProfiles) {
101        for (QProfile profile : currentProfiles.values()) {
102          QProfile previousProfile = previousProfiles.get(profile.getKey());
103          if (previousProfile != null) {
104            if (profile.getRulesUpdatedAt().after(previousProfile.getRulesUpdatedAt())) {
105              markAsChanged(context, previousProfile, profile);
106            }
107          } else {
108            markAsAdded(context, profile);
109          }
110        }
111      }
112    
113      private void markAsChanged(DecoratorContext context, QProfile previousProfile, QProfile profile) {
114        // DecoratorContext does not allow to set event data, so SonarIndex must be used
115        Event event = new Event();
116        event.setName(String.format("Changes in %s", profileLabel(profile)));
117        event.setCategory(Event.CATEGORY_PROFILE);
118        Date from = previousProfile.getRulesUpdatedAt();
119    
120        String data = KeyValueFormat.format(ImmutableSortedMap.of(
121          "key", profile.getKey(),
122          "from", UtcDateUtils.formatDateTime(fixDate(from)),
123          "to", UtcDateUtils.formatDateTime(fixDate(profile.getRulesUpdatedAt()))));
124        event.setData(data);
125        persistenceManager.saveEvent(context.getResource(), event);
126      }
127    
128      /**
129       * This hack must be done because date precision is millisecond in db/es and date format is select only
130       */
131      private Date fixDate(Date date) {
132        return DateUtils.addSeconds(date, 1);
133      }
134    
135      private void markAsRemoved(DecoratorContext context, QProfile profile) {
136        context.createEvent(String.format("Stop using %s", profileLabel(profile)), null, Event.CATEGORY_PROFILE, null);
137      }
138    
139      private void markAsAdded(DecoratorContext context, QProfile profile) {
140        context.createEvent(String.format("Use %s", profileLabel(profile)), null, Event.CATEGORY_PROFILE, null);
141      }
142    
143      @CheckForNull
144      private Measure getPreviousMeasure(Resource project, Metric metric) {
145        TimeMachineQuery query = new TimeMachineQuery(project)
146          .setOnlyLastAnalysis(true)
147          .setMetrics(metric);
148        List<Measure> measures = timeMachine.getMeasures(query);
149        if (measures.isEmpty()) {
150          return null;
151        }
152        return measures.get(0);
153      }
154    
155      private String profileLabel(QProfile profile) {
156        Language language = languages.get(profile.getLanguage());
157        String languageName = language != null ? language.getName() : profile.getLanguage();
158        return String.format("'%s' (%s)", profile.getName(), languageName);
159      }
160    
161      @Override
162      public String toString() {
163        return getClass().getSimpleName();
164      }
165    }