jhorvath commented on code in PR #7413:
URL: https://github.com/apache/netbeans/pull/7413#discussion_r1625626034


##########
enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/CloudAssets.java:
##########
@@ -0,0 +1,279 @@
+/*
+ * 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.assets;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.stream.JsonReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.StringReader;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.db.explorer.ConnectionListener;
+import org.netbeans.api.db.explorer.ConnectionManager;
+import org.netbeans.api.db.explorer.DatabaseConnection;
+import org.netbeans.modules.cloud.oracle.bucket.BucketItem;
+import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
+import org.netbeans.modules.cloud.oracle.compute.ComputeInstanceItem;
+import org.netbeans.modules.cloud.oracle.database.DatabaseItem;
+import org.netbeans.modules.cloud.oracle.items.OCID;
+import org.netbeans.modules.cloud.oracle.items.OCIItem;
+import org.netbeans.modules.cloud.oracle.vault.VaultItem;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.modules.OnStart;
+import org.openide.util.ChangeSupport;
+import org.openide.util.Exceptions;
+
+/**
+ *
+ * @author Jan Horvath
+ */
+public class CloudAssets {
+
+    private static final String CLOUD_ASSETS_PATH = "CloudAssets"; //NOI18N
+    private static final String CLOUD_ASSETS_FILE = "default.json"; //NOI18N
+    private static CloudAssets instance = null;
+
+    private boolean assetsLoaded = false;
+    private Set<OCIItem> items = new HashSet<>();
+
+    private final ChangeSupport changeSupport;
+    private final Gson gson;
+
+    CloudAssets() {
+        this.gson = new GsonBuilder()
+                .setPrettyPrinting()
+                .registerTypeAdapter(OCID.class, new OCIDDeserializer())
+                .create();
+        changeSupport = new ChangeSupport(this);
+        ConnectionManager.getDefault().addConnectionListener(() -> {
+            DatabaseConnection[] connections = 
ConnectionManager.getDefault().getConnections();
+            Set<String> ocids = new HashSet<>();
+            for (int i = 0; i < connections.length; i++) {
+                ocids.add((String) 
connections[i].getConnectionProperties().get("OCID")); //NOI18N
+            }
+            boolean update = false;
+            for (Iterator it = items.iterator(); it.hasNext();) {
+                OCIItem item = (OCIItem) it.next();
+                if (!ocids.contains(item.getKey().getValue()) && 
"Databases".equals(item.getKey().getPath())) { //NOI18N
+                    it.remove();
+                    update = true;
+                }
+            }
+            if (update) {
+                update();
+            }
+        });
+    }
+
+    public static synchronized CloudAssets getDefault() {
+        if (instance == null) {
+            instance = new CloudAssets();
+        }
+        return instance;
+    }
+
+    @OnStart
+    public static final class Init implements Runnable {
+
+        @Override
+        public void run() {
+            CloudAssets.getDefault().loadAssets();
+        }
+
+    }
+
+    public void addItem(OCIItem newItem) {
+        items.add(newItem);
+        update();
+    }
+
+    void removeItem(OCIItem item) {
+        if (items.remove(item)) {
+            update();
+        }
+    }
+
+    public void update() {
+        OpenProjectsFinder.getDefault().findOpenProjects().thenAccept(projects 
-> {
+            SuggestionAnalyzer analyzer = new DependenciesAnalyzer();
+            Set<SuggestedItem> suggested = analyzer.findSuggestions(projects);
+            setSuggestions(suggested);
+        });
+    }
+
+    public synchronized void setSuggestions(Set<SuggestedItem> suggested) {
+        for (OCIItem item : new HashSet<OCIItem>(items)) {
+            if (item.getKey().getPath().equals("Suggested")) { //NOI18N
+                items.remove(item);
+            }
+        }
+        Set<String> present = items.stream()
+                .map(i -> i.getKey().getPath())
+                .collect(Collectors.toSet());
+        for (SuggestedItem s : suggested) {
+            if (!present.contains(s.getPath())) {
+                boolean add = true;
+                for (String exclusivePath : s.getExclusivePaths()) {
+                    if (present.contains(exclusivePath)) {
+                        add = false;
+                        continue;
+                    }
+                }
+                if (add) {
+                    items.add(s);
+                }
+            }
+        }
+        if (assetsLoaded) {
+            storeAssets();

Review Comment:
   accepted for storing assets, it makes sense only when assets are added or 
remove. In this place only suggestions are being updated and we don't persist 
it. Fire has to be called here as the suggestions might be updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to