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