This is an automated email from the ASF dual-hosted git repository. jhorvath pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 8ac5875 Adding internal command for registering DB connection (#3765) 8ac5875 is described below commit 8ac58755808acf8d8cda5cac735ef59616ec408f Author: jhorvath <j...@horvath.cz> AuthorDate: Thu Mar 31 21:19:50 2022 +0200 Adding internal command for registering DB connection (#3765) Make Add DB Command capable of adding connection with predefined properties --- .../java/lsp/server/db/DBAddConnection.java | 52 ++++++++++++++++++++++ java/java.lsp.server/vscode/src/extension.ts | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java index a2dc1ff..aefaacc 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/db/DBAddConnection.java @@ -18,6 +18,8 @@ */ package org.netbeans.modules.java.lsp.server.db; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import java.net.URL; import java.sql.DatabaseMetaData; import java.sql.ResultSet; @@ -25,6 +27,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; @@ -66,12 +69,61 @@ import org.openide.util.lookup.ServiceProvider; @ServiceProvider(service = CodeActionsProvider.class) public class DBAddConnection extends CodeActionsProvider { public static final String DB_ADD_CONNECTION = "db.add.connection"; // NOI18N + public static final String USER_ID = "userId"; // NOI18N + public static final String PASSWORD = "password"; // NOI18N + public static final String DRIVER = "driver"; // NOI18N + public static final String DB_URL = "url"; // NOI18N + public static final String SCHEMA = "schema"; // NOI18N + public static final String DISPLAY_NAME = "displayName"; // NOI18N + + private final Gson gson = new Gson(); @Override public CompletableFuture<Object> processCommand(NbCodeLanguageClient client, String command, List<Object> arguments) { if (!DB_ADD_CONNECTION.equals(command)) { return null; } + + String userId = null; + String dbUrl = null; + String driverClass = null; + final Map m = gson.fromJson((JsonObject) arguments.get(0), Map.class); + if (m != null) { + userId = (String) m.get(USER_ID); + dbUrl = (String) m.get(DB_URL); + driverClass = (String) m.get(DRIVER); + } + if (dbUrl != null && driverClass != null) { + + JDBCDriver[] driver = JDBCDriverManager.getDefault().getDrivers(driverClass); //NOI18N + if (driver != null && driver.length > 0) { + CompletableFuture<String> usernameFuture = userId != null ? CompletableFuture.completedFuture(userId) : client.showInputBox(new ShowInputBoxParams( + Bundle.MSG_EnterUsername(), userId)); + + usernameFuture.thenAccept((username) -> { //NOI18N + if (username == null) { + return; + } + String password = (String) m.get(PASSWORD); + CompletableFuture<String> passwordFuture = password != null ? CompletableFuture.completedFuture(password) : client.showInputBox(new ShowInputBoxParams( + Bundle.MSG_EnterPassword(), "", true)); + passwordFuture.thenAccept((p) -> { //NOI18N + if (p == null) { + return; + } + DatabaseConnection dbconn = DatabaseConnection.create(driver[0], (String) m.get(DB_URL), username, (String) m.get(SCHEMA), p, true, (String) m.get(DISPLAY_NAME)); + try { + ConnectionManager.getDefault().addConnection(dbconn); + } catch (DatabaseException ex) { + client.showMessage(new MessageParams(MessageType.Error, ex.getMessage())); + } + }); + }); + client.showMessage(new MessageParams(MessageType.Info, Bundle.MSG_ConnectionAdded())); + } + return CompletableFuture.completedFuture(null); + } + JDBCDriver[] drivers = JDBCDriverManager.getDefault().getDrivers(); List<QuickPickItem> items = new ArrayList<>(); for (int i = 0; i < drivers.length; i++) { diff --git a/java/java.lsp.server/vscode/src/extension.ts b/java/java.lsp.server/vscode/src/extension.ts index 275fa4d..4d80da1 100644 --- a/java/java.lsp.server/vscode/src/extension.ts +++ b/java/java.lsp.server/vscode/src/extension.ts @@ -523,7 +523,6 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { // register completions: launchConfigurations.registerCompletion(context); - return Object.freeze({ version : API_VERSION }); @@ -552,6 +551,7 @@ function activateWithJDK(specifiedJDK: string | null, context: ExtensionContext, }); const a : Promise<void> | null = maintenance; commands.executeCommand('setContext', 'nbJavaLSReady', false); + commands.executeCommand('setContext', 'dbAddConnectionPresent', true); activationPending = true; // chain the restart after termination of the former process. if (a != null) { --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists