From 5e9eccc4f6c52a71d2939b6355bb6aaee2655d01 Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Thu, 11 Apr 2024 00:24:40 +0200 Subject: [PATCH] Researches EndPoints --- .../ResearchController.java | 95 ++++++++++++++++ .../Clyde/EndPoints/UserController.java | 2 +- .../ResearchCoAuthorsRepository.java | 14 +++ .../ResearchRepository.java | 9 ++ .../ResearcherRepository.java | 9 ++ .../ResearchesService.java | 106 ++++++++++++++++++ .../ScientificPublications/Research.java | 16 ++- .../ResearchCoAuthors.java | 2 + 8 files changed, 250 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java new file mode 100644 index 0000000..c3bdf9b --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java @@ -0,0 +1,95 @@ +package ovh.herisson.Clyde.EndPoints.ScientificPublications; + +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import ovh.herisson.Clyde.Responses.UnauthorizedResponse; +import ovh.herisson.Clyde.Services.AuthenticatorService; +import ovh.herisson.Clyde.Services.ScientificPublications.ResearchesService; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; + +import java.util.Map; + +@RestController +@CrossOrigin(originPatterns = "*", allowCredentials = "true") +@AllArgsConstructor +public class ResearchController { + + + + private final ResearchesService researchesServ; + + private final AuthenticatorService authServ; + + /** Is accessible by anyone + * but if the user doesn't have the permission to download the research + * the return Research Download URL will be null + */ + @GetMapping("/research/{id}") + public ResponseEntity getResearch(@RequestHeader("Authorization") String token, @PathVariable long id){ + + Research research = researchesServ.getResearchById(id); + + if (research == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + if (researchesServ.hasNoAccessTo(research,authServ.getUserFromToken(token))){ + research.setPdfLocation(null); + }// If the user doesn't have access to the document he can't download the pdf + + return new ResponseEntity<>(research, HttpStatus.OK); + } + + @PostMapping("/research") + public ResponseEntity postResearch(@RequestHeader("Authorization") String token, @RequestBody Research research){ + + if (authServ.isNotIn(new Role[]{Role.Admin},token) && + researchesServ.getResearcherByUser(authServ.getUserFromToken(token)) == null){ + return new UnauthorizedResponse<>(null); + } // if the poster isn't a researcher + + return new ResponseEntity<>(researchesServ.saveResearch(research), HttpStatus.OK); + } + + @PatchMapping("/research/{id}") + public ResponseEntity patchResearch(@RequestHeader("Authorization") String token, + @RequestBody Map updates, + @PathVariable long id + ) + { + Research research = researchesServ.getResearchById(id); + Researcher researcher = researchesServ.getResearcherByUser(authServ.getUserFromToken(token)); + + if (research == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary},token) && + researcher != research.getAuthor()) { + return new UnauthorizedResponse<>(null); + } + + researchesServ.modifyResearchData(research, updates); + return new ResponseEntity<>(HttpStatus.OK); + } + + @DeleteMapping("/research/{id}") + public ResponseEntity deleteResearch(@RequestHeader("Authorization") String token, @PathVariable long id){ + + Research research = researchesServ.getResearchById(id); + Researcher researcher = researchesServ.getResearcherByUser(authServ.getUserFromToken(token)); + + if (research == null) + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + + if (authServ.isNotIn(new Role[]{Role.Admin, Role.Secretary},token) && + researcher != research.getAuthor()){ + return new UnauthorizedResponse<>(null); + } + + researchesServ.delete(research); + return new ResponseEntity<>(HttpStatus.OK); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 20ca9ee..64eec4a 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -96,7 +96,7 @@ public class UserController { if (poster == null) {return new UnauthorizedResponse<>("bad token");} if (!userService.modifyData(id, updates, poster)) - return new UnauthorizedResponse<>("there was an issue with the updates requested"); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(null, HttpStatus.OK); } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java new file mode 100644 index 0000000..70eb16e --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java @@ -0,0 +1,14 @@ +package ovh.herisson.Clyde.Repositories.ScientificPublications; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.Course; +import ovh.herisson.Clyde.Tables.Curriculum; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; +import ovh.herisson.Clyde.Tables.ScientificPublications.ResearchCoAuthors; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; + +public interface ResearchCoAuthorsRepository extends CrudRepository { + @Query("select distinct rca.coAuthor from ResearchCoAuthors rca where rca.research = ?1") + public Iterable findResearchCoAuthors(Research research); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java new file mode 100644 index 0000000..09d74ad --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchRepository.java @@ -0,0 +1,9 @@ +package ovh.herisson.Clyde.Repositories.ScientificPublications; + +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; + +public interface ResearchRepository extends CrudRepository { + + public Research findById(long id); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java new file mode 100644 index 0000000..3cb2f9d --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearcherRepository.java @@ -0,0 +1,9 @@ +package ovh.herisson.Clyde.Repositories.ScientificPublications; + +import org.springframework.data.repository.CrudRepository; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; +import ovh.herisson.Clyde.Tables.User; + +public interface ResearcherRepository extends CrudRepository { + public Researcher findByUser(User user); +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java new file mode 100644 index 0000000..e7ca875 --- /dev/null +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java @@ -0,0 +1,106 @@ +package ovh.herisson.Clyde.Services.ScientificPublications; + +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; +import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearchCoAuthorsRepository; +import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearchRepository; +import ovh.herisson.Clyde.Repositories.ScientificPublications.ResearcherRepository; +import ovh.herisson.Clyde.Tables.Role; +import ovh.herisson.Clyde.Tables.ScientificPublications.Access; +import ovh.herisson.Clyde.Tables.ScientificPublications.PaperType; +import ovh.herisson.Clyde.Tables.ScientificPublications.Research; +import ovh.herisson.Clyde.Tables.ScientificPublications.Researcher; +import ovh.herisson.Clyde.Tables.User; + +import java.util.Date; +import java.util.Map; + +@Service +@AllArgsConstructor +public class ResearchesService { + + private final ResearcherRepository researcherRepo; + private final ResearchCoAuthorsRepository researchCoAuthorsRepo; + private final ResearchRepository articleRepo; + + // Researchers Part + public Researcher getResearcherByUser(User user){ + return researcherRepo.findByUser(user); + } + + public boolean isCoAuthor(Research research, Researcher researcher){ + Iterable coAuthors = researchCoAuthorsRepo.findResearchCoAuthors(research); + for (Researcher coAuthor : coAuthors){ + if (researcher == coAuthor){ + return true; + } + } + return false; + } + + + // researches Part + public Research getResearchById(long id) { + return articleRepo.findById(id); + } + + public boolean hasNoAccessTo(Research research, User user){ + + if (research.getAccess() == Access.OpenSource) return false; // if the access is open source even non-users can see it + if (user == null) return true; // else you need at least to be a user + + if (research.getAccess() == Access.Restricted && + user.getRole() == Role.Admin || user.getRole() == Role.Secretary || + user.getRole() == Role.Teacher || user.getRole() == Role.InscriptionService) + return false; // if the access is restricted only the staff member (above) can access the research + + Researcher researcher = getResearcherByUser(user); + + if (researcher==null) + return true; + + return (research.getAccess() != Access.Private || research.getAuthor() != researcher) && + (research.getAccess() != Access.Private || !isCoAuthor(research, researcher)); + // if the researcher is the author or one of the co-authors of the research will return false + } + + public Research saveResearch(Research research) { + return articleRepo.save(research); + } + + public void modifyResearchData(Research research, Map updates) { + for (Map.Entry entry : updates.entrySet()){ + switch (entry.getKey()){ + case "title": + research.setTitle((String) entry.getValue()); + break; + case "date": + research.setReleaseDate((Date) entry.getValue()); + break; + case "paperType": + research.setPaperType((PaperType) entry.getValue()); + break; + case "PdfLocation": + research.setPdfLocation((String) entry.getValue()); + break; + case "bibtexLocation": + research.setBibTexLocation((String) entry.getValue()); + break; + case "language": + research.setLanguage((String) entry.getValue()); + break; + case "domain": + research.setDomain((String) entry.getValue()); + break; + case "summary": + research.setSummary((String) entry.getValue()); + break; + } + } + articleRepo.save(research); + } + + public void delete(Research research) { + articleRepo.delete(research); + } +} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java index 2787f23..3d31056 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Research.java @@ -9,7 +9,6 @@ package ovh.herisson.Clyde.Tables.ScientificPublications; * ******************************************************/ import jakarta.persistence.*; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -20,7 +19,6 @@ import java.util.Date; @Getter @Setter @NoArgsConstructor -@AllArgsConstructor @Entity public class Research { @Id @@ -53,5 +51,19 @@ public class Research { private String summary; private int views; + + public Research(String title, Researcher author, Date releaseDate, PaperType paperType, + String pdfLocation, String bibTexLocation, String language, Access access, String domain, String summary){ + this.author = author; + this.title = title; + this.releaseDate = releaseDate; + this.paperType = paperType; + this.pdfLocation = pdfLocation; + this.bibTexLocation = bibTexLocation; + this.language = language; + this.access = access; + this.domain = domain; + this.summary = summary; + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java index 5d3b123..1b657ce 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java @@ -8,6 +8,7 @@ package ovh.herisson.Clyde.Tables.ScientificPublications; * Co-Authors List entity (will be accessed by Articles) * ******************************************************/ +import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; @@ -22,6 +23,7 @@ import org.hibernate.annotations.OnDeleteAction; @Setter @NoArgsConstructor @AllArgsConstructor +@Entity public class ResearchCoAuthors { @ManyToOne(fetch = FetchType.EAGER)