3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.qualitymodel;
22 import com.google.common.collect.ImmutableMap;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import javax.annotation.concurrent.Immutable;
27 import org.sonar.api.config.Settings;
28 import org.sonar.api.utils.MessageException;
30 import static java.lang.String.format;
31 import static org.sonar.api.CoreProperties.DEVELOPMENT_COST;
32 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS;
33 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY;
34 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY;
35 import static org.sonar.api.CoreProperties.LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY;
36 import static org.sonar.api.CoreProperties.RATING_GRID;
37 import static org.sonar.api.CoreProperties.RATING_GRID_DEF_VALUES;
39 public class RatingSettings {
41 private final Settings settings;
42 private final Map<String, LanguageSpecificConfiguration> languageSpecificConfigurationByLanguageKey;
44 public RatingSettings(Settings settings) {
45 this.settings = settings;
46 this.languageSpecificConfigurationByLanguageKey = buildLanguageSpecificConfigurationByLanguageKey(settings);
49 private static Map<String, LanguageSpecificConfiguration> buildLanguageSpecificConfigurationByLanguageKey(Settings settings) {
50 ImmutableMap.Builder<String, LanguageSpecificConfiguration> builder = ImmutableMap.builder();
51 String[] languageConfigIndexes = settings.getStringArray(LANGUAGE_SPECIFIC_PARAMETERS);
52 for (String languageConfigIndex : languageConfigIndexes) {
53 String languagePropertyKey = LANGUAGE_SPECIFIC_PARAMETERS + "." + languageConfigIndex + "." + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY;
54 String languageKey = settings.getString(languagePropertyKey);
55 if (languageKey == null) {
56 throw MessageException.of("Technical debt configuration is corrupted. At least one language specific parameter has no Language key. " +
57 "Contact your administrator to update this configuration in the global administration section of SonarQube.");
59 builder.put(languageKey, LanguageSpecificConfiguration.create(settings, languageConfigIndex));
61 return builder.build();
64 public RatingGrid getRatingGrid() {
66 String[] ratingGrades = settings.getStringArray(RATING_GRID);
67 double[] grid = new double[4];
68 for (int i = 0; i < 4; i++) {
69 grid[i] = Double.parseDouble(ratingGrades[i]);
71 return new RatingGrid(grid);
72 } catch (Exception e) {
73 throw new IllegalArgumentException("The rating grid is incorrect. Expected something similar to '"
74 + RATING_GRID_DEF_VALUES + "' and got '"
75 + settings.getString(RATING_GRID) + "'", e);
79 public long getDevCost(@Nullable String languageKey) {
80 if (languageKey != null) {
82 LanguageSpecificConfiguration languageSpecificConfig = getSpecificParametersForLanguage(languageKey);
83 if (languageSpecificConfig != null && languageSpecificConfig.getManDays() != null) {
84 return Long.parseLong(languageSpecificConfig.getManDays());
86 } catch (NumberFormatException e) {
87 throw new IllegalArgumentException(format("The manDays for language %s is not a valid long number", languageKey), e);
91 return getDefaultDevelopmentCost();
94 private long getDefaultDevelopmentCost() {
96 return Long.parseLong(settings.getString(DEVELOPMENT_COST));
97 } catch (NumberFormatException e) {
98 throw new IllegalArgumentException("The value of the development cost property '" + DEVELOPMENT_COST
99 + "' is incorrect. Expected long but got '" + settings.getString(DEVELOPMENT_COST) + "'", e);
104 private LanguageSpecificConfiguration getSpecificParametersForLanguage(String languageKey) {
105 return languageSpecificConfigurationByLanguageKey.get(languageKey);
109 private static class LanguageSpecificConfiguration {
110 private final String language;
111 private final String manDays;
112 private final String metricKey;
114 private LanguageSpecificConfiguration(String language, String manDays, String metricKey) {
115 this.language = language;
116 this.manDays = manDays;
117 this.metricKey = metricKey;
120 static LanguageSpecificConfiguration create(Settings settings, String configurationId) {
122 String configurationPrefix = LANGUAGE_SPECIFIC_PARAMETERS + "." + configurationId + ".";
124 String language = settings.getString(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_LANGUAGE_KEY);
125 String manDays = settings.getString(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_MAN_DAYS_KEY);
126 String metric = settings.getString(configurationPrefix + LANGUAGE_SPECIFIC_PARAMETERS_SIZE_METRIC_KEY);
128 return new LanguageSpecificConfiguration(language, manDays, metric);
131 String getLanguage() {
135 String getManDays() {
139 String getMetricKey() {