-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from ConnorMarcus/report-upload-student
Report upload student
- Loading branch information
Showing
22 changed files
with
906 additions
and
11 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/sysc4806/project/controllers/DeadlineController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/main/java/sysc4806/project/controllers/DownloadController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/sysc4806/project/controllers/ReportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/sysc4806/project/repositories/ReportFileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.