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 com.google.gwt.json.client.JSONString; 026 import com.google.gwt.json.client.JSONValue; 027 import org.sonar.api.web.gwt.client.Utils; 028 029 public final class ViolationsQuery extends AbstractResourceQuery<Violations> { 030 031 private String scopes; 032 private String qualifiers; 033 private String rules; 034 private String categories; 035 private String priorities; 036 private Integer depth; 037 038 private ViolationsQuery(String resourceKey) { 039 super(resourceKey); 040 } 041 042 public static ViolationsQuery create(String resourceKey) { 043 return new ViolationsQuery(resourceKey); 044 } 045 046 public String getScopes() { 047 return scopes; 048 } 049 050 public ViolationsQuery setScopes(String scopes) { 051 this.scopes = scopes; 052 return this; 053 } 054 055 public String getQualifiers() { 056 return qualifiers; 057 } 058 059 public ViolationsQuery setQualifiers(String qualifiers) { 060 this.qualifiers = qualifiers; 061 return this; 062 } 063 064 public String getRules() { 065 return rules; 066 } 067 068 public ViolationsQuery setRules(String rules) { 069 this.rules = rules; 070 return this; 071 } 072 073 public String getCategories() { 074 return categories; 075 } 076 077 public ViolationsQuery setCategories(String s) { 078 this.categories = s; 079 return this; 080 } 081 082 public Integer getDepth() { 083 return depth; 084 } 085 086 public ViolationsQuery setDepth(Integer depth) { 087 this.depth = depth; 088 return this; 089 } 090 091 public String getPriorities() { 092 return priorities; 093 } 094 095 public ViolationsQuery setPriorities(String priorities) { 096 this.priorities = priorities; 097 return this; 098 } 099 100 @Override 101 public String toString() { 102 String url = Utils.getServerApiUrl() + "/violations?resource=" + getResourceKey() + "&"; 103 if (depth != null) { 104 url += "depth=" + depth + "&"; 105 } 106 if (scopes != null) { 107 url += "scopes=" + scopes + "&"; 108 } 109 if (qualifiers != null) { 110 url += "qualifiers=" + qualifiers + "&"; 111 } 112 if (rules != null) { 113 url += "rules=" + rules + "&"; 114 } 115 if (categories != null) { 116 url += "categories=" + categories + "&"; 117 } 118 if (priorities != null) { 119 url += "priorities=" + priorities + "&"; 120 } 121 return url; 122 } 123 124 @Override 125 public void execute(final QueryCallBack<Violations> callback) { 126 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<Violations>(callback) { 127 @Override 128 public Violations parseResponse(JavaScriptObject obj) { 129 return parseJSON(obj); 130 } 131 }); 132 } 133 134 private Violations parseJSON(JavaScriptObject obj) { 135 Violations result = new Violations(); 136 JSONArray jsonArray = new JSONArray(obj); 137 for (int i = 0; i < jsonArray.size(); i++) { 138 JSONObject jsViolation = jsonArray.get(i).isObject(); 139 if (jsViolation == null) { 140 continue; 141 } 142 JSONString message = jsViolation.get("message").isString(); 143 JSONString priority = jsViolation.get("priority").isString(); 144 JSONValue lineJson = jsViolation.get("line"); 145 int lineIndex = 0; 146 if (lineJson != null) { 147 lineIndex = (int) lineJson.isNumber().doubleValue(); 148 } 149 150 JSONObject ruleObj = jsViolation.get("rule").isObject(); 151 Rule rule = new Rule( 152 JsonUtils.getString(ruleObj, "key"), 153 JsonUtils.getString(ruleObj, "name"), 154 JsonUtils.getString(ruleObj, "category") 155 ); 156 157 result.add(new Violation(message.stringValue(), priority.stringValue(), lineIndex, rule, null)); 158 } 159 return result; 160 } 161 162 }