Commit b953275c authored by Zane Safi Mroue's avatar Zane Safi Mroue
Browse files

fixed database

parent 5dc68eb7
Loading
Loading
Loading
Loading

Main.java

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

public class Main {

    public static void main(String[] args) {
        MongoDBInteraction dbInteraction = new MongoDBInteraction();

        // Test PointSet Operations
        String pointSetId = "set101";
        PointSet testPointSet = new PointSet(new Point(1.0f, 2.0f, 3.0f));
        System.out.println("Attempting to update/insert PointSet with ID: " + pointSetId);
        dbInteraction.update(pointSetId, testPointSet);
        System.out.println("PointSet Added/Updated");

        System.out.println("Attempting to retrieve PointSet with ID: " + pointSetId);
        PointSet retrievedPointSet = dbInteraction.retrieve(pointSetId);
        if (retrievedPointSet != null && !retrievedPointSet.getPoints().isEmpty()) {
            System.out.println("PointSet Retrieved Successfully");
        } else {
            System.out.println("PointSet Retrieve Failed");
        }

//        System.out.println("Attempting to delete PointSet with ID: " + pointSetId);
//        dbInteraction.delete(pointSetId);
//        PointSet deletedPointSet = dbInteraction.retrieve(pointSetId);
//        if (deletedPointSet == null) {
//            System.out.println("PointSet Successfully Deleted");
//        } else {
//            System.out.println("PointSet Delete Failed");
//        }

        // Test ObjectSet Operations
        ObjectSet objectSet = new ObjectSet();
        System.out.println("Creating and populating ObjectSet with two PointSets.");
        objectSet.makeObject(new Point(1, 2, 3), new Point(4, 5, 6));
        objectSet.makeObject(new Point(7, 8, 9), new Point(10, 11, 12));

        int index = 1;
        System.out.println("Attempting to update/insert ObjectSet at index: " + index);
        dbInteraction.updateObjectSet(index, objectSet);
        System.out.println("ObjectSet Add/Update Completed");

        System.out.println("Attempting to retrieve ObjectSet at index: " + index);
        ObjectSet retrievedObjectSet = dbInteraction.retrieveObjectSet(index);
        if (retrievedObjectSet != null && !retrievedObjectSet.objects.isEmpty()) {
            System.out.println("ObjectSet Retrieved Successfully");
        } else {
            System.out.println("ObjectSet Retrieve Failed");
        }

//        System.out.println("Attempting to delete ObjectSet at index: " + index);
//        dbInteraction.deleteObjectSet(index);
//        ObjectSet deletedObjectSet = dbInteraction.retrieveObjectSet(index);
//        if (deletedObjectSet == null) {
//            System.out.println("ObjectSet Successfully Deleted");
//        } else {
//            System.out.println("ObjectSet Delete Failed");
//        }
    }
}

Point.java

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

public class Point {

    // index
    static int count = 0;

    // members
    private float x;
    private float y;
    private float z;
    final int idx;

    // METHODS

    /*
    Object Constructor of a feature in 3D
     */
    public Point(float xx, float yy, float zz){
        this.x = xx;
        this.y = yy;
        this.z = zz;
        this.idx = count++;
    }

    /**
     * helper function for equals overriding
     * @param aa : first point
     * @param bb : second point
     * @param err : error between aa and bb that is acceptable
     * @return true if points are close enough
     */
    public static boolean equals(Point aa, Point bb, float err){
        return (aa.x - bb.x < err) && (aa.y - bb.y < err) && (aa.z - bb.z < err);
    }

    @Override
    public boolean equals(Object o){
        if(getClass() != o.getClass()){
            return false;
        }
        Point p = (Point) o;
        return Point.equals(this, p, (float) 0.05);
    }

    @Override
    public int hashCode() {
        float res = this.x + this.y + this.z;
        return Float.hashCode(res);
    }
    @Override
    public String toString() {
        return "Point(" + this.x + " ," + this.y + " ," + this.z + ")";
    }

    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }
    public float getZ() {
        return z;
    }

}

PointSet.java

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

import java.util.*;

public class PointSet {

    static final int NUM_REPS = 10;
    Set<Point> pset;
    Point[] reps;

    /**
     *
     * @param pp : the points that this PointSet will contain
     */
    public PointSet(Point ...pp){
        pset = new HashSet<>();

        // add every point blankly to pointset
        pset.addAll(Arrays.asList(pp));
        reps = new Point[NUM_REPS];
    }

    public void addPoint(Point p){
        pset.add(p);
    }

