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.batch.components;
021    
022    import org.apache.commons.configuration.Configuration;
023    import org.apache.commons.lang.StringUtils;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchExtension;
026    import org.sonar.api.CoreProperties;
027    import org.sonar.api.database.model.Snapshot;
028    
029    import java.text.ParseException;
030    import java.text.SimpleDateFormat;
031    import java.util.Date;
032    
033    public class PastSnapshotFinder implements BatchExtension {
034    
035      /**
036       * IMPORTANT : please update default values in the ruby side too. See app/helpers/FiltersHelper.rb, method period_names()
037       */
038      private PastSnapshotFinderByDays finderByDays;
039      private PastSnapshotFinderByVersion finderByVersion;
040      private PastSnapshotFinderByDate finderByDate;
041      private PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis;
042      private PastSnapshotFinderByPreviousVersion finderByPreviousVersion;
043    
044      public PastSnapshotFinder(PastSnapshotFinderByDays finderByDays, PastSnapshotFinderByVersion finderByVersion,
045          PastSnapshotFinderByDate finderByDate, PastSnapshotFinderByPreviousAnalysis finderByPreviousAnalysis,
046          PastSnapshotFinderByPreviousVersion finderByPreviousVersion) {
047        this.finderByDays = finderByDays;
048        this.finderByVersion = finderByVersion;
049        this.finderByDate = finderByDate;
050        this.finderByPreviousAnalysis = finderByPreviousAnalysis;
051        this.finderByPreviousVersion = finderByPreviousVersion;
052      }
053    
054      public PastSnapshot find(Snapshot projectSnapshot, Configuration conf, int index) {
055        String propertyValue = getPropertyValue(conf, index);
056        PastSnapshot pastSnapshot = find(projectSnapshot, index, propertyValue);
057        if (pastSnapshot == null && StringUtils.isNotBlank(propertyValue)) {
058          LoggerFactory.getLogger(PastSnapshotFinder.class).debug("Property " + CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + " is not valid: " + propertyValue);
059        }
060        return pastSnapshot;
061      }
062    
063      static String getPropertyValue(Configuration conf, int index) {
064        String defaultValue = null;
065        switch (index) {
066          case 1:
067            defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_1;
068            break;
069          case 2:
070            defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_2;
071            break;
072          case 3:
073            defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_3;
074            break;
075          case 4:
076            defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_4;
077            break; // NOSONAR false-positive: constant 4 is the same than 5 (empty string)
078          case 5:
079            defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5;
080            break; // NOSONAR false-positive: constant 5 is the same than 4 (empty string)
081        }
082        return conf.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index, defaultValue);
083      }
084    
085      public PastSnapshot findPreviousAnalysis(Snapshot projectSnapshot) {
086        return finderByPreviousAnalysis.findByPreviousAnalysis(projectSnapshot);
087      }
088    
089      public PastSnapshot find(Snapshot projectSnapshot, int index, String property) {
090        if (StringUtils.isBlank(property)) {
091          return null;
092        }
093    
094        PastSnapshot result = findByDays(projectSnapshot, property);
095        if (result == null) {
096          result = findByDate(projectSnapshot, property);
097          if (result == null) {
098            result = findByPreviousAnalysis(projectSnapshot, property);
099            if (result == null) {
100              result = findByPreviousVersion(projectSnapshot, property);
101              if (result == null) {
102                result = findByVersion(projectSnapshot, property);
103              }
104            }
105          }
106        }
107    
108        if (result != null) {
109          result.setIndex(index);
110        }
111    
112        return result;
113      }
114    
115      private PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot, String property) {
116        PastSnapshot pastSnapshot = null;
117        if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, property)) {
118          pastSnapshot = finderByPreviousAnalysis.findByPreviousAnalysis(projectSnapshot);
119        }
120        return pastSnapshot;
121      }
122    
123      private PastSnapshot findByPreviousVersion(Snapshot projectSnapshot, String property) {
124        PastSnapshot pastSnapshot = null;
125        if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, property)) {
126          pastSnapshot = finderByPreviousVersion.findByPreviousVersion(projectSnapshot);
127        }
128        return pastSnapshot;
129      }
130    
131      private PastSnapshot findByDate(Snapshot projectSnapshot, String property) {
132        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
133        try {
134          Date date = format.parse(property);
135          return finderByDate.findByDate(projectSnapshot, date);
136    
137        } catch (ParseException e) {
138          return null;
139        }
140      }
141    
142      private PastSnapshot findByVersion(Snapshot projectSnapshot, String property) {
143        return finderByVersion.findByVersion(projectSnapshot, property);
144      }
145    
146      private PastSnapshot findByDays(Snapshot projectSnapshot, String property) {
147        try {
148          int days = Integer.parseInt(property);
149          return finderByDays.findFromDays(projectSnapshot, days);
150    
151        } catch (NumberFormatException e) {
152          return null;
153        }
154      }
155    
156    }