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