Skip to content

Commit

Permalink
Merge pull request #36 from ConnorMarcus/report-upload-student
Browse files Browse the repository at this point in the history
Report upload student
  • Loading branch information
ConnorMarcus authored Nov 24, 2023
2 parents 792a40f + eeae676 commit 289a188
Show file tree
Hide file tree
Showing 22 changed files with 906 additions and 11 deletions.
37 changes: 37 additions & 0 deletions src/main/java/sysc4806/project/controllers/DeadlineController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sysc4806.project.controllers;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sysc4806.project.models.Project;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import static sysc4806.project.util.AuthenticationHelper.PROFESSOR_ROLE;

@Controller
public class DeadlineController {

@GetMapping(path = "/deadlines")
@Secured(PROFESSOR_ROLE)
public String getDeadlinePage(Model model) {
model.addAttribute("reportDeadline", Project.getDeadline().getTime().toString());
return "deadlines";
}

@PostMapping(path = "/deadlines/setReport")
@Secured(PROFESSOR_ROLE)
public String setReportDeadline(@RequestParam("selectedDate") String deadline, Model model) throws ParseException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
cal.setTime(sdf.parse(deadline));
Project.setDeadline(cal);
model.addAttribute("reportDeadline", Project.getDeadline().getTime().toString());
return "deadlines";
}
}
47 changes: 47 additions & 0 deletions src/main/java/sysc4806/project/controllers/DownloadController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sysc4806.project.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sysc4806.project.models.ReportFile;
import sysc4806.project.models.Student;
import sysc4806.project.services.ApplicationUserService;
import java.io.IOException;

import static sysc4806.project.util.AuthenticationHelper.STUDENT_ROLE;

@RestController
public class DownloadController {
@Autowired
private ApplicationUserService userService;

@RequestMapping(path = "/downloadReport", method = RequestMethod.GET)
@Secured(STUDENT_ROLE)
public ResponseEntity<Resource> studentDownloadReport() throws IOException {
Student user = (Student) userService.getCurrentUser();
if (user.getProject() != null && user.getProject().getReport() != null) {
ReportFile reportFile = user.getProject().getReport();
ByteArrayResource resource = new ByteArrayResource(reportFile.getFileData());

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + reportFile.getFileName());
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return ResponseEntity.ok()
.headers(headers)
.contentLength(reportFile.getFileData().length)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);

}

return ResponseEntity.badRequest().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import sysc4806.project.services.ApplicationUserService;
import java.util.List;


import static sysc4806.project.util.AuthenticationHelper.*;

@Controller
Expand Down Expand Up @@ -67,7 +66,8 @@ public String viewProjectsPage(Model model) throws Exception {
return "viewProjects";
}

@DeleteMapping( "/deleteProject/{projectId}")
@DeleteMapping(path = "/deleteProject/{projectId}")
@Secured(PROFESSOR_ROLE)
public String deleteProject(@PathVariable Long projectId) {
log.info(String.format("DELETE project request received for id: %s", projectId));
Project project = projectRepository.findById(projectId).orElse(null);
Expand All @@ -82,7 +82,8 @@ public String deleteProject(@PathVariable Long projectId) {
}
}

