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 */
020 package org.sonar.wsclient.issue.internal;
021
022 import org.json.simple.JSONArray;
023 import org.json.simple.JSONValue;
024 import org.sonar.wsclient.base.Paging;
025 import org.sonar.wsclient.component.Component;
026 import org.sonar.wsclient.issue.BulkChange;
027 import org.sonar.wsclient.issue.IssueChange;
028 import org.sonar.wsclient.issue.Issues;
029 import org.sonar.wsclient.rule.Rule;
030 import org.sonar.wsclient.unmarshallers.JsonUtils;
031 import org.sonar.wsclient.user.User;
032
033 import java.util.ArrayList;
034 import java.util.List;
035 import java.util.Map;
036
037 /**
038 * @since 3.6
039 */
040 public 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 result.setMaxResultsReached(JsonUtils.getBoolean(jsonRoot, "maxResultsReached"));
067 }
068
069 private void parseProjects(DefaultIssues result, Map jsonRoot) {
070 List<Map> jsonProjects = (List<Map>) jsonRoot.get("projects");
071 if (jsonProjects != null) {
072 for (Map jsonProject : jsonProjects) {
073 result.addProject(new Component(jsonProject));
074 }
075 }
076 }
077
078 private void parseComponents(DefaultIssues result, Map jsonRoot) {
079 List<Map> jsonComponents = (List<Map>) jsonRoot.get("components");
080 if (jsonComponents != null) {
081 for (Map jsonComponent : jsonComponents) {
082 result.addComponent(new Component(jsonComponent));
083 }
084 }
085 }
086
087 private void parseUsers(DefaultIssues result, Map jsonRoot) {
088 List<Map> jsonUsers = (List<Map>) jsonRoot.get("users");
089 if (jsonUsers != null) {
090 for (Map jsonUser : jsonUsers) {
091 result.add(new User(jsonUser));
092 }
093 }
094 }
095
096 private void parseRules(DefaultIssues result, Map jsonRoot) {
097 List<Map> jsonRules = (List<Map>) jsonRoot.get("rules");
098 if (jsonRules != null) {
099 for (Map jsonRule : jsonRules) {
100 result.add(new Rule(jsonRule));
101 }
102 }
103 }
104
105 private void parseActionPlans(DefaultIssues result, Map jsonRoot) {
106 List<Map> jsonRules = (List) jsonRoot.get("actionPlans");
107 if (jsonRules != null) {
108 for (Map jsonRule : jsonRules) {
109 result.add(new DefaultActionPlan(jsonRule));
110 }
111 }
112 }
113
114 List<String> parseTransitions(String json) {
115 List<String> transitions = new ArrayList<String>();
116 Map jRoot = (Map) JSONValue.parse(json);
117 List<String> jTransitions = (List<String>) jRoot.get("transitions");
118 for (String jTransition : jTransitions) {
119 transitions.add(jTransition);
120 }
121 return transitions;
122 }
123
124 List<IssueChange> parseChangelog(String json) {
125 List<IssueChange> changes = new ArrayList<IssueChange>();
126 Map jRoot = (Map) JSONValue.parse(json);
127 List<Map> jChanges = (List<Map>) jRoot.get("changelog");
128 if (jChanges != null) {
129 for (Map jChange : jChanges) {
130 changes.add(new DefaultIssueChange(jChange));
131 }
132 }
133 return changes;
134 }
135
136 List<String> parseActions(String json) {
137 List<String> actions = new ArrayList<String>();
138 Map jRoot = (Map) JSONValue.parse(json);
139 List<String> jActions = (List<String>) jRoot.get("actions");
140 for (String jAction : jActions) {
141 actions.add(jAction);
142 }
143 return actions;
144 }
145
146 BulkChange parseBulkChange(String json) {
147 DefaultBulkChange result = new DefaultBulkChange();
148
149 Map jsonRoot = (Map) JSONValue.parse(json);
150 Map issuesChanged = (Map) jsonRoot.get("issuesChanged");
151 result.setTotalIssuesChanged(JsonUtils.getInteger(issuesChanged, TOTAL));
152
153 Map issuesNotChanged = (Map) jsonRoot.get("issuesNotChanged");
154 result.setTotalIssuesNotChanged(JsonUtils.getInteger(issuesNotChanged, TOTAL));
155 JSONArray issuesJson = JsonUtils.getArray(issuesNotChanged, ISSUES);
156 if (issuesJson != null) {
157 result.setIssuesNotChanged(issuesJson);
158 }
159
160 return result;
161 }
162 }