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