Commit 59a5b83a authored by Sanford Jonathan Edelist's avatar Sanford Jonathan Edelist
Browse files

Delete DatabaseUpdater.java

parent 95726b93
Loading
Loading
Loading
Loading

DatabaseUpdater.java

deleted100644 → 0
+0 −66
Original line number Diff line number Diff line
package org.group8;

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import org.bson.Document;
import object_detection.types.ObjectSet;
import object_detection.types.PointSet;
import java.util.ArrayList;
import java.util.List;

public class DatabaseUpdater {

    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> collection;

    public DatabaseUpdater() {
        String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
        mongoClient = MongoClients.create(uri);
        database = mongoClient.getDatabase("Objects");
        collection = database.getCollection("objectSets");
    }

    /**
     * Updates the MongoDB with an ObjectSet.
     * 
     * @param objSet The ObjectSet to update in the database.
     * @return true if the update was successful, false otherwise.
     */
    public boolean updateDB(ObjectSet objSet) {
        try {
            for (PointSet pointSet : ObjectSet.objects) {
                Document doc = pointSetToDocument(String.valueOf(pointSet.hashCode()), pointSet);
                ReplaceOptions options = new ReplaceOptions().upsert(true);
                collection.replaceOne(Filters.eq("setId", String.valueOf(pointSet.hashCode())), doc, options);
            }
            return true;
        } catch (Exception e) {
            System.err.println("An error occurred while updating the database: " + e);
            return false;
        }
    }

    private Document pointSetToDocument(String setId, PointSet pointSet) {
        List<Document> pointsList = new ArrayList<>();
        for (Point p : pointSet.getPoints()) {
            pointsList.add(new Document("x", p.getX())
                    .append("y", p.getY())
                    .append("z", p.getZ()));
        }

        return new Document("setId", setId)
                .append("points", pointsList);
    }
    
    public static void main(String[] args) {
        DatabaseUpdater updater = new DatabaseUpdater();
        ObjectSet objectSet = new ObjectSet(); // Assume this is populated
        boolean success = updater.updateDB(objectSet);
        System.out.println("Update successful: " + success);
    }
}