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.plugins.core.sensors;
021    
022    import org.apache.commons.lang.NotImplementedException;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.api.measures.Measure;
025    import org.sonar.api.measures.Metric;
026    import org.sonar.api.profiles.Alert;
027    
028    public final class AlertUtils {
029      private AlertUtils() {
030      }
031    
032      /**
033       * Get the matching alert level for the given measure
034       */
035      public static Metric.Level getLevel(Alert alert, Measure measure) {
036        if (evaluateAlert(alert, measure, Metric.Level.ERROR)) {
037          return Metric.Level.ERROR;
038        }
039        if (evaluateAlert(alert, measure, Metric.Level.WARN)) {
040          return Metric.Level.WARN;
041        }
042        return Metric.Level.OK;
043      }
044    
045      private static boolean evaluateAlert(Alert alert, Measure measure, Metric.Level alertLevel) {
046        String valueToEval;
047        if (alertLevel.equals(Metric.Level.ERROR)) {
048          valueToEval = alert.getValueError();
049    
050        } else if (alertLevel.equals(Metric.Level.WARN)) {
051          valueToEval = alert.getValueWarning();
052        } else {
053          throw new IllegalStateException(alertLevel.toString());
054        }
055        if (StringUtils.isEmpty(valueToEval)) {
056          return false;
057        }
058    
059        Comparable criteriaValue = getValueForComparison(alert.getMetric(), valueToEval);
060        Comparable metricValue = getMeasureValue(alert.getMetric(), measure);
061    
062        int comparison = metricValue.compareTo(criteriaValue);
063        if (alert.isNotEqualsOperator() && comparison == 0 ||
064            alert.isGreaterOperator() && comparison != 1 ||
065            alert.isSmallerOperator() && comparison != -1 ||
066            alert.isEqualsOperator() && comparison != 0) {
067          return false;
068        }
069        return true;
070      }
071    
072    
073      private static Comparable<?> getValueForComparison(Metric metric, String value) {
074        if (metric.getType() == Metric.ValueType.FLOAT ||
075            metric.getType() == Metric.ValueType.PERCENT) {
076          return Double.parseDouble(value);
077        }
078        if (metric.getType() == Metric.ValueType.INT ||
079            metric.getType() == Metric.ValueType.MILLISEC) {
080          return value.contains(".") ? Integer.parseInt(value.substring(0, value.indexOf('.'))) : Integer.parseInt(value);
081        }
082        if (metric.getType() == Metric.ValueType.STRING ||
083            metric.getType() == Metric.ValueType.LEVEL) {
084          return value;
085        }
086        if (metric.getType() == Metric.ValueType.BOOL) {
087          return Boolean.valueOf(value);
088        }
089        if (metric.getType() == Metric.ValueType.RATING) {
090          return Double.parseDouble(value);
091        }
092        throw new NotImplementedException(metric.getType().toString());
093      }
094    
095      private static Comparable<?> getMeasureValue(Metric metric, Measure measure) {
096        if (metric.getType() == Metric.ValueType.FLOAT ||
097            metric.getType() == Metric.ValueType.PERCENT) {
098          return measure.getValue();
099        }
100        if (metric.getType() == Metric.ValueType.INT ||
101            metric.getType() == Metric.ValueType.MILLISEC) {
102          return measure.getValue().intValue();
103        }
104        if (metric.getType() == Metric.ValueType.STRING ||
105            metric.getType() == Metric.ValueType.LEVEL) {
106          return measure.getData();
107        }
108        if (metric.getType() == Metric.ValueType.BOOL) {
109          return measure.getValue() == 0d ? Boolean.FALSE : Boolean.TRUE;
110        }
111        if (metric.getType() == Metric.ValueType.RATING) {
112          return measure.getValue();
113        }
114        throw new NotImplementedException(metric.getType().toString());
115      }
116    }