Commit 82830dda authored by Zane Safi Mroue's avatar Zane Safi Mroue Committed by Rohan Kumar
Browse files

fixed database

parent 58909b33
Loading
Loading
Loading
Loading
+9 −84
Original line number Diff line number Diff line
package database;

import com.mongodb.client.*;
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 object_detection.types.ObjectSet;
@@ -17,6 +13,7 @@ import java.util.List;
import java.util.Map;

public class MongoDBInteraction {

    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> objectCollection;
@@ -45,6 +42,11 @@ public class MongoDBInteraction {
        return allObjectSets;
    }

    public ObjectSet retrieveLatestObjectSet() {
        Document doc = objectCollection.find().sort(new Document("index", -1)).first();
        return doc != null ? convertDocumentToObjectSet(doc) : null;
    }

    private ObjectSet convertDocumentToObjectSet(Document doc) {
        List<Document> pointSetsDocs = doc.getList("objectSets", Document.class);
        ObjectSet objectSet = new ObjectSet();
@@ -54,92 +56,15 @@ public class MongoDBInteraction {
        return objectSet;
    }


    // 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()));
        }
        return new Document("setId", setId)
                .append("points", pointsList);
    }

    /*
    public void update(String setId, PointSet pointSet) {
        Document doc = pointSetToDocument(setId, pointSet);
        ReplaceOptions options = new ReplaceOptions().upsert(true);
        pointCollection.replaceOne(Filters.eq("setId", setId), doc, options);
        cache.put(setId, pointSet);
    }

    public PointSet retrieve(String setId) {
        PointSet cachedPointSet = cache.get(setId);
        if (cachedPointSet != null) {
            return cachedPointSet;
        }
        Document query = new Document("setId", setId);
        Document doc = pointCollection.find(query).first();
        if (doc != null) {
            PointSet pointSet = convertDocumentToPointSet(doc);
            cache.put(setId, pointSet);
            return pointSet;
        }
        return null;
    }

 */

    private PointSet convertDocumentToPointSet(Document doc) {
        List<Document> pointsList = (List<Document>) doc.get("points");
        List<Document> pointsDocs = doc.getList("points", Document.class);
        PointSet pointSet = new PointSet();
        for (Document pointDoc : pointsList) {
        for (Document pointDoc : pointsDocs) {
            float x = pointDoc.getDouble("x").floatValue();
            float y = pointDoc.getDouble("y").floatValue();
            float z = pointDoc.getDouble("z").floatValue();
            int additionalData = pointDoc.getInteger("additionalData", 0); // Assuming 'additionalData' is a field in your document
            pointSet.addPoint(new Point(x, y, z, additionalData));
            pointSet.addPoint(new Point(x, y, z, 0));
        }
        return pointSet;
    }


    // ObjectSet management methods
    private Document objectSetToDocument(int index, ObjectSet objectSet) {
        List<Document> pointSetsList = new ArrayList<>();
        int setId = 1;
        for (object_detection.types.PointSet ps : objectSet.objects) {
            Document pointSetDoc = pointSetToDocument(String.valueOf(setId++), ps);
            pointSetsList.add(pointSetDoc);
        }
        return new Document("index", index)
                .append("objectSets", pointSetsList);
    }


    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);
    }

    public ObjectSet retrieveObjectSet(int index) {
        Document query = new Document("index", index);
        Document doc = objectCollection.find(query).first();
        if (doc != null) {
            return convertDocumentToObjectSet(doc);
        }
        return null;
    }

    public ObjectSet retrieveLatestObjectSet() {
        // Assuming 'index' is an integer that increments with each new ObjectSet
        Document highestIndexDoc = objectCollection.find().sort(new Document("index", -1)).first();
        if (highestIndexDoc != null) {
            return convertDocumentToObjectSet(highestIndexDoc);
        }
        return null;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -103,7 +103,7 @@ public class ObjectSet {
            // get the Point and its corresponding 2D projection from the current KeyFrame
            String[] line = scanner.nextLine().split(",");
            Point2D point2D = new Point2D(Float.parseFloat(line[0]), Float.parseFloat(line[1]), Integer.parseInt(line[2]));
            Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5]), Integer.parseInt(line[2]));
            Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5]));

            // Check if the point is within any bounding box
            for (int i = 0; i < boundingBoxes.size(); i++) {
+2 −26
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ public class BackendJava {
    }

    @RestController
    @RequestMapping("/objectset")
    @RequestMapping("/api")
    public static class ObjectSetController {

        private final MongoDBInteraction dbInteraction = new MongoDBInteraction();
@@ -39,7 +39,7 @@ public class BackendJava {
        @GetMapping("/{index}")
        public ResponseEntity<String> getObjectSet(@PathVariable int index) {
            try {
                ObjectSet objectSet = dbInteraction.retrieveObjectSet(index);
                ObjectSet objectSet = dbInteraction.retrieveLatestObjectSet();
                if (objectSet != null) {
                    return ResponseEntity.ok(gson.toJson(objectSet));
                } else {
@@ -66,7 +66,6 @@ public class BackendJava {
            }
        }
    }
    }

    @Controller
    public static class BackendService {
@@ -122,29 +121,6 @@ public class BackendJava {
        public String getPC() {
            return "js/pointCloud.js";
        }

    @RestController
    @RequestMapping("/objectset")
    public static class ObjectSetController {

        private final MongoDBInteraction dbInteraction = new MongoDBInteraction();
        private final Gson gson = new GsonBuilder().create();

        @GetMapping("/{index}")
        public ResponseEntity<String> getObjectSet(@PathVariable int index) {
            try {
                ObjectSet objectSet = dbInteraction.retrieveObjectSet(index);
                if (objectSet != null) {
                    return ResponseEntity.ok(gson.toJson(objectSet.objects));
                } else {
                    return ResponseEntity.notFound().build();
                }
            } catch (NumberFormatException e) {
                return ResponseEntity.badRequest().body("{\"error\":\"Invalid index format\"}");
            } catch (Exception e) {
                return ResponseEntity.internalServerError().body("{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}");
            }
        }
    }

    @Configuration