From 8fbcf308288e8c8b490ec520ff9078591a0a0c46 Mon Sep 17 00:00:00 2001 From: jhorvath Date: Tue, 17 Jan 2023 10:09:07 +0100 Subject: [PATCH] Adding new action which registers ADB connection (#5291) * Adding new action which registers ADB connection * minor fixes in AddABAction --- .../cloud/oracle/actions/AddADBAction.java | 144 ++++++++++++++++++ .../modules/nbcode/integration/layer.xml | 1 + .../java/lsp/server/db/DBAddConnection.java | 3 +- .../server/explorer/NodeActionsProvider.java | 16 +- java/java.lsp.server/vscode/package.json | 6 +- 5 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java diff --git a/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java new file mode 100644 index 000000000000..beb96cc5b721 --- /dev/null +++ b/enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/actions/AddADBAction.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.cloud.oracle.actions; + +import com.oracle.bmc.model.BmcException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import org.netbeans.modules.cloud.oracle.OCIManager; +import org.netbeans.modules.cloud.oracle.OCIProfile; +import org.netbeans.modules.cloud.oracle.compartment.CompartmentItem; +import org.netbeans.modules.cloud.oracle.compartment.CompartmentNode; +import org.netbeans.modules.cloud.oracle.database.DatabaseItem; +import org.netbeans.modules.cloud.oracle.database.DatabaseNode; +import org.netbeans.modules.cloud.oracle.items.OCIItem; +import org.netbeans.modules.cloud.oracle.items.TenancyItem; +import org.openide.DialogDescriptor; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.NotifyDescriptor.QuickPick.Item; +import org.openide.awt.ActionID; +import org.openide.awt.ActionReference; +import org.openide.awt.ActionReferences; +import org.openide.awt.ActionRegistration; +import org.openide.util.NbBundle; + +/** + * + * @author Jan Horvath + */ +@ActionID( + category = "Tools", + id = "org.netbeans.modules.cloud.oracle.actions.AddADBAction" +) +@ActionRegistration( + displayName = "#AddADB", + asynchronous = true +) + +@ActionReferences(value = { + @ActionReference(path = "Cloud/Oracle/Common/Actions", position = 260) +}) +@NbBundle.Messages({ + "AddADB=Add Oracle Autonomous DB", + "SelectTenancy=Select Tenancy", + "SelectCompartment=Select Compartment", + "SelectDatabase=Select Database" +}) +public class AddADBAction implements ActionListener { + private static final Logger LOGGER = Logger.getLogger(AddADBAction.class.getName()); + + + @Override + public void actionPerformed(ActionEvent e) { + List tenancies = new ArrayList<>(); + for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) { + p.getTenancy().ifPresent(tenancies::add); + } + Optional selectedTenancy = chooseOneItem(tenancies, Bundle.SelectTenancy()); + + Optional selectedCompartment = Optional.empty(); + + if (!selectedTenancy.isPresent()) { + return; + } + + List compartments = CompartmentNode.getCompartments().apply(selectedTenancy.get()); + selectedCompartment = chooseOneItem(compartments, Bundle.SelectCompartment()); + DatabaseItem selectedDatabase = null; + + if (selectedCompartment.isPresent()) { + while(selectedDatabase == null) { + OCIItem item = chooseCopartmentOrDb(selectedCompartment.get()); + if (item == null) { + return; + } + if (item instanceof DatabaseItem) { + selectedDatabase = (DatabaseItem) item; + } + if (item instanceof CompartmentItem) { + selectedCompartment = Optional.of((CompartmentItem) item); + } + } + } + if (selectedDatabase != null) { + DownloadWalletAction action = new DownloadWalletAction(selectedDatabase); + action.actionPerformed(null); + } + } + + private Optional chooseOneItem(List ociItems, String title) { + Optional result = Optional.empty(); + if (ociItems.size() == 1) { + result = Optional.of(ociItems.get(0)); + } else if (ociItems.size() > 0) { + List items = ociItems.stream() + .map(tenancy -> new Item(tenancy.getName(), tenancy.getDescription())) + .collect(Collectors.toList()); + NotifyDescriptor.QuickPick qp = new NotifyDescriptor.QuickPick(title, title, items, false); + if (DialogDescriptor.OK_OPTION == DialogDisplayer.getDefault().notify(qp)) { + Optional selected = qp.getItems().stream().filter(item -> item.isSelected()).map(item -> item.getLabel()).findFirst(); + if (selected.isPresent()) { + result = ociItems.stream().filter(t -> t.getName().equals(selected.get())).findFirst(); + } + + } + } + return result; + } + + + private OCIItem chooseCopartmentOrDb(CompartmentItem compartment) { + List items = new ArrayList<> (); + try { + items.addAll(DatabaseNode.getDatabases().apply(compartment)); + } catch (BmcException e) { + LOGGER.log(Level.SEVERE, "Unable to load compartment list", e); // NOI18N + } + items.addAll(CompartmentNode.getCompartments().apply(compartment)); + return chooseOneItem(items, Bundle.SelectDatabase()).orElseGet(() -> null); + } + +} diff --git a/java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/layer.xml b/java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/layer.xml index 75a90d7e2b6f..9bd333789a1b 100644 --- a/java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/layer.xml +++ b/java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/layer.xml @@ -42,6 +42,7 @@ + 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 20ba44676a24..c24ddae16afe 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 @@ -19,6 +19,7 @@ package org.netbeans.modules.java.lsp.server.db; import com.google.gson.Gson; +import com.google.gson.JsonNull; import com.google.gson.JsonObject; import java.net.URL; import java.sql.DatabaseMetaData; @@ -104,7 +105,7 @@ public CompletableFuture processCommand(NbCodeLanguageClient client, Str } if (arguments != null && !arguments.isEmpty()) { - final Map m = gson.fromJson((JsonObject) arguments.get(0), Map.class); + final Map m = arguments.get(0) instanceof JsonNull ? Collections.emptyMap() : gson.fromJson((JsonObject) arguments.get(0), Map.class); String userId = m != null ? (String) m.get(USER_ID) : null; String password = m != null ? (String) m.get(PASSWORD) : null; String dbUrl = m != null ? (String) m.get(DB_URL) : null; diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/explorer/NodeActionsProvider.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/explorer/NodeActionsProvider.java index 5d59d0499a17..b583025c985c 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/explorer/NodeActionsProvider.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/explorer/NodeActionsProvider.java @@ -41,7 +41,6 @@ import org.openide.filesystems.FileUtil; import org.openide.nodes.Node; import org.openide.util.ContextAwareAction; -import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.lookup.Lookups; import org.openide.util.lookup.ProxyLookup; @@ -100,15 +99,17 @@ public CompletableFuture processCommand(NbCodeLanguageClient client, Str if (!command.startsWith(NBLS_ACTION_PREFIX)) { return CompletableFuture.completedFuture(false); } - JsonObject item = gson.fromJson(gson.toJson(arguments.get(0)), JsonObject.class); - JsonElement el = item.get("data"); // NOI18N + JsonElement el = null; + if (arguments.size() > 0) { + JsonObject item = gson.fromJson(gson.toJson(arguments.get(0)), JsonObject.class); + el = item.get("data"); // NOI18N + } int id = -1; if (el != null) { - JsonElement nodeId = el.getAsJsonObject().get("id"); + JsonElement nodeId = el.getAsJsonObject().get("id"); // NOI18N if (nodeId != null && nodeId.isJsonPrimitive()) { id = nodeId.getAsJsonPrimitive().getAsInt(); - } } @@ -171,6 +172,11 @@ CompletableFuture invokeAction(NbCodeLanguageClient client, String categ FileObject config = FileUtil.getConfigFile(path); String contextType = (String) config.getAttribute("type"); //NOI18N try { + if (contextType == null) { + Action a = Actions.forID(category, aid); + a.actionPerformed(new ActionEvent(client, 0, aid)); + return CompletableFuture.completedFuture(false); + } Class clazz = Thread.currentThread().getContextClassLoader().loadClass(contextType); Object context = gson.fromJson(gson.toJson(arguments.get(0)), clazz); if (context != null) { diff --git a/java/java.lsp.server/vscode/package.json b/java/java.lsp.server/vscode/package.json index 7ec5f18093cc..bbbe55371375 100644 --- a/java/java.lsp.server/vscode/package.json +++ b/java/java.lsp.server/vscode/package.json @@ -88,7 +88,7 @@ "viewsWelcome": [ { "view": "database.connections", - "contents": "No Database Connections found.\n[Add a new connection](command:db.add.connection)" + "contents": "No Database Connections found.\n[Add a new connection](command:db.add.connection)\n[Add Oracle Autonomous DB](command:nbls:Tools:org.netbeans.modules.cloud.oracle.actions.AddADBAction)" } ], "configuration": { @@ -584,6 +584,10 @@ "command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.DownloadWalletAction", "title": "Add DB Connection" }, + { + "command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.AddADBAction", + "title": "Add Oracle Autonomous Database" + }, { "command": "nbls:Tools:org.netbeans.modules.cloud.oracle.actions.CreateAutonomousDBAction", "title": "Create Autonomous Database"