001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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.JSONValue;
023import org.sonar.wsclient.component.Component;
024import org.sonar.wsclient.issue.Issues;
025import org.sonar.wsclient.base.Paging;
026import org.sonar.wsclient.rule.Rule;
027import org.sonar.wsclient.unmarshallers.JsonUtils;
028import org.sonar.wsclient.user.User;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * @since 3.6
036 */
037public class IssueJsonParser {
038
039  public Issues parseIssues(String json) {
040    DefaultIssues result = new DefaultIssues();
041    Map jsonRoot = (Map) JSONValue.parse(json);
042    List<Map> jsonIssues = (List<Map>) jsonRoot.get("issues");
043    if (jsonIssues != null) {
044      for (Map jsonIssue : jsonIssues) {
045        result.add(new DefaultIssue(jsonIssue));
046      }
047    }
048    parseRules(result, jsonRoot);
049    parseUsers(result, jsonRoot);
050    parseComponents(result, jsonRoot);
051    parseProjects(result, jsonRoot);
052    parseActionPlans(result, jsonRoot);
053    parsePaging(result, jsonRoot);
054    return result;
055  }
056
057  private void parsePaging(DefaultIssues result, Map jsonRoot) {
058    Map paging = (Map) jsonRoot.get("paging");
059    result.setPaging(new Paging(paging));
060    result.setMaxResultsReached(JsonUtils.getBoolean(jsonRoot, "maxResultsReached"));
061  }
062
063  private void parseProjects(DefaultIssues result, Map jsonRoot) {
064    List<Map> jsonProjects = (List<Map>) jsonRoot.get("projects");
065    if (jsonProjects != null) {
066      for (Map jsonProject : jsonProjects) {
067        result.addProject(new Component(jsonProject));
068      }
069    }
070  }
071
072  private void parseComponents(DefaultIssues result, Map jsonRoot) {
073    List<Map> jsonComponents = (List<Map>) jsonRoot.get("components");
074    if (jsonComponents != null) {
075      for (Map jsonComponent : jsonComponents) {
076        result.addComponent(new Component(jsonComponent));
077      }
078    }
079  }
080
081  private void parseUsers(DefaultIssues result, Map jsonRoot) {
082    List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
083    if (jsonUsers != null) {
084      for (Map jsonUser : jsonUsers) {
085        result.add(new User(jsonUser));
086      }
087    }
088  }
089
090  private void parseRules(DefaultIssues result, Map jsonRoot) {
091    List<Map> jsonRules = (List<Map>) jsonRoot.get("rules");
092    if (jsonRules != null) {
093      for (Map jsonRule : jsonRules) {
094        result.add(new Rule(jsonRule));
095      }
096    }
097  }
098
099  private void parseActionPlans(DefaultIssues result, Map jsonRoot) {
100    List<Map> jsonRules = (List) jsonRoot.get("actionPlans");
101    if (jsonRules != null) {
102      for (Map jsonRule : jsonRules) {
103        result.add(new DefaultActionPlan(jsonRule));
104      }
105    }
106  }
107
108  List<String> parseTransitions(String json) {
109    List<String> transitions = new ArrayList<String>();
110    Map jRoot = (Map) JSONValue.parse(json);
111    List<String> jTransitions = (List<String>) jRoot.get("transitions");
112    for (String jTransition : jTransitions) {
113      transitions.add(jTransition);
114    }
115    return transitions;
116  }
117
118  List<String> parseActions(String json) {
119    List<String> actions = new ArrayList<String>();
120    Map jRoot = (Map) JSONValue.parse(json);
121    List<String> jActions = (List<String>) jRoot.get("actions");
122    for (String jAction : jActions) {
123      actions.add(jAction);
124    }
125    return actions;
126  }
127}