Commit 3abcd2d5 authored by liguofu@yeejoin.com's avatar liguofu@yeejoin.com

优化:【空工大】maas数据源新增时,名称重复得问题

parent 64db854a
...@@ -146,7 +146,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -146,7 +146,7 @@ public class ClientHandler<path> implements Runnable {
// String product = robotAuthentication.getProduct(); // String product = robotAuthentication.getProduct();
// String appKey = robotAuthentication.getAppKey(); // String appKey = robotAuthentication.getAppKey();
String token = "f5f5ebba-f68a-4093-a78e-388c925a6eaa"; // 如果token、失效,就去浏览器复制最新的token、 String token = "a1bae8ec-6ef4-40f3-94b0-78f16a249b0e"; // 如果token、失效,就去浏览器复制最新的token、
String product = "AMOS_STUDIO_WEB"; String product = "AMOS_STUDIO_WEB";
String appKey = "AMOS_STUDIO"; String appKey = "AMOS_STUDIO";
...@@ -254,57 +254,60 @@ public class ClientHandler<path> implements Runnable { ...@@ -254,57 +254,60 @@ public class ClientHandler<path> implements Runnable {
// String product = robotAuthentication.getProduct(); // String product = robotAuthentication.getProduct();
// String appKey = robotAuthentication.getAppKey(); // String appKey = robotAuthentication.getAppKey();
String token = "f5f5ebba-f68a-4093-a78e-388c925a6eaa"; // 如果token、失效,就去浏览器复制最新的token、 String token = "a1bae8ec-6ef4-40f3-94b0-78f16a249b0e"; // 如果token、失效,就去浏览器复制最新的token、
String product = "AMOS_STUDIO_WEB"; String product = "AMOS_STUDIO_WEB";
String appKey = "AMOS_STUDIO"; String appKey = "AMOS_STUDIO";
String[] parts = url.split("/|:"); String[] parts = url.split("/|:");
String dbIp = parts[2]; String dbIp = parts[4];
int dbPort = Integer.parseInt(parts[3]); int dbPort = Integer.parseInt(parts[5]);
String dbName = parts[4]; String dbName = parts[6].split("\\?")[0];
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String defaultDatabaseName = "空工大_" + dbName; String defaultDatabaseName = "空工大_" + dbName;
//查询数据库是否已创建 //查询数据库是否已创建
String connId = ""; String connId = "";
String datasourcesUrl = "http://" + hostAndPort + "/maas/dsm/datasources/query"; String datasourcesUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns-jdbc";
Map<String, String> datasourcesParams = new HashMap<>(); Map<String, String> datasourcesParams = new HashMap<>();
datasourcesParams.put("pid", dataSourcesGroupId);
HttpEntity<Map<String, String>> datasourcesRequestEntity = new HttpEntity<>(datasourcesParams, getHeader(token, product, appKey, hostAndPort, false)); HttpEntity<Map<String, String>> datasourcesRequestEntity = new HttpEntity<>(datasourcesParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> datasourcesResponse = restTemplate.exchange(datasourcesUrl, HttpMethod.POST, datasourcesRequestEntity, String.class); ResponseEntity<String> datasourcesResponse = restTemplate.exchange(datasourcesUrl, HttpMethod.GET, datasourcesRequestEntity, String.class);
String datasourcesBody = datasourcesResponse.getBody(); String datasourcesBody = datasourcesResponse.getBody();
JSONObject datasourcesJsonObject = JSONObject.parseObject(datasourcesBody); JSONObject datasourcesJsonObject = JSONObject.parseObject(datasourcesBody);
JSONArray datasourcesResult = datasourcesJsonObject.getJSONArray("result"); JSONArray datasourcesResult = datasourcesJsonObject.getJSONArray("result");
if (datasourcesResult != null) { if (datasourcesResult != null && datasourcesResult.size() > 0) {
for (int i = 0; i < datasourcesResult.size(); i++) { for (int i = 0; i < datasourcesResult.size(); i++) {
Map<String, Object> stringObjectMap = (Map<String, Object>) datasourcesResult.get(i); Map<String, Object> stringObjectMap = (Map<String, Object>) datasourcesResult.get(i);
if (defaultDatabaseName.equals(stringObjectMap.get("name"))) { if (defaultDatabaseName.equals(stringObjectMap.get("name"))) {
connId = (String) stringObjectMap.get("id"); connId = (String) stringObjectMap.get("id");
break;
} }
} }
} }
//查询数据库表是否已创建 //查询数据库表是否已创建
boolean isCreateTable = false; boolean isCreateTable = false;
String jdbcTablesUrl = "http://" + hostAndPort + "/maas/dsm/jdbc/tables"; if (StringUtil.isNotEmpty(connId)) {
Map<String, String> jdbcTablesParams = new HashMap<>();
datasourcesParams.put("dsId", connId); String tablelistUrl = "http://" + hostAndPort + "/maas/maas/desktop/tablelist";
datasourcesParams.put("dsType", "mysql"); Map<String, String> tablelistParams = new HashMap<>();
datasourcesParams.put("resourceType", "mysql"); tablelistParams.put("connId", connId);
datasourcesParams.put("dsdetail", "{\"hostname\":\"" + dbIp + "\",\"port\":" + dbPort + ",\"name\":\"" + defaultDatabaseName + "\",\"database\":\"" + dbName + "\",\"username\":\"" + username + "\",\"password\":\"" + DesUtil.encode(password, passwordSecretKey) + "\",\"dbtype\":\"mysql\"}"); tablelistParams.put("subcjectId", dataSourcesGroupId);
HttpEntity<Map<String, String>> jdbcTablesRequestEntity = new HttpEntity<>(jdbcTablesParams, getHeader(token, product, appKey, hostAndPort, false)); HttpEntity<Map<String, String>> tablelistRequestEntity = new HttpEntity<>(tablelistParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> jdbcTablesResponse = restTemplate.exchange(jdbcTablesUrl, HttpMethod.POST, jdbcTablesRequestEntity, String.class); ResponseEntity<String> tablelistResponse = restTemplate.exchange(tablelistUrl, HttpMethod.POST, tablelistRequestEntity, String.class);
String jdbcTablesBody = jdbcTablesResponse.getBody(); String tablelistBody = tablelistResponse.getBody();
JSONObject jdbcTablesJsonObject = JSONObject.parseObject(jdbcTablesBody); JSONObject tablelistJsonObject = JSONObject.parseObject(tablelistBody);
JSONArray jdbcTablesResult = jdbcTablesJsonObject.getJSONArray("result"); JSONArray tablelistResult = tablelistJsonObject.getJSONArray("result");
if (jdbcTablesResult != null) { if (tablelistResult != null && tablelistResult.size() > 0) {
for (int i = 0; i < jdbcTablesResult.size(); i++) { for (int i = 0; i < tablelistResult.size(); i++) {
Map<String, Object> stringObjectMap = (Map<String, Object>) jdbcTablesResult.get(i); Map<String, Object> stringObjectMap = (Map<String, Object>) tablelistResult.get(i);
if (tableName.equals(stringObjectMap.get("name"))) { if (tableName.equals(stringObjectMap.get("name"))) {
isCreateTable = true; isCreateTable = true;
break;
}
} }
} }
} }
//3. 调用conn接口,数据库已创建过就不在创建 //3. 调用conn接口,数据库已创建过就不在创建
if (StringUtil.isEmpty(connId)) { if (StringUtil.isEmpty(connId)) {
String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns"; String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns";
...@@ -312,7 +315,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -312,7 +315,7 @@ public class ClientHandler<path> implements Runnable {
connsParams.put("name", defaultDatabaseName); connsParams.put("name", defaultDatabaseName);
connsParams.put("connType", "mysql"); connsParams.put("connType", "mysql");
connsParams.put("subjectid", dataSourcesGroupId); connsParams.put("subjectid", dataSourcesGroupId);
connsParams.put("detail", "{\"hostname\":\""+dbIp+"\",\"port\":"+dbPort+",\"username\":\"" + username + "\",\"password\":\"" + DesUtil.encode(password, passwordSecretKey) + "\",\"name\":\"" + defaultDatabaseName + "\",\"database\":\"" + dbName + "\",\"dbtype\":\"mysql\"}"); connsParams.put("detail", "{\"hostname\":\"" + dbIp + "\",\"port\":" + dbPort + ",\"username\":\"" + username + "\",\"password\":\"" + DesUtil.encode(password, passwordSecretKey) + "\",\"name\":\"" + defaultDatabaseName + "\",\"database\":\"" + dbName + "\",\"dbtype\":\"mysql\"}");
HttpEntity<Map<String, String>> connsRequestEntity = new HttpEntity<>(connsParams, getHeader(token, product, appKey, hostAndPort, false)); HttpEntity<Map<String, String>> connsRequestEntity = new HttpEntity<>(connsParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> connsResponse = restTemplate.exchange(connsUrl, HttpMethod.POST, connsRequestEntity, String.class); ResponseEntity<String> connsResponse = restTemplate.exchange(connsUrl, HttpMethod.POST, connsRequestEntity, String.class);
...@@ -336,16 +339,6 @@ public class ClientHandler<path> implements Runnable { ...@@ -336,16 +339,6 @@ public class ClientHandler<path> implements Runnable {
JSONObject tablesJsonObject = JSONObject.parseObject(tablesBody); JSONObject tablesJsonObject = JSONObject.parseObject(tablesBody);
String tablesResult = tablesJsonObject.getString("result"); String tablesResult = tablesJsonObject.getString("result");
String tablelistUrl = "http://" + hostAndPort + "/maas/maas/desktop/tablelist";
Map<String, String> tablelistParams = new HashMap<>();
tablesParams.put("connId", connId);
tablesParams.put("subcjectId", dataSourcesGroupId);
HttpEntity<Map<String, String>> tablelistRequestEntity = new HttpEntity<>(tablelistParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> tablelistResponse = restTemplate.exchange(tablelistUrl, HttpMethod.POST, tablelistRequestEntity, String.class);
String tablelistBody = tablelistResponse.getBody();
JSONObject tablelistJsonObject = JSONObject.parseObject(tablelistBody);
String tablelistResult = tablelistJsonObject.getString("result");
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment