Skip to content

Commit

Permalink
Support test connect when and or update workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
BrookYuGit committed Nov 7, 2021
1 parent d61413c commit e7ecc0e
Show file tree
Hide file tree
Showing 33 changed files with 302 additions and 142 deletions.
20 changes: 20 additions & 0 deletions sdp_export/sdp_sdp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3531,6 +3531,26 @@
"table_name": "sdp_workspace",
"workspace_name": "sdp"
},
{
"id": null,
"is_disable": 0,
"java_imports": "",
"java_return_type": "String",
"java_type": "SdpWorkspaceUpdateRequest",
"name": "request",
"parameter_catalog": "api.facade",
"parameter_catalog_type": "test_connect",
"parameter_is_export_excel": 0,
"parameter_is_import_excel": 0,
"parameter_mode": "append",
"parameter_sql_issimple": 0,
"parameter_sql_return_nolist": 0,
"parameter_sql_value": "",
"remarks": "",
"sort_no": 0,
"table_name": "sdp_workspace",
"workspace_name": "sdp"
},
{
"id": null,
"is_disable": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ public Integer cloneWorkspace(HttpServletRequest httpServletRequest, HttpServlet
return facade.cloneWorkspace(request);
}

public String testConnect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, SdpWorkspaceUpdateRequest request) throws Exception {
return facade.testConnect(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ public Integer cloneWorkspace(HttpServletRequest httpServletRequest, HttpServlet
return super.cloneWorkspace(httpServletRequest, httpServletResponse, request);
}

@ResponseBody
@PostMapping("/test_connect")
public String testConnect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestBody SdpWorkspaceUpdateRequest request) throws Exception {
fixRequest(request);
return super.testConnect(httpServletRequest, httpServletResponse, request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
public class ProcessSQLFacadeImpl extends BaseFacadeImpl implements ProcessSQLFacade {

@Autowired

DataSource dataSource;

@Autowired
Expand Down Expand Up @@ -243,11 +242,11 @@ public Integer execute(BaseNameRequest request, Object _processInstance, Method
FileUtil.clear();

try {
String lineSeparator = System.lineSeparator();
lineSeparator = "\r\n";
Field lineSeparatorField = System.class.getDeclaredField("lineSeparator");
lineSeparatorField.setAccessible(true);
lineSeparatorField.set(System.class, lineSeparator);
// String lineSeparator = System.lineSeparator();
// lineSeparator = "\r\n";
// Field lineSeparatorField = System.class.getDeclaredField("lineSeparator");
// lineSeparatorField.setAccessible(true);
// lineSeparatorField.set(System.class, lineSeparator);

}catch (Exception ex) {
ex.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ public interface SdpWorkspaceFacade {

Integer cloneWorkspace(BaseNameIdRequest request) throws Exception;

String testConnect(SdpWorkspaceUpdateRequest request) throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,9 @@ public Integer cloneWorkspace(BaseNameIdRequest request) throws Exception {
throw createFailException("not implement");
}

@Override
public String testConnect(SdpWorkspaceUpdateRequest request) throws Exception {
throw createFailException("not implement");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
public class SdpWorkspaceFacadeCustomImpl extends SdpWorkspaceFacadeBaseImpl {

@Autowired

DataSource dataSource;

@Autowired
Expand Down Expand Up @@ -517,4 +516,97 @@ public List<SdpWorkspaceGetTableListResponse> getTableList(SdpWorkspaceGetTableL

return responseList;
}

@Override
public String testConnect(SdpWorkspaceUpdateRequest request) throws Exception {
SdpWorkspaceQueryResponse workspace = new SdpWorkspaceQueryResponse();
if (request.getId() != null && !Integer.valueOf(0).equals(request.getId())) {
SdpWorkspaceWithBLOBs oriWorkspace = sdpWorkspaceMapper.selectByPrimaryKeyWithBLOBs(request.getId());
if (oriWorkspace == null) {
throw new Exception("Not found workspace:"+request.getId());
}
BeanUtils.copyProperties(oriWorkspace, workspace);
if (!StringUtils.isEmpty(oriWorkspace.getDbPassword())) {
workspace.setDbPassword(processSQLFacade.decryptDbPassword(workspace));
}
if (request.getDbClassname() != null) {
workspace.setDbClassname(request.getDbClassname());
}
if (request.getDbHost() != null) {
workspace.setDbHost(request.getDbHost());
}
if (request.getDbPort() != null) {
workspace.setDbPort(request.getDbPort());
}
if (request.getDbDatabase() != null) {
workspace.setDbDatabase(request.getDbDatabase());
}
if (request.getDbUsername() != null) {
workspace.setDbUsername(request.getDbUsername());
}
if (request.getDbPassword() != null) {
workspace.setDbPassword(request.getDbPassword());
}
} else {
BeanUtils.copyProperties(request, workspace);
}
if (Integer.valueOf(0).equals(request.getDbPort())) {
workspace.setDbPort(null);
}

String url;
if ("com.mysql.jdbc.Driver".equals(workspace.getDbClassname()) || "com.mysql.cj.jdbc.Driver".equals(workspace.getDbClassname())) {
url = "jdbc:mysql://"+workspace.getDbHost()+":"+workspace.getDbPort()+"/"+workspace.getDbDatabase()+"?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai&tcpKeepAlive=true&autoReconnect=true&useSSL=false";
} else if ("org.h2.Driver".equals(workspace.getDbClassname())) {
url = "jdbc:h2:"+workspace.getDbDatabase()+";AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1";
} else {
throw new Exception("不支持的数据库驱动类:"+workspace.getDbClassname()+","+JSON.toJSONString(workspace));
}
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setDriverClass(workspace.getDbClassname());
jdbcConnectionConfiguration.setConnectionURL(url);
jdbcConnectionConfiguration.setUserId(workspace.getDbUsername());
String dbPassword = workspace.getDbPassword();
jdbcConnectionConfiguration.setPassword(dbPassword);

Context context = new Context(null);
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_AUTO_DELIMIT_KEYWORDS, "true");

JavaTypeResolver javaTypeResolver = ObjectFactory
.createJavaTypeResolver(context, new ArrayList<>());

Connection connection = null;

try {
if ("org.h2.Driver".equals(workspace.getDbClassname()) && StringUtils.isEmpty(workspace.getDbDatabase())) {
connection = dataSource.getConnection();
context.setConnection(connection);
} else {
ConnectionFactory connectionFactory;
connectionFactory = new JDBCConnectionFactory(jdbcConnectionConfiguration);

connection = connectionFactory.getConnection();
connection.close();
}
}catch(Exception ex) {
ex.printStackTrace();
if (ex instanceof SQLNonTransientConnectionException) {
Throwable ex1 = ((SQLNonTransientConnectionException)ex).getCause();
if (ex1 == null) {
throw new Exception("无法连接到数据库:数据库:"+workspace.getDbHost()+":"+workspace.getDbPort()+"@"+workspace.getDbUsername()+"("+ex.getMessage()+")");
}
if (ex1.getMessage().indexOf("Access denied for user") >= 0) {
throw new Exception("数据库无权限或密码错误:数据库:"+workspace.getDbHost()+":"+workspace.getDbPort()+"@"+workspace.getDbUsername()+"("+ex1.getMessage()+")"+","+ex.getMessage());
}

throw new Exception("无法连接到数据库:数据库:"+workspace.getDbHost()+":"+workspace.getDbPort()+"@"+workspace.getDbUsername()+"("+ex1.getMessage()+")"+","+ex.getMessage());

}
throw new Exception("无法连接到数据库:数据库:"+workspace.getDbHost()+":"+workspace.getDbPort()+"@"+workspace.getDbUsername()+":"+workspace.getDbPassword()+"("+ex.getMessage()+")");
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ public Integer cloneWorkspace(BaseNameIdRequest request) throws Exception {
return super.cloneWorkspace(request);
}

@Override
public String testConnect(SdpWorkspaceUpdateRequest request) throws Exception {
return super.testConnect(request);
}

}

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions server/sdp/src/main/resources/static/css/app.ee2dfb7f.css

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e7ecc0e

Please sign in to comment.