Skip to content

Commit

Permalink
Merge pull request #29 from hotungkhanh/kan-74/timetable-deletion
Browse files Browse the repository at this point in the history
KAN-74 feat(timetables) add deletion logic and uuid
  • Loading branch information
FlyingPufferFish authored Oct 4, 2024
2 parents 741914b + 444b3ab commit e844717
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
29 changes: 24 additions & 5 deletions backend/src/main/java/org/acme/TimetableResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.concurrent.ExecutionException;

import java.util.UUID;

/**
* Entry to the timetabling program.
* Receives a timetabling problem and outputs the solution
Expand All @@ -33,13 +35,16 @@ public class TimetableResource {
@Inject
SolverManager<Timetable, String> solverManager;

private int jobId = 0;

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

System.out.println("Your UUID is: " + uuidAsString);
String name = "Job" + uuidAsString;

findByCampusAndDelete(problem.campusName);

// generate solution timetable with TimeFold Solver
Timetable solution = solverManager.solve(name, problem).getFinalBestSolution();
Expand All @@ -64,6 +69,17 @@ public Unit handleUnit(Unit unit) {
return unit;
}

public void findByCampusAndDelete(String campusName) {
List<Timetable> timetables = Timetable.listAll();
for (Timetable timetable : timetables) {
System.out.println("CHECKING NOW\n");
if (campusName.equals(timetable.campusName)) {
System.out.println("SMTH HAS BEEN DELETED WOOOO\n");
timetable.delete();
}
}
}

@GET
@Transactional
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -88,7 +104,7 @@ public Timetable solveExample() throws ExecutionException, InterruptedException
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(
var problem = new Timetable("Campus A",
List.of(
u1, u2, u3, u4
// new Unit(5, "5", Duration.ofHours(2), List.of(c, d, e)),
Expand Down Expand Up @@ -126,6 +142,9 @@ public Timetable solveExample() throws ExecutionException, InterruptedException
* timetable assignment, while the 'new' Unit does not have the list
* of students enrolled, but does have the assigned date and room
*/

findByCampusAndDelete(problem.campusName);

Timetable solution = solverManager.solve("job 1", problem).getFinalBestSolution();

solution.persist();
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/org/acme/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Room extends PanacheEntity {
* A list of timetables that the Room is a part of
*/
@JsonIgnoreProperties("rooms")
@ManyToMany(mappedBy = "rooms", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@ManyToMany(mappedBy = "rooms", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/org/acme/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Student extends PanacheEntity{
public String name;

@JsonIgnoreProperties("students")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "student_unit",
joinColumns = @JoinColumn(name = "student_id"),
Expand Down
13 changes: 8 additions & 5 deletions backend/src/main/java/org/acme/domain/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Timetable extends PanacheEntity {
* campus, hence the many-to-many relationship
*/
@JsonIgnoreProperties("timetables")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "room_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
Expand All @@ -66,7 +66,7 @@ public class Timetable extends PanacheEntity {
* multiple campuses, so may appear in multiple timetables
*/
@JsonIgnoreProperties("timetables")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "unit_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
Expand All @@ -88,7 +88,8 @@ public Timetable() {
* @param units The list of units to be allocated.
* @param startTimes The list of available starting times.
*/
public Timetable(List<Unit> units, List<LocalTime> startTimes) {
public Timetable(String campusName, List<Unit> units, List<LocalTime> startTimes) {
this.campusName = campusName;
this.units = units;
this.startTimes = startTimes;
this.setUnitTimetable();
Expand All @@ -101,7 +102,8 @@ public Timetable(List<Unit> units, List<LocalTime> startTimes) {
* @param startTimes The list of available starting times.
* @param rooms The list of available rooms.
*/
public Timetable(List<Unit> units, List<LocalTime> startTimes, List<Room> rooms) {
public Timetable(String campusName, List<Unit> units, List<LocalTime> startTimes, List<Room> rooms) {
this.campusName = campusName;
this.units = units;
this.startTimes = startTimes;
this.rooms = rooms;
Expand All @@ -117,7 +119,8 @@ public Timetable(List<Unit> units, List<LocalTime> startTimes, List<Room> rooms)
* @param startTimes The list of available starting times.
* @param rooms The list of available rooms.
*/
public Timetable(List<Unit> units, List<DayOfWeek> daysOfWeek, List<LocalTime> startTimes, List<Room> rooms) {
public Timetable(String campusName, List<Unit> units, List<DayOfWeek> daysOfWeek, List<LocalTime> startTimes, List<Room> rooms) {
this.campusName = campusName;
this.units = units;
this.daysOfWeek = daysOfWeek;
this.startTimes = startTimes;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main/java/org/acme/domain/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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})
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<Student> students;

/*
Expand All @@ -57,7 +57,7 @@ public class Unit extends PanacheEntity {
* etc.
*/
@JsonIgnoreProperties("units")
@ManyToOne(cascade = {CascadeType.ALL})
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "room_id")
@PlanningVariable
public Room room;
Expand All @@ -68,7 +68,7 @@ public class Unit extends PanacheEntity {
* The timetables that the Unit object belongs to
*/
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
public List<Timetable> timetables = new ArrayList<Timetable>();

Expand Down

0 comments on commit e844717

Please sign in to comment.