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 */ 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.Issues; 028 import org.sonar.wsclient.rule.Rule; 029 import org.sonar.wsclient.unmarshallers.JsonUtils; 030 import org.sonar.wsclient.user.User; 031 032 import java.util.ArrayList; 033 import java.util.List; 034 import java.util.Map; 035 036 /** 037 * @since 3.6 038 */ 039 public class IssueJsonParser { 040 041 public Issues parseIssues(String json) { 042 DefaultIssues result = new DefaultIssues(); 043 Map jsonRoot = (Map) JSONValue.parse(json); 044 List<Map> jsonIssues = (List<Map>) jsonRoot.get("issues"); 045 if (jsonIssues != null) { 046 for (Map jsonIssue : jsonIssues) { 047 result.add(new DefaultIssue(jsonIssue)); 048 } 049 } 050 parseRules(result, jsonRoot); 051 parseUsers(result, jsonRoot); 052 parseComponents(result, jsonRoot); 053 parseProjects(result, jsonRoot); 054 parseActionPlans(result, jsonRoot); 055 parsePaging(result, jsonRoot); 056 return result; 057 } 058 059 private void parsePaging(DefaultIssues result, Map jsonRoot) { 060 Map paging = (Map) jsonRoot.get("paging"); 061 result.setPaging(new Paging(paging)); 062 result.setMaxResultsReached(JsonUtils.getBoolean(jsonRoot, "maxResultsReached")); 063 } 064 065 private void parseProjects(DefaultIssues result, Map jsonRoot) { 066 List<Map> jsonProjects = (List<Map>) jsonRoot.get("projects"); 067 if (jsonProjects != null) { 068 for (Map jsonProject : jsonProjects) { 069 result.addProject(new Component(jsonProject)); 070 } 071 } 072 } 073 074 private void parseComponents(DefaultIssues result, Map jsonRoot) { 075 List<Map> jsonComponents = (List<Map>) jsonRoot.get("components"); 076 if (jsonComponents != null) { 077 for (Map jsonComponent : jsonComponents) { 078 result.addComponent(new Component(jsonComponent)); 079 } 080 } 081 } 082 083 private void parseUsers(DefaultIssues result, Map jsonRoot) { 084 List<Map> jsonUsers = (List<Map>) jsonRoot.get("users"); 085 if (jsonUsers != null) { 086 for (Map jsonUser : jsonUsers) { 087 result.add(new User(jsonUser)); 088 } 089 } 090 } 091 092 private void parseRules(DefaultIssues result, Map jsonRoot) { 093 List<Map> jsonRules = (List<Map>) jsonRoot.get("rules"); 094 if (jsonRules != null) { 095 for (Map jsonRule : jsonRules) { 096 result.add(new Rule(jsonRule)); 097 } 098 } 099 } 100 101 private void parseActionPlans(DefaultIssues result, Map jsonRoot) { 102 List<Map> jsonRules = (List) jsonRoot.get("actionPlans"); 103 if (jsonRules != null) { 104 for (Map jsonRule : jsonRules) { 105 result.add(new DefaultActionPlan(jsonRule)); 106 } 107 } 108 } 109 110 List<String> parseTransitions(String json) { 111 List<String> transitions = new ArrayList<String>(); 112 Map jRoot = (Map) JSONValue.parse(json); 113 List<String> jTransitions = (List<String>) jRoot.get("transitions"); 114 for (String jTransition : jTransitions) { 115 transitions.add(jTransition); 116 } 117 return transitions; 118 } 119 120 List<String> parseActions(String json) { 121 List<String> actions = new ArrayList<String>(); 122 Map jRoot = (Map) JSONValue.parse(json); 123 List<String> jActions = (List<String>) jRoot.get("actions"); 124 for (String jAction : jActions) { 125 actions.add(jAction); 126 } 127 return actions; 128 } 129 130 BulkChange parseBulkChange(String json) { 131 DefaultBulkChange result = new DefaultBulkChange(); 132 133 Map jsonRoot = (Map) JSONValue.parse(json); 134 Map issuesChanged = (Map) jsonRoot.get("issuesChanged"); 135 result.setTotalIssuesChanged(JsonUtils.getInteger(issuesChanged, "total")); 136 137 Map issuesNotChanged = (Map) jsonRoot.get("issuesNotChanged"); 138 result.setTotalIssuesNotChanged(JsonUtils.getInteger(issuesNotChanged, "total")); 139 JSONArray issuesJson = JsonUtils.getArray(issuesNotChanged, "issues"); 140 if (issuesJson != null) { 141 result.setIssuesNotChanged(issuesJson); 142 } 143 144 return result; 145 } 146 }