Commit c72d8b4d authored by Nafis A Abeer's avatar Nafis A Abeer
Browse files

refixed

parent 940a1474
Loading
Loading
Loading
Loading
(6 KiB)

File changed.

No diff preview for this file type.

(6 KiB)

File changed.

No diff preview for this file type.

(6 KiB)

File changed.

No diff preview for this file type.

(10 KiB)

File changed.

No diff preview for this file type.

+69 −27
Original line number Diff line number Diff line
@@ -26,18 +26,15 @@ public class MongoDBInteraction {
        String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true";

        // Construct a ServerApi instance using the ServerApi.builder() method
        ServerApi serverApi = ServerApi.builder()
                .version(ServerApiVersion.V1)
                .build();

        MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(new ConnectionString(uri))
                .serverApi(serverApi)
                .serverApi(ServerApi.builder().version(ServerApiVersion.V1).build())
                .build();

        this.mongoClient = MongoClients.create(settings);
        this.database = mongoClient.getDatabase("Objects");
        this.objectCollection = database.getCollection("objectSets");
        System.out.println("MongoDB Connection Established");
    }


@@ -46,64 +43,109 @@ public class MongoDBInteraction {
     * @return
     */
    public ObjectSet retrieveLatestObjectSet() {
        try {
            Document doc = objectCollection.find().sort(new Document("index", -1)).first();
        return doc != null ? convertDocumentToObjectSet(doc) : null;
            if (doc == null) {
                System.out.println("No document found.");
                return null;
            } else {
                System.out.println("Document found: " + doc.toJson());
                return convertDocumentToObjectSet(doc);
            }
        } catch (Exception e) {
            System.out.println("Error retrieving document: " + e.getMessage());
            return null;
        }
    }

    private ObjectSet convertDocumentToObjectSet(Document doc) {
        List<Document> pointSetsDocs = doc.getList("objectSets", Document.class);
        ObjectSet objectSet = new ObjectSet();
        for (Document pointSetDoc : pointSetsDocs) {
            objectSet.objects.add(convertDocumentToPointSet(pointSetDoc));
        if (doc == null) {
            System.out.println("Document is null, no conversion possible.");
            return null;
        }

        List<Document> pointSetDocs = doc.getList("objectSets", Document.class);
        if (pointSetDocs == null || pointSetDocs.isEmpty()) {
            System.out.println("No point sets found in document.");
            return new ObjectSet();
        }

        ObjectSet objectSet = new ObjectSet();
        for (Document pointSetDoc : pointSetDocs) {
            PointSet pointSet = convertDocumentToPointSet(pointSetDoc);
            if (pointSet != null) {
                objectSet.objects.add(pointSet);
            } else {
                System.out.println("Failed to convert point set document: " + pointSetDoc.toJson());
            }
        }
        return objectSet;
    }

    private PointSet convertDocumentToPointSet(Document doc) {
        int idx = Integer.parseInt(doc.getString("setId"));
        if (doc == null) {
            System.out.println("PointSet document is null.");
            return null;
        }

        // Using Integer.parseInt to safely convert String to Integer if necessary
        int idx;
        try {
            idx = doc.get("setId") instanceof Integer ? (Integer) doc.get("setId") : Integer.parseInt((String) doc.get("setId"));
        } catch (NumberFormatException e) {
            System.err.println("Invalid format for setId, must be an integer: " + doc.get("setId"));
            return null;
        }

        List<Document> pointsDocs = doc.getList("points", Document.class);
        if (pointsDocs == null) {
            System.out.println("No points found in point set document.");
            return new PointSet(idx);
        }

        PointSet pointSet = new PointSet(idx);
        for (Document pointDoc : pointsDocs) {
            float x = pointDoc.getDouble("x").floatValue();
            float y = pointDoc.getDouble("y").floatValue();
            float z = pointDoc.getDouble("z").floatValue();
            pointSet.addPoint(new Point(x, y, z, 0));
            Point point = new Point(
                    pointDoc.getDouble("x").floatValue(),
                    pointDoc.getDouble("y").floatValue(),
                    pointDoc.getDouble("z").floatValue(),
                    pointDoc.getInteger("R"),
                    pointDoc.getInteger("G"),
                    pointDoc.getInteger("B")
                    );
            pointSet.addPoint(point);
        }
        return pointSet;
    }

    // PointSet management methods

    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()));
                    .append("z", p.getZ())
                    .append("R", p.R)
                    .append("G", p.G)
                    .append("B", p.B));
        }
        return new Document("setId", setId)
                .append("points", pointsList);
    }


    // ObjectSet management methods
    private Document objectSetToDocument(int index, ObjectSet objectSet) {
        List<Document> pointSetsList = new ArrayList<>();

        for (object_detection.types.PointSet ps : objectSet.objects) {
        List<Document> objectList = new ArrayList<>();
        for (PointSet ps : objectSet.objects) {
            Document pointSetDoc = pointSetToDocument(Integer.toString(ps.getIDX()), ps);
            pointSetsList.add(pointSetDoc);
            objectList.add(pointSetDoc);
        }
        return new Document("index", index)
                .append("objectSets", pointSetsList);
                .append("objectSets", objectList);
    }


    public void updateObjectSet(int index, ObjectSet objectSet) {
        Document doc = objectSetToDocument(index, objectSet);
        ReplaceOptions options = new ReplaceOptions().upsert(true);
        objectCollection.replaceOne(Filters.eq("index", index), doc, options);
    }

}
Loading