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.batch.components;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.lang.StringUtils;
024 import org.sonar.api.BatchExtension;
025 import org.sonar.api.CoreProperties;
026 import org.sonar.api.database.model.Snapshot;
027 import org.sonar.api.utils.Logs;
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
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 Logs.INFO.debug("The property " + CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + " has an unvalid value: " + 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: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_1; break;
064 case 2: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_2; break;
065 case 3: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_3; break;
066 case 4: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_4; break; // NOSONAR false-positive: constant 4 is the same than 5 (empty string)
067 case 5: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5; break; // NOSONAR false-positive: constant 5 is the same than 4 (empty string)
068 }
069 return conf.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index, defaultValue);
070 }
071
072 public PastSnapshot findPreviousAnalysis(Snapshot projectSnapshot) {
073 return finderByPreviousAnalysis.findByPreviousAnalysis(projectSnapshot);
074 }
075
076 public PastSnapshot find(Snapshot projectSnapshot, int index, String property) {
077 if (StringUtils.isBlank(property)) {
078 return null;
079 }
080
081 PastSnapshot result = findByDays(projectSnapshot, property);
082 if (result == null) {
083 result = findByDate(projectSnapshot, property);
084 if (result == null) {
085 result = findByPreviousAnalysis(projectSnapshot, property);
086 if (result == null) {
087 result = findByVersion(projectSnapshot, property);
088 }
089 }
090 }
091
092 if (result != null) {
093 result.setIndex(index);
094 }
095
096 return result;
097 }
098
099 private PastSnapshot findByPreviousAnalysis(Snapshot projectSnapshot, String property) {
100 PastSnapshot pastSnapshot = null;
101 if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, property)) {
102 pastSnapshot = finderByPreviousAnalysis.findByPreviousAnalysis(projectSnapshot);
103 }
104 return pastSnapshot;
105 }
106
107 private PastSnapshot findByDate(Snapshot projectSnapshot, String property) {
108 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
109 try {
110 Date date = format.parse(property);
111 return finderByDate.findByDate(projectSnapshot, date);
112
113 } catch (ParseException e) {
114 return null;
115 }
116 }
117
118 private PastSnapshot findByVersion(Snapshot projectSnapshot, String property) {
119 return finderByVersion.findByVersion(projectSnapshot, property);
120 }
121
122 private PastSnapshot findByDays(Snapshot projectSnapshot, String property) {
123 try {
124 int days = Integer.parseInt(property);
125 return finderByDays.findFromDays(projectSnapshot, days);
126
127 } catch (NumberFormatException e) {
128 return null;
129 }
130 }
131
132 }