001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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 java.util.ArrayList;
023    import java.util.List;
024    
025    import org.sonar.api.web.gwt.client.Utils;
026    import org.sonar.api.web.gwt.client.webservices.WSMetrics.MetricsList;
027    
028    import com.google.gwt.core.client.JavaScriptObject;
029    import com.google.gwt.json.client.JSONArray;
030    import com.google.gwt.json.client.JSONObject;
031    
032    public final class MetricsQuery extends Query<MetricsList> {
033      
034      private Boolean userManaged;
035      private List<WSMetrics.Metric.ValueType> excludedTypes = new ArrayList<WSMetrics.Metric.ValueType>();
036    
037      public static MetricsQuery get() {
038        return new MetricsQuery();
039      }
040    
041      private MetricsQuery() {
042        super();
043      }
044      
045      public Boolean isUserManaged() {
046        return userManaged;
047      }
048    
049      public MetricsQuery setUserManaged(Boolean userManaged) {
050        this.userManaged = userManaged;
051        return this;
052      }
053      
054      public MetricsQuery excludeTypes(WSMetrics.Metric.ValueType... types) {
055        for (WSMetrics.Metric.ValueType valueType : types) {
056          excludedTypes.add(valueType);
057        }
058        return this;
059      }
060    
061      @Override
062      public String toString() {
063        return Utils.getServerApiUrl() + "/metrics?";
064      }
065    
066      @Override
067      public void execute(QueryCallBack<MetricsList> callback) {
068        JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<MetricsList>(callback) {
069          @Override
070          public MetricsList parseResponse(JavaScriptObject obj) {
071            return parseMetrics(obj);
072          }
073        });
074      }
075    
076      private MetricsList parseMetrics(JavaScriptObject json) {
077        JSONArray array = new JSONArray(json);
078        MetricsList list = new MetricsList();
079        for (int i = 0; i < array.size(); i++) {
080          JSONObject jsStock = array.get(i).isObject();
081          if (jsStock != null) {
082            WSMetrics.Metric m = parseMetric(jsStock);
083            boolean skip = (isUserManaged() != null && (!isUserManaged() && m.isUserManaged())) || excludedTypes.contains(m.getType());
084            if (!skip) {
085              list.getMetrics().add(m);
086            }
087          }
088        }
089        return list;
090      }
091    
092      private WSMetrics.Metric parseMetric(JSONObject json) {
093        String key = JsonUtils.getString(json, "key");
094        String name = JsonUtils.getString(json, "name");
095        String description = JsonUtils.getString(json, "description");
096        String domain = JsonUtils.getString(json, "domain");
097        String type = JsonUtils.getString(json, "val_type");
098        boolean qualitative = JsonUtils.getBoolean(json, "qualitative");
099        boolean userManaged = JsonUtils.getBoolean(json, "user_managed");
100        Integer direction = JsonUtils.getInteger(json, "direction");
101        return new WSMetrics.Metric(key, name, description, domain, qualitative, userManaged, direction, WSMetrics.Metric.ValueType.valueOf(type));
102      }
103    
104    }