@PatchMapping("/project/{projectId}/addStudent/{userId}")
@PatchMapping(path = "/project/{projectId}/addStudent/{userId}")
@Secured(STUDENT_ROLE)
public String addStudentToProject(@PathVariable Long projectId, @PathVariable Long userId) {
log.info("add student to project PATCH request received");
Project project = projectRepository.findById(projectId).orElse(null);
Expand All @@ -98,7 +99,8 @@ public String addStudentToProject(@PathVariable Long projectId, @PathVariable Lo
return "error";
}

@PatchMapping("/project/{projectId}/removeStudent/{userId}")
@PatchMapping(path = "/project/{projectId}/removeStudent/{userId}")
@Secured(STUDENT_ROLE)
public String removeStudentFromProject(@PathVariable Long projectId, @PathVariable Long userId) {
log.info("remove student from project PATCH request received");
Project project = projectRepository.findById(projectId).orElse(null);
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/sysc4806/project/controllers/ReportController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package sysc4806.project.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import sysc4806.project.models.Project;
import sysc4806.project.models.ReportFile;
import sysc4806.project.models.Student;
import sysc4806.project.services.ApplicationUserService;
import sysc4806.project.services.ReportService;
import sysc4806.project.util.AuthenticationHelper;

import java.util.Date;

import static sysc4806.project.util.AuthenticationHelper.STUDENT_ROLE;

@Controller
public class ReportController {
@Autowired
private ReportService reportService;

@Autowired
private ApplicationUserService userService;

@GetMapping(path = "/studentReportUpload")
@Secured(STUDENT_ROLE)
public String uploadReportPage(Model model) {
Student user = (Student) userService.getCurrentUser();
model.addAttribute("deadline", Project.getDeadline().getTime().toString());
model.addAttribute("beforeDeadline", isBeforeDeadline());
if (user.getProject() != null) {
ReportFile report = user.getProject().getReport();
model.addAttribute("inProject", true);
model.addAttribute("reportName", report == null ? "" : report.getFileName());
}
else {
model.addAttribute("inProject", false);
}

return "studentReportUpload";
}

@PostMapping(path = "/studentReportUpload")
@Secured(STUDENT_ROLE)
public String uploadReport(@RequestParam("file") MultipartFile file, Model model) throws Exception {
Student user = (Student) userService.getCurrentUser();
if (user.getProject() != null) {
model.addAttribute("deadline", Project.getDeadline());
boolean beforeDeadline = isBeforeDeadline();
if (beforeDeadline) {
try {
reportService.saveReport(user.getProject(), file);
}
catch (Exception e) {
model.addAttribute("uploadError", "There was an error uploading the file!");
}
model.addAttribute("reportName", file.getOriginalFilename());
}
model.addAttribute("inProject", true);
model.addAttribute("beforeDeadline", beforeDeadline);
return "studentReportUpload";
}

model.addAttribute("userType", STUDENT_ROLE);
model.addAttribute("userName", user.getName());
return "redirect:/home"; // Redirect to home page if project is null
}

protected boolean isBeforeDeadline() {
return new Date().before(Project.getDeadline().getTime());
}

}
42 changes: 40 additions & 2 deletions src/main/java/sysc4806/project/models/Project.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package sysc4806.project.models;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;

import java.util.*;

/**
* Project model
Expand All @@ -27,6 +27,11 @@ public class Project {
@ManyToOne
private Professor professor;

@OneToOne
private ReportFile report;

private static Calendar deadline = new GregorianCalendar(2023, Calendar.DECEMBER, 8);

public Project() {
this.students = new ArrayList<>();
this.restrictions = new ArrayList<>();
Expand Down Expand Up @@ -159,4 +164,37 @@ public void addStudent(Student student) {
public void removeStudent(Student student) {
students.remove(student);
}


/**
* Getter for the report
* @return byte array representing the report
*/
public ReportFile getReport() {
return report;
}

/**
* Setter for the report
* @param report the report thats being set
*/
public void setReport(ReportFile report) {
this.report = report;
}

/**
* Setter for the deadline
* @return The deadline of the project
*/
public static Calendar getDeadline() {
return deadline;
}

/**
* Getter for the deadline
* @param deadline The deadline of the project
*/
public static void setDeadline(Calendar deadline) {
Project.deadline = deadline;
}
}
83 changes: 83 additions & 0 deletions src/main/java/sysc4806/project/models/ReportFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package sysc4806.project.models;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;


@Entity
public class ReportFile {

private String fileName;

@Lob
private byte[] fileData;

@Id
@GeneratedValue
private Long id;

/**
* Constructor for ReportFile
* @param fileName String filename
* @param fileData byte[] data of file
*/
public ReportFile(String fileName, byte[] fileData) {
this.fileName = fileName;
this.fileData = fileData;
}

/**
* Default constructor
*/
public ReportFile() {}

/**
* Gets the filename
* @return String file name
*/
public String getFileName() {
return fileName;
}

/**
* Sets the filename
* @param fileName String filename to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}

/**
* Gets the filedata
* @return byte[] the file data
*/
public byte[] getFileData() {
return fileData;
}

/**
* Sets the filedata
* @param fileData byte[] filedata to set
*/
public void setFileData(byte[] fileData) {
this.fileData = fileData;
}

/**
* Sets the id
* @param id Long id to set
*/
public void setId(Long id) {
this.id = id;
}

/**
* Gets the id
* @return Long the id
*/
public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sysc4806.project.repositories;

import org.springframework.data.repository.CrudRepository;
import sysc4806.project.models.ReportFile;

public interface ReportFileRepository extends CrudRepository<ReportFile, Long> {
public ReportFile findReportFileById(Long id);
}
Loading

0 comments on commit 289a188

Please sign in to comment.