001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.wsclient.issue.internal;
021
022import org.json.simple.JSONArray;
023import org.json.simple.JSONValue;
024import org.sonar.wsclient.base.Paging;
025import org.sonar.wsclient.component.Component;
026import org.sonar.wsclient.issue.BulkChange;
027import org.sonar.wsclient.issue.IssueChange;
028import org.sonar.wsclient.issue.Issues;
029import org.sonar.wsclient.rule.Rule;
030import org.sonar.wsclient.unmarshallers.JsonUtils;
031import org.sonar.wsclient.user.User;
032
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * @since 3.6
039 */
040public class IssueJsonParser {
041
042  private static final String ISSUES = "issues";
043  private static final String TOTAL = "total";
044
045  public Issues parseIssues(String json) {
046    DefaultIssues result = new DefaultIssues();
047    Map jsonRoot = (Map) JSONValue.parse(json);
048    List<Map> jsonIssues = (List<Map>) jsonRoot.get(ISSUES);
049    if (jsonIssues != null) {
050      for (Map jsonIssue : jsonIssues) {
051        result.add(new DefaultIssue(jsonIssue));
052      }
053    }
054    parseRules(result, jsonRoot);
055    parseUsers(result, jsonRoot);
056    parseComponents(result, jsonRoot);
057    parseProjects(result, jsonRoot);
058    parseActionPlans(result, jsonRoot);
059    parsePaging(result, jsonRoot);
060    return result;
061  }
062
063  private void parsePaging(DefaultIssues result, Map jsonRoot) {
064    Map paging = (Map) jsonRoot.get("paging");
065    result.setPaging(new Paging(paging));
066  }
067
068  private void parseProjects(DefaultIssues result, Map jsonRoot) {
069    List<Map> jsonProjects = (List<Map>) jsonRoot.get("projects");
070    if (jsonProjects != null) {
071      for (Map jsonProject : jsonProjects) {
072        result.addProject(new Component(jsonProject));
073      }
074    }
075  }
076
077  private void parseComponents(DefaultIssues result, Map jsonRoot) {
078    List<Map> jsonComponents = (List<Map>) jsonRoot.get("components");
079    if (jsonComponents != null) {
080      for (Map jsonComponent : jsonComponents) {
081        result.addComponent(new Component(jsonComponent));
082      }
083    }
084  }
085
086  private void parseUsers(DefaultIssues result, Map jsonRoot) {
087    List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
088    if (jsonUsers != null) {
089      for (Map jsonUser : jsonUsers) {
090        result.add(new User(jsonUser));
091      }
092    }
093  }
094
095  private void parseRules(DefaultIssues result, Map jsonRoot) {
096    List<Map> jsonRules = (List<Map>) jsonRoot.get("rules");
097    if (jsonRules != null) {
098      for (Map jsonRule : jsonRules) {
099        result.add(new Rule(jsonRule));
100      }
101    }
102  }
103
104  private void parseActionPlans(DefaultIssues result, Map jsonRoot) {
105    List<Map> jsonRules = (List) jsonRoot.get("actionPlans");
106    if (jsonRules != null) {
107      for (Map jsonRule : jsonRules) {
108        result.add(new DefaultActionPlan(jsonRule));
109      }
110    }
111  }
112
113  List<String> parseTransitions(String json) {
114    List<String> transitions = new ArrayList<String>();
115    Map jRoot = (Map) JSONValue.parse(json);
116    List<String> jTransitions = (List<String>) jRoot.get("transitions");
117    for (String jTransition : jTransitions) {
118      transitions.add(jTransition);
119    }
120    return transitions;
121  }
122
123  List<IssueChange> parseChangelog(String json) {
124    List<IssueChange> changes = new ArrayList<IssueChange>();
125    Map jRoot = (Map) JSONValue.parse(json);
126    List<Map> jChanges = (List<Map>) jRoot.get("changelog");
127    if (jChanges != null) {
128      for (Map jChange : jChanges) {
129        changes.add(new DefaultIssueChange(jChange));
130      }
131    }
132    return changes;
133  }
134
135  List<String> parseActions(String json) {
136    List<String> actions = new ArrayList<String>();
137    Map jRoot = (Map) JSONValue.parse(json);
138    List<String> jActions = (List<String>) jRoot.get("actions");
139    for (String jAction : jActions) {
140      actions.add(jAction);
141    }
142    return actions;
143  }
144
145  BulkChange parseBulkChange(String json) {
146    DefaultBulkChange result = new DefaultBulkChange();
147
148    Map jsonRoot = (Map) JSONValue.parse(json);
149    Map issuesChanged = (Map) jsonRoot.get("issuesChanged");
150    result.setTotalIssuesChanged(JsonUtils.getInteger(issuesChanged, TOTAL));
151
152    Map issuesNotChanged = (Map) jsonRoot.get("issuesNotChanged");
153    result.setTotalIssuesNotChanged(JsonUtils.getInteger(issuesNotChanged, TOTAL));
154    JSONArray issuesJson = JsonUtils.getArray(issuesNotChanged, ISSUES);
155    if (issuesJson != null) {
156      result.setIssuesNotChanged(issuesJson);
157    }
158
159    return result;
160  }
161}