Skip to content

Commit

Permalink
Merge pull request #26 from hotungkhanh/kan-71/db-schema
Browse files Browse the repository at this point in the history
Kan 71 Fix database annotations to allow JSON deserialization
  • Loading branch information
FlyingPufferFish authored Sep 30, 2024
2 parents cea6f7a + 2d4a8c0 commit 7fd470f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 103 deletions.
22 changes: 18 additions & 4 deletions backend/src/main/java/org/acme/TimetableResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ai.timefold.solver.core.api.solver.SolverManager;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
Expand Down Expand Up @@ -35,11 +36,17 @@ public class TimetableResource {
private int jobId = 0;

@POST
@Transactional
public Timetable handleRequest(Timetable problem) throws ExecutionException, InterruptedException {
jobId += 1;
String name = "Job" + Integer.toString(jobId);

// generate solution timetable with TimeFold Solver
Timetable solution = solverManager.solve(name, problem).getFinalBestSolution();

// store the solution timetable to the database
solution.persist();

return solution;
}

Expand All @@ -50,6 +57,13 @@ public List<Timetable> view() {
return Timetable.listAll();
}

@Path("/unit")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Unit handleUnit(Unit unit) {
return unit;
}

@GET
@Transactional
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -69,10 +83,10 @@ public Timetable solveExample() throws ExecutionException, InterruptedException
Room r2 = new Room("Room2", 4, false);
Room r3 = new Room("Room3", 4, false);

Unit u1 = new Unit(1, "1", Duration.ofHours(2), List.of(a, b), true);
Unit u2 = new Unit(2, "2", Duration.ofHours(2), List.of(a, c, d, e), true);
Unit u3 = new Unit(3, "3", Duration.ofHours(2), List.of(f, g, h, i), false);
Unit u4 = new Unit(4, "4", Duration.ofHours(2), List.of(a, b), false);
Unit u1 = new Unit(1, "1", "Course A", Duration.ofHours(2), List.of(a, b), true);
Unit u2 = new Unit(2, "2", "Course A", Duration.ofHours(2), List.of(a, c, d, e), true);
Unit u3 = new Unit(3, "3", "Course B", Duration.ofHours(2), List.of(f, g, h, i), false);
Unit u4 = new Unit(4, "4", "Course C", Duration.ofHours(2), List.of(a, b), false);

var problem = new Timetable(
List.of(
Expand Down
19 changes: 0 additions & 19 deletions backend/src/main/java/org/acme/domain/Campus.java

This file was deleted.

30 changes: 0 additions & 30 deletions backend/src/main/java/org/acme/domain/CampusResource.java

This file was deleted.

4 changes: 1 addition & 3 deletions backend/src/main/java/org/acme/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import ai.timefold.solver.core.api.domain.lookup.PlanningId;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
Expand All @@ -22,6 +21,7 @@
*/
@Entity
public class Room extends PanacheEntity {

@PlanningId
public String roomCode;

Expand All @@ -36,7 +36,6 @@ public class Room extends PanacheEntity {
*/
@JsonIgnoreProperties("room")
@OneToMany(mappedBy = "room", orphanRemoval = false)
@JsonManagedReference
@JsonIgnore
public List<Unit> units = new ArrayList<Unit>();

Expand All @@ -45,7 +44,6 @@ public class Room extends PanacheEntity {
*/
@JsonIgnoreProperties("rooms")
@ManyToMany(mappedBy = "rooms", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JsonManagedReference
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand Down
4 changes: 0 additions & 4 deletions backend/src/main/java/org/acme/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
Expand All @@ -24,8 +23,6 @@
@Entity
public class Student extends PanacheEntity{

// String studentID;

public String name;

@JsonIgnoreProperties("students")
Expand All @@ -35,7 +32,6 @@ public class Student extends PanacheEntity{
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
@JsonManagedReference
@JsonIgnore
public List<Unit> units = new ArrayList<Unit>();

Expand Down
13 changes: 6 additions & 7 deletions backend/src/main/java/org/acme/domain/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Transient;

import java.time.DayOfWeek;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents a timetable, the solution from the program.
Expand All @@ -33,6 +31,9 @@
@Entity
@PlanningSolution
public class Timetable extends PanacheEntity {

public String campusName;

@ElementCollection
@ValueRangeProvider
public List<DayOfWeek> daysOfWeek;
Expand All @@ -54,9 +55,8 @@ public class Timetable extends PanacheEntity {
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "room_id")
)
@JsonManagedReference
@ProblemFactCollectionProperty
@JsonIgnore
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@ValueRangeProvider
public List<Room> rooms;

Expand All @@ -72,7 +72,6 @@ public class Timetable extends PanacheEntity {
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
@JsonManagedReference
@PlanningEntityCollectionProperty
public List<Unit> units;

Expand Down Expand Up @@ -187,7 +186,7 @@ public List<ConflictingUnit> calculateSoftUnitConflictList() {
ArrayList<ConflictingUnit> out = new ArrayList<ConflictingUnit>();
for (var first : units) {
for (var second : units) {
if (first.getUnitID() >= second.getUnitID()) {
if (first.getUnitId() >= second.getUnitId()) {
continue;
}
int numStudents = 0;
Expand Down
51 changes: 27 additions & 24 deletions backend/src/main/java/org/acme/domain/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;


/**
* Represents a unit.
Expand All @@ -31,17 +29,13 @@
@PlanningEntity
public class Unit extends PanacheEntity {

// TODO: change unit to be the owner, rather than the student being owner
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JsonManagedReference
public List<Student> students;

@PlanningId
public int unitID;
public int unitId;

public String name;

public String course;

public Duration duration;

@PlanningVariable
Expand All @@ -50,6 +44,11 @@ public class Unit extends PanacheEntity {
@PlanningVariable
public LocalTime startTime;

// TODO: change unit to be the owner, rather than the student being owner
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
public List<Student> students;

/*
* currently each unit only has 1 'slot' on the timetable, so it can only
* be associated with one room, but in the final product, we would most
Expand All @@ -60,7 +59,6 @@ public class Unit extends PanacheEntity {
@JsonIgnoreProperties("units")
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "room_id")
@JsonManagedReference
@PlanningVariable
public Room room;

Expand All @@ -71,7 +69,6 @@ public class Unit extends PanacheEntity {
*/
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JsonManagedReference
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand All @@ -82,13 +79,15 @@ public Unit() {
* Creates a unit.
*
* @param unitID The unit’s ID.
* @param name The unit’s ID.
* @param name The unit’s name.
* @param course The course that the unit belongs to.
* @param duration The unit’s duration.
* @param students The list of students enrolled in the unit.
*/
public Unit(int unitID, String name, Duration duration, List<Student> students) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, Duration duration, List<Student> students) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.duration = duration;
this.students = students;
this.setStudentsUnits();
Expand All @@ -98,14 +97,16 @@ public Unit(int unitID, String name, Duration duration, List<Student> students)
* Creates a unit.
*
* @param unitID The unit’s ID.
* @param name The unit’s ID.
* @param name The unit’s name.
* @param course The course that the unit belongs to.
* @param duration The unit’s duration.
* @param students The list of students enrolled in the unit.
* @param wantsLab Whether the unit wants a laboratory room.
*/
public Unit(int unitID, String name, Duration duration, List<Student> students, boolean wantsLab) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, Duration duration, List<Student> students, boolean wantsLab) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.duration = duration;
this.students = students;
this.wantsLab = wantsLab;
Expand All @@ -116,15 +117,17 @@ public Unit(int unitID, String name, Duration duration, List<Student> students,
* Creates a unit.
*
* @param unitID The unit’s ID.
* @param name The unit’s ID.
* @param name The unit’s name.
* @param course The course that the unit belongs to.
* @param duration The unit’s duration.
* @param students The list of students enrolled in the unit.
* @param wantsLab Whether the unit wants a laboratory room.
* @param room The unit's room.
*/
public Unit(int unitID, String name, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List<Student> students, boolean wantsLab, Room room) {
this.unitID = unitID;
public Unit(int unitID, String name, String course, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List<Student> students, boolean wantsLab, Room room) {
this.unitId = unitID;
this.name = name;
this.course = course;
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.duration = duration;
Expand All @@ -133,12 +136,12 @@ public Unit(int unitID, String name, DayOfWeek dayOfWeek, LocalTime startTime, D
this.room = room;
}

public int getUnitID() {
return unitID;
public int getUnitId() {
return unitId;
}

public void setUnitID(int unitID) {
this.unitID = unitID;
public void setUnitId(int unitID) {
this.unitId = unitID;
}

public String getName() {
Expand Down
Loading

0 comments on commit 7fd470f

Please sign in to comment.