    public void addAll(Point ...p) {
        Collections.addAll(pset, p);
    }

    public void addAll(List<Point> p) {
        pset.addAll(p);
    }

    public Point[] getSetReps(){
        return this.reps;
    }

    public List<Point> getPoints(){
        return new ArrayList<>(pset);
    }

    /**
     * This method is called on a specific Point, and updates the Point via
     * some disjoint sets' method.
     */
    public void updateReps(){
        // get first NUM_REPS from pset for now, will make more efficient later
        Iterator<Point> iter = this.pset.iterator();
        for(int i = 0; i < NUM_REPS; i++){

            // early exit if we have less than NUM_REPS points in the current object
            if(i == this.pset.size()) {
                return;
            }

            reps[i] = iter.next();
        }
    }

}

Server.java

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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import static spark.Spark.*;

public class Server {
    public static void main(String[] args) {
        port(8080);

        MongoDBInteraction dbInteraction = new MongoDBInteraction();

        // Customize Gson to handle the specifics of your data structure
        Gson gson = new GsonBuilder().create();

        // ObjectSet GET endpoint
        get("/objectset/:index", (request, response) -> {
            response.type("application/json");
            try {
                int index = Integer.parseInt(request.params(":index"));
                ObjectSet objectSet = dbInteraction.retrieveObjectSet(index);

                if (objectSet != null) {
                    // Manually serialize the static list
                    return gson.toJson(ObjectSet.objects);
                } else {
                    response.status(404);
                    return "{}";
                }
            } catch (NumberFormatException e) {
                response.status(400);
                return "{\"error\":\"Invalid index format\"}";
            } catch (Exception e) {
                response.status(500);
                return "{\"error\":\"Internal Server Error\"}";
            }
        });

        // ObjectSet POST endpoint
        post("/objectset/:index", (request, response) -> {
            response.type("application/json");
            try {
                int index = Integer.parseInt(request.params(":index"));
                // Assume JSON is being sent correctly formatted to match the static list structure
                List<PointSet> receivedObjects = gson.fromJson(request.body(), new TypeToken<List<PointSet>>(){}.getType());
                ObjectSet.objects = receivedObjects; // Directly set the static list
                dbInteraction.updateObjectSet(index, new ObjectSet()); // Pass a new ObjectSet instance that will use the updated static list
                return gson.toJson(ObjectSet.objects);
            } catch (Exception e) {
                e.printStackTrace();
                response.status(500);
                return "{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}";
            }
        });

        // ObjectSet DELETE endpoint
        delete("/objectset/:index", (request, response) -> {
            int index = Integer.parseInt(request.params(":index"));
            dbInteraction.deleteObjectSet(index);
            response.status(204);
            return "";
        });
    }
}

ServerTest.java

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

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

public class ServerTest {

    public static void main(String[] args) {
        // Start Unirest instance (optional if using newer versions)
        Unirest.config().defaultBaseUrl("http://localhost:8080");

        // Ensure your server is running before executing this main method
        try {
            testGetObjectSet();
            //testPostObjectSet();
            //testDeleteObjectSet();
        } finally {
            Unirest.shutDown();
        }
    }

    private static void testGetObjectSet() {
        System.out.println("Testing GET /objectset/:index");
        HttpResponse<String> response = Unirest.get("/objectset/1").asString();
        if (response.getStatus() == 200) {
            System.out.println("Success: " + response.getBody());
        } else {
            System.out.println("Failed: HTTP status " + response.getStatus());
        }
    }

//    private static void testPostObjectSet() {
//        System.out.println("Testing POST /objectset/:index");
//        String json = "{\"objects\":[{\"points\":[{\"x\":1,\"y\":2,\"z\":3},{\"x\":4,\"y\":5,\"z\":6}]}]}";
//        HttpResponse<String> response = Unirest.post("/objectset/2")
//                .header("Content-Type", "application/json")
//                .body(json)
//                .asString();
//        if (response.getStatus() == 200) {
//            System.out.println("Success: " + response.getBody());
//        } else {
//            System.out.println("Failed: HTTP status " + response.getStatus());
//        }
//    }
//
//    private static void testDeleteObjectSet() {
//        System.out.println("Testing DELETE /objectset/:index");
//        HttpResponse<String> response = Unirest.delete("/objectset/1").asString();
//        if (response.getStatus() == 204) {
//            System.out.println("Success: ObjectSet deleted successfully.");
//        } else {
//            System.out.println("Failed: HTTP status " + response.getStatus());
//        }
//    }
}
Loading