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.api.web.gwt.client.webservices;
021    
022    import com.google.gwt.core.client.JavaScriptObject;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    /**
028     * @deprecated since 2.8. Use sonar-gwt-api instead.
029     */
030    @Deprecated
031    public final class WSMetrics {
032    
033      private WSMetrics() {
034      }
035    
036      private final static List<Metric> DICTIONNARY = new ArrayList<Metric>();
037    
038      public static final Metric NCLOC = add(new Metric("ncloc"));
039      public static final Metric LINES = add(new Metric("lines"));
040      public static final Metric CLASSES = add(new Metric("classes"));
041      public static final Metric PACKAGES = add(new Metric("packages"));
042      public static final Metric FUNCTIONS = add(new Metric("functions"));
043      public static final Metric ACCESSORS = add(new Metric("accessors"));
044      public static final Metric FILES = add(new Metric("files"));
045      public static final Metric DIRECTORIES = add(new Metric("directories"));
046      public static final Metric PUBLIC_API = add(new Metric("public_api"));
047    
048      /* complexity */
049      public static final Metric COMPLEXITY = add(new Metric("complexity"));
050      public static final Metric CLASS_COMPLEXITY = add(new Metric("class_complexity"));
051      public static final Metric FUNCTION_COMPLEXITY = add(new Metric("function_complexity"));
052      public static final Metric FILE_COMPLEXITY = add(new Metric("file_complexity"));
053      public static final Metric STATEMENTS = add(new Metric("statements"));
054    
055      public static final Metric CLASS_COMPLEXITY_DISTRIBUTION = add(new Metric("class_complexity_distribution"));
056      public static final Metric FUNCTION_COMPLEXITY_DISTRIBUTION = add(new Metric("function_complexity_distribution"));
057    
058      /* comments */
059      public static final Metric COMMENT_LINES = add(new Metric("comment_lines"));
060      public static final Metric COMMENT_LINES_DENSITY = add(new Metric("comment_lines_density"));
061      public static final Metric PUBLIC_DOCUMENTED_API_DENSITY = add(new Metric("public_documented_api_density"));
062      public static final Metric PUBLIC_UNDOCUMENTED_API = add(new Metric("public_undocumented_api"));
063      public static final Metric COMMENTED_OUT_CODE_LINES = add(new Metric("commented_out_code_lines"));
064    
065      /* unit tests */
066      public static final Metric TESTS = add(new Metric("tests"));
067      public static final Metric TESTS_EXECUTION_TIME = add(new Metric("test_execution_time"));
068      public static final Metric TEST_ERRORS = add(new Metric("test_errors"));
069      public static final Metric SKIPPED_TESTS = add(new Metric("skipped_tests"));
070      public static final Metric TEST_FAILURES = add(new Metric("test_failures"));
071      public static final Metric TEST_SUCCESS_DENSITY = add(new Metric("test_success_density"));
072      public static final Metric TEST_DATA = add(new Metric("test_data"));
073    
074      /* coverage */
075      public static final Metric COVERAGE = add(new Metric("coverage"));
076      public static final Metric LINE_COVERAGE = add(new Metric("line_coverage"));
077      public static final Metric UNCOVERED_LINES = add(new Metric("uncovered_lines"));
078      public static final Metric BRANCH_COVERAGE = add(new Metric("branch_coverage"));
079      public static final Metric UNCOVERED_CONDITIONS = add(new Metric("uncovered_conditions"));
080      public static final Metric COVERAGE_LINE_HITS_DATA = add(new Metric("coverage_line_hits_data"));
081      public static final Metric BRANCH_COVERAGE_HITS_DATA = add(new Metric("branch_coverage_hits_data"));
082    
083      /* duplicated lines */
084      public static final Metric DUPLICATED_LINES = add(new Metric("duplicated_lines"));
085      public static final Metric DUPLICATED_BLOCKS = add(new Metric("duplicated_blocks"));
086      public static final Metric DUPLICATED_FILES = add(new Metric("duplicated_files"));
087      public static final Metric DUPLICATED_LINES_DENSITY = add(new Metric("duplicated_lines_density"));
088      public static final Metric DUPLICATIONS_DATA = add(new Metric("duplications_data"));
089    
090      /* coding rules */
091      public static final Metric VIOLATIONS_DENSITY = add(new Metric("violations_density"));
092      public static final Metric VIOLATIONS = add(new Metric("violations"));
093      public static final Metric WEIGHTED_VIOLATIONS = add(new Metric("weighted_violations"));
094    
095      /* design */
096      public static final Metric LCOM4 = add(new Metric("lcom4"));
097      public static final Metric RFC = add(new Metric("rfc"));
098    
099      public static class MetricsList extends ResponsePOJO {
100    
101        private List<Metric> metrics = new ArrayList<Metric>();
102    
103        public List<Metric> getMetrics() {
104          return metrics;
105        }
106      }
107    
108      /**
109       * Generates a callback that will update the metrics definitions from the WSMetrics metrics constants list with data
110       * received from a MetricsQuery call
111       *
112       * @param callback
113       * @return
114       */
115      public static QueryCallBack<MetricsList> getUpdateMetricsFromServer(final QueryCallBack<MetricsList> callback) {
116        return new QueryCallBack<MetricsList>() {
117          public void onResponse(MetricsList response, JavaScriptObject jsonRawResponse) {
118            for (Metric metric : response.getMetrics()) {
119              Metric WSMetricConstant = get(metric.getKey());
120              if (WSMetricConstant != null) {
121                WSMetricConstant.updateFrom(metric);
122              } else {
123                add(metric);
124              }
125            }
126            callback.onResponse(response, jsonRawResponse);
127          }
128    
129          public void onError(int errorCode, String errorMessage) {
130            callback.onError(errorCode, errorMessage);
131          }
132    
133          public void onTimeout() {
134            callback.onTimeout();
135          }
136        };
137      }
138    
139      public static class Metric {
140        public enum ValueType {
141          INT, FLOAT, PERCENT, BOOL, STRING, MILLISEC, DATA, LEVEL, DISTRIB, RATING
142        }
143    
144        private String key;
145        private String name;
146        private String description;
147        private String domain;
148        private boolean qualitative;
149        private boolean userManaged;
150        private int direction;
151        private ValueType type;
152    
153        public Metric(String key) {
154          super();
155          this.key = key;
156        }
157    
158        public Metric(String key, String name, String description, String domain,
159                      boolean qualitative, boolean userManaged, int direction, ValueType type) {
160          super();
161          this.key = key;
162          this.name = name;
163          this.description = description;
164          this.domain = domain;
165          this.qualitative = qualitative;
166          this.userManaged = userManaged;
167          this.direction = direction;
168          this.type = type;
169        }
170    
171        public void updateFrom(Metric metric) {
172          this.name = metric.getName();
173          this.description = metric.getDescription();
174          this.domain = metric.getDomain();
175          this.qualitative = metric.isQualitative();
176          this.userManaged = metric.isUserManaged();
177          this.direction = metric.getDirection();
178          this.type = metric.getType();
179        }
180    
181        public String getName() {
182          return name;
183        }
184    
185        public ValueType getType() {
186          return type;
187        }
188    
189        public String getDescription() {
190          return description;
191        }
192    
193        public String getDomain() {
194          return domain;
195        }
196    
197        public boolean isQualitative() {
198          return qualitative;
199        }
200    
201        public boolean isUserManaged() {
202          return userManaged;
203        }
204    
205        public int getDirection() {
206          return direction;
207        }
208    
209        public String getKey() {
210          return key;
211        }
212    
213        @Override
214        public int hashCode() {
215          return key.hashCode();
216        }
217    
218        @Override
219        public boolean equals(Object obj) {
220          if (!(obj instanceof Metric)) {
221            return false;
222          }
223          if (this == obj) {
224            return true;
225          }
226          Metric other = (Metric) obj;
227          return key.equals(other.getKey());
228        }
229      }
230    
231      public static Metric add(Metric metric) {
232        if (!DICTIONNARY.contains(metric)) {
233          DICTIONNARY.add(metric);
234        }
235        return metric;
236      }
237    
238      public static Metric get(String metricKey) {
239        for (Metric metric : DICTIONNARY) {
240          if (metric.getKey().equals(metricKey)) {
241            return metric;
242          }
243        }
244        return new Metric(metricKey);
245      }
246    
247    }