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 8fbcf30828 Adding new action which registers ADB connection (#5291)
8fbcf30828 is described below

commit 8fbcf308288e8c8b490ec520ff9078591a0a0c46
Author: jhorvath <j...@horvath.cz>
AuthorDate: Tue Jan 17 10:09:07 2023 +0100

    Adding new action which registers ADB connection (#5291)
    
    * Adding new action which registers ADB connection
    
    * minor fixes in AddABAction
---
 .../modules/cloud/oracle/actions/AddADBAction.java | 144 +++++++++++++++++++++
 .../netbeans/modules/nbcode/integration/layer.xml  |   1 +
 .../java/lsp/server/db/DBAddConnection.java        |   3 +-
 .../lsp/server/explorer/NodeActionsProvider.java   |  16 ++-
 java/java.lsp.server/vscode/package.json           |   6 +-
 5 files changed, 163 insertions(+), 7 deletions(-)

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 0000000000..beb96cc5b7
--- /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<TenancyItem> tenancies = new ArrayList<>();
+        for (OCIProfile p : OCIManager.getDefault().getConnectedProfiles()) {
+           p.getTenancy().ifPresent(tenancies::add);
+        }
+        Optional<TenancyItem> selectedTenancy = chooseOneItem(tenancies, 
Bundle.SelectTenancy());
+        
+        Optional<CompartmentItem> selectedCompartment = Optional.empty();
+                
+        if (!selectedTenancy.isPresent()) {
+            return;
+        }
+        
+        List<CompartmentItem> 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 <T extends OCIItem> Optional<T> chooseOneItem(List<T> ociItems, 
String title) {
+        Optional<T> result = Optional.empty();
+        if (ociItems.size() == 1) {
+            result = Optional.of(ociItems.get(0));
+        } else if (ociItems.size() > 0) {
+            List<Item> 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<String> 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<OCIItem> 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 75a90d7e2b..9bd333789a 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 @@
             <file name="cloud-resources-commands.instance">
                 <attr name="instanceCreate" 
methodvalue="org.netbeans.modules.java.lsp.server.explorer.NodeActionsProvider.forFile"/>
                 <attr 
name="action:org.netbeans.modules.cloud.oracle.actions.DownloadWalletAction" 
stringvalue="Tools"/>
+                <attr 
name="action:org.netbeans.modules.cloud.oracle.actions.AddADBAction" 
stringvalue="Tools"/>
                 <attr 
name="action:org.netbeans.modules.cloud.oracle.actions.CreateAutonomousDBAction"
 stringvalue="Tools"/>
                 <attr 
name="action:org.netbeans.modules.cloud.oracle.actions.OpenServiceConsoleAction"
 stringvalue="Tools"/>
                 <attr 
name="action:org.netbeans.modules.cloud.oracle.actions.CloudRefresh" 
stringvalue="Tools"/>
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 20ba44676a..c24ddae16a 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 class DBAddConnection extends CodeActionsProvider {
         }
         
         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 5d59d0499a..b583025c98 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.FileObject;
 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 class NodeActionsProvider extends 
CodeActionsProvider {
         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 @@ public class NodeActionsProvider extends 
CodeActionsProvider {
         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 7ec5f18093..bbbe553713 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"


---------------------------------------------------------------------
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

Reply via email to