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    
021    package org.sonar.batch.debt;
022    
023    import org.sonar.api.BatchComponent;
024    import org.sonar.api.CoreProperties;
025    import org.sonar.api.config.Settings;
026    import org.sonar.api.measures.Metric;
027    
028    public class SqaleRatingSettings implements BatchComponent {
029    
030      private final Settings settings;
031    
032      public SqaleRatingSettings(Settings settings) {
033        this.settings = settings;
034      }
035    
036      public double[] getRatingGrid() {
037        try {
038          String[] ratingGrades = settings.getStringArray(CoreProperties.RATING_GRID);
039          double[] grid = new double[4];
040          for (int i = 0; i < 4; i++) {
041            grid[i] = Double.parseDouble(ratingGrades[i]);
042          }
043          return grid;
044        } catch (Exception e) {
045          throw new IllegalArgumentException("The SQALE rating grid is incorrect. Expected something similar to '"
046            + CoreProperties.RATING_GRID_DEF_VALUES + "' and got '"
047            + settings.getString(CoreProperties.RATING_GRID) + "'", e);
048        }
049      }
050    
051      public long getDevCost(String languageKey) {
052        try {
053          LanguageSpecificConfiguration languageSpecificConfig = getSpecificParametersForLanguage(languageKey);
054          if (languageSpecificConfig != null && languageSpecificConfig.getManDays() != null) {
055            return Long.parseLong(languageSpecificConfig.getManDays());
056          }
057          return Long.parseLong(settings.getString(CoreProperties.DEVELOPMENT_COST));
058        } catch (Exception e) {
059          throw new IllegalArgumentException("The value of the SQALE property '" + CoreProperties.DEVELOPMENT_COST
060            + "' is incorrect. Expected long but got '" + settings.getString(CoreProperties.DEVELOPMENT_COST) + "'", e);
061        }
062      }
063    
064      public Metric getSizeMetric(String languageKey, Metric[] metrics) {
065        LanguageSpecificConfiguration languageSpecificConfig = getSpecificParametersForLanguage(languageKey);
066        if (languageSpecificConfig != null && languageSpecificConfig.getMetric() != null) {
067          return getMetricForKey(languageSpecificConfig.getMetric(), metrics);
068        }
069        return getMetricForKey(settings.getString(CoreProperties.SIZE_METRIC), metrics);
070      }
071    
072      private Metric getMetricForKey(String sizeMetricKey, Metric[] metrics) {
073        for (Metric metric : metrics) {
074          if (metric.getKey().equals(sizeMetricKey)) {
075            return metric;
076          }
077        }
078        throw new IllegalArgumentException("The metric key used to define the SQALE size metric is unknown : '" + sizeMetricKey + "'");
079      }
080    
081      private LanguageSpecificConfiguration getSpecificParametersForLanguage(String languageKey) {
082        String[] languageConfigIndexes = settings.getStringArray(CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS);
083        for (String languageConfigIndex : languageConfigIndexes) {
084          String languagePropertyKey = CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + languageConfigIndex + "." + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY;
085          if (languageKey.equals(settings.getString(languagePropertyKey))) {
086            return LanguageSpecificConfiguration.create(settings, languageConfigIndex);
087          }
088        }
089        return null;
090      }
091    
092      private static class LanguageSpecificConfiguration {
093    
094        private final String language;
095        private final String manDays;
096        private final String metric;
097    
098        private LanguageSpecificConfiguration(String language, String manDays, String metric) {
099          this.language = language;
100          this.manDays = manDays;
101          this.metric = metric;
102        }
103    
104        static LanguageSpecificConfiguration create(Settings settings, String configurationId) {
105    
106          String configurationPrefix = CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS + "." + configurationId + ".";
107    
108          String language = settings.getString(configurationPrefix + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY);
109          String manDays = settings.getString(configurationPrefix + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY);
110          String metric = settings.getString(configurationPrefix + CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY);
111    
112          return new LanguageSpecificConfiguration(language, manDays, metric);
113        }
114    
115        String getLanguage() {
116          return language;
117        }
118    
119        String getManDays() {
120          return manDays;
121        }
122    
123        String getMetric() {
124          return metric;
125        }
126      }
127    }