From 54d19eb888b98362ba15fc5d9323570e8e07edfa Mon Sep 17 00:00:00 2001 From: Bartha Maxime <231026@umons.ac.be> Date: Sat, 20 Apr 2024 19:07:27 +0200 Subject: [PATCH] added Co Author --- backend/build.gradle.kts | 2 +- .../ScientificPublications/ResearchDTO.java | 12 +- .../Clyde/EndPoints/MockController.java | 19 +- .../ResearchController.java | 76 +------- .../ResearchCoAuthorsRepository.java | 18 -- .../ResearchesService.java | 165 +++++++++--------- .../ScientificPublications/Research.java | 14 +- .../ResearchCoAuthors.java | 42 ----- .../ScientificPublications/Researcher.java | 1 + .../ScientificPublications/ListResearches.vue | 19 +- .../ResearchComponent.vue | 20 ++- .../ResearchPostComponent.vue | 35 +++- .../ResearcherProfile.vue | 9 +- .../ScientificPublications/ManageResearch.js | 5 +- 14 files changed, 186 insertions(+), 251 deletions(-) delete mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java delete mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index 8fe2246..794631f 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -27,7 +27,7 @@ dependencies { implementation("com.kohlschutter.junixsocket:junixsocket-core:2.9.0") // implementation("org.springframework.session:spring-session-jdbc") developmentOnly("org.springframework.boot:spring-boot-devtools") - // developmentOnly("org.springframework.boot:spring-boot-docker-compose") + developmentOnly("org.springframework.boot:spring-boot-docker-compose") runtimeOnly("org.postgresql:postgresql") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.boot:spring-boot-testcontainers") diff --git a/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java index d56372b..5c8f2b3 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java +++ b/backend/src/main/java/ovh/herisson/Clyde/DTO/ScientificPublications/ResearchDTO.java @@ -12,8 +12,11 @@ import lombok.Data; 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 java.util.Date; +import java.util.HashSet; +import java.util.Set; @Data public class ResearchDTO { @@ -21,6 +24,7 @@ public class ResearchDTO { private long id; private String title; private ResearcherDTO researcher; + private final Set coAuthors; private Date releaseDate; private PaperType paperType; private String pdfLocation; @@ -31,7 +35,7 @@ public class ResearchDTO { private String summary; private long views; - private ResearchDTO(String title, ResearcherDTO researcherDTO, Date releaseDate, PaperType paperType, String pdfLocation, String language, Access access, String domain,String bibTexLocation, String summary, long id,long views) { + private ResearchDTO(String title, ResearcherDTO researcherDTO, Date releaseDate, PaperType paperType, String pdfLocation, String language, Access access, String domain, String bibTexLocation, String summary, Set coAuthors, long id, long views) { this.title = title; this.researcher = researcherDTO; this.releaseDate = releaseDate; @@ -43,6 +47,10 @@ public class ResearchDTO { this.summary = summary; this.id = id; this.bibTexLocation = bibTexLocation; + this.coAuthors = new HashSet<>(); + for (Researcher coAuthor: coAuthors) { + this.coAuthors.add(ResearcherDTO.construct(coAuthor)); + } this.views = views; } @@ -50,6 +58,6 @@ public class ResearchDTO { public static ResearchDTO construct(Research research){ return new ResearchDTO(research.getTitle(), ResearcherDTO.construct(research.getAuthor()), research.getReleaseDate(), research.getPaperType(),research.getPdfLocation(),research.getLanguage(),research.getAccess(), - research.getDomain(),research.getBibTexLocation(), research.getSummary(), research.getId(), research.getViews()); + research.getDomain(),research.getBibTexLocation(), research.getSummary(), research.getCoAuthors(),research.getId(), research.getViews()); } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index ed49140..ad6f18f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -19,9 +19,7 @@ import ovh.herisson.Clyde.Tables.Inscription.InscriptionRequest; import ovh.herisson.Clyde.Tables.Inscription.Minerval; import ovh.herisson.Clyde.Tables.Inscription.ScholarshipRequest; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; +import java.util.*; @RestController @CrossOrigin(originPatterns = "*", allowCredentials = "true") @@ -138,24 +136,29 @@ public class MockController { // extension Publications Scientifiques Researcher jojoResearcherAccount = new Researcher(jojo, "3363-22555-AB33-T", null, "IT"); + Researcher joResearchAccount = new Researcher(joe,"N555-321213-BED-DD",null, "Physics"); + + Researcher output = researchesService.saveResearcher(jojoResearcherAccount); + Researcher joOutput = researchesService.saveResearcher(joResearchAccount); + + Set coAuthor = new HashSet<>(); + coAuthor.add(joOutput); Research jojoResearch = new Research("Graphs : Advanced Search Algorithms", output, new Date(0), PaperType.Article, "test.pdf", null, "english", - Access.OpenSource, "IT", "This Article's title speaks for itself \n We'll discuss about advanced Graph search Algorithms"); - + Access.OpenSource, "IT", "This Article's title speaks for itself \n We'll discuss about advanced Graph search Algorithms",coAuthor); Research restrictedResearch = new Research("just another Name", output, new Date(1111111111), PaperType.Article, "restricted", null, "english", - Access.Restricted, "Restricted", "This Article's title speaks for itself\n We'll discuss about advanced Graph search Algorithms"); + Access.Restricted, "Restricted", "This Article's title speaks for itself\n We'll discuss about advanced Graph search Algorithms", new HashSet<>()); Research privateResearch = new Research("the great Potato War", output, new Date(), PaperType.Article, "private", null, "english", - Access.Private, "private", "This Article's title speaks for itself\n We'll discuss about advanced Graph search Algorithms"); + Access.Private, "private", "This Article's title speaks for itself\n We'll discuss about advanced Graph search Algorithms",null); researchesService.saveResearch(restrictedResearch); researchesService.saveResearch(privateResearch); - researchesService.saveResearch(jojoResearch); } 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 index 3524d30..518147f 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/ScientificPublications/ResearchController.java @@ -108,6 +108,7 @@ public class ResearchController { } /** post updates to the research + * in the updates, the coAuthors have to be referenced by their ids * */ @PatchMapping("/research/{id}") @@ -152,81 +153,6 @@ public class ResearchController { return new ResponseEntity<>(HttpStatus.OK); } - - ///////////// - // Co-authors Part - - /** get all the co-authors in a research - * - * @param id the research id - * @return all the co-authors associated with the research - */ - @GetMapping("/research/{id}/co-authors") - public ResponseEntity> getCoAuthors(@PathVariable long id){ - Research research = researchesServ.getResearchById(id); - - if (research == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); - - Iterable researchers= researchesServ.getCoAuthors(research); - ArrayList toReturn = new ArrayList<>(); - - for (Researcher researcher: researchers){ - toReturn.add(ResearcherDTO.construct(researcher)); - } - return new ResponseEntity<>(toReturn,HttpStatus.OK); - } - - /** post co-authors to the research - * - * @param token the Authorization header - * @param researchersId the co-authors ids - * @param id the research id - * @return the added researchers - */ - @PostMapping("/research/{id}/co-authors") - public ResponseEntity> postCoAuthor(@RequestHeader("Authorization") String token, - @RequestBody Iterable researchersId, - @PathVariable long id) - { - Research research = researchesServ.getResearchById(id); - if (research == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); - - if (authServ.isNotIn(new Role[]{Role.Admin}, token) || research.getAuthor() != researchesServ.getResearcherByUser(authServ.getUserFromToken(token))) - return new UnauthorizedResponse<>(null); - - if (!researchesServ.saveCoAuthors(researchersId,research)) - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - - ArrayList toReturn = new ArrayList<>(); - for (Long reId: researchersId){ - toReturn.add(ResearcherDTO.construct(researchesServ.getResearcherById(reId))); - } - - return new ResponseEntity<>(toReturn,HttpStatus.OK); - } - - /** deletes the co-author with *coAuthorId* from the research with the *researchId* - */ - @DeleteMapping("/research/{researchId}/co-author/{coAuthorId}") - public ResponseEntity deleteCoAuthor(@RequestHeader("Authorization") String token, - @PathVariable long researchId, - @PathVariable long coAuthorId) - { - Research research = researchesServ.getResearchById(researchId); - Researcher coAuthor = researchesServ.getResearcherById(coAuthorId); - - if (authServ.isNotIn(new Role[]{Role.Admin}, token) || research.getAuthor() != researchesServ.getResearcherByUser(authServ.getUserFromToken(token))) - return new UnauthorizedResponse<>(null); - - if (coAuthor == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND); - - if (!researchesServ.deleteCoAuthor(research, coAuthor)) - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - - return new ResponseEntity<>(HttpStatus.OK); - } - - /////// //views part @PostMapping("/addview/cdn/{url}") 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 deleted file mode 100644 index 51f25fb..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/Repositories/ScientificPublications/ResearchCoAuthorsRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -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") - Iterable findResearchCoAuthors(Research research); - - - @Query("select rca from ResearchCoAuthors rca where rca.research = ?1 and rca.coAuthor =?2") - ResearchCoAuthors findResearchCoAuthors(Research research, Researcher researcher); -} 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 index 7b822f7..9916bd8 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/ScientificPublications/ResearchesService.java @@ -2,47 +2,33 @@ package ovh.herisson.Clyde.Services.ScientificPublications; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; -import ovh.herisson.Clyde.DTO.ScientificPublications.ResearchDTO; -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.*; import ovh.herisson.Clyde.Tables.User; -import java.util.ArrayList; -import java.util.Date; -import java.util.Map; +import java.util.*; @Service @AllArgsConstructor +@SuppressWarnings("unchecked") 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 Research getResearchByUrl(String url) { + return articleRepo.findByPdfLocation(url); + } + + public Iterable getResearchesByAuthor(long authorId){ Researcher researcher = researcherRepo.findById(authorId); if (researcher == null) return null; @@ -50,29 +36,6 @@ public class ResearchesService { return researcherRepo.findAllByAuthorId(researcher); } - 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 (user.getRole() == Role.Admin) return false; - - if (research.getAccess() == Access.Restricted && - 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); } @@ -98,6 +61,18 @@ public class ResearchesService { case "access": research.setAccess(Access.valueOf((String) entry.getValue())); break; + case "coAuthors": + Set set = new HashSet<>(); + + for (int id : (List) entry.getValue()) { + + Researcher r = researcherRepo.findById(id); + if (r != null){ + set.add(r); + } + } + research.setCoAuthors(set); + break; } } articleRepo.save(research); @@ -107,6 +82,12 @@ public class ResearchesService { articleRepo.delete(research); } + + // Researchers Part + public Researcher getResearcherByUser(User user){ + return researcherRepo.findByUser(user); + } + public Iterable getAllResearches() { return articleRepo.findAll(); } @@ -129,41 +110,6 @@ public class ResearchesService { researcherRepo.delete(researcher); } - public boolean saveCoAuthors(Iterable researchersId, Research research) { - - if (researchersId == null) return false; - - ArrayList toAdd = new ArrayList<>(); - for (long researcherId : researchersId){ - Researcher researcher= researcherRepo.findById(researcherId); - if (research== null){ - return false; - } - if (!toAdd.contains(researcher)) - { - toAdd.add(researcher); - } - } - for (Researcher researcher: toAdd){ - researchCoAuthorsRepo.save(new ResearchCoAuthors(researcher,research)); - } - return true; - } - - public Iterable getCoAuthors(Research research) { - return researchCoAuthorsRepo.findResearchCoAuthors(research); - } - - public boolean deleteCoAuthor(Research research,Researcher coAuthor) { - ResearchCoAuthors result = researchCoAuthorsRepo.findResearchCoAuthors(research,coAuthor); - - if (result ==null) - return false; - - researchCoAuthorsRepo.delete(result); - return true; - } - public void modifyResearcherData(Researcher researcher, Map updates) { for (Map.Entry entry : updates.entrySet()){ @@ -185,12 +131,69 @@ public class ResearchesService { researcherRepo.save(researcher); } - public Research getResearchByUrl(String url) { - return articleRepo.findByPdfLocation(url); + + //Co Author part + public boolean saveCoAuthors(Iterable researchersId, Research research) { + + if (researchersId == null) return false; + + ArrayList toAdd = new ArrayList<>(); + for (long researcherId : researchersId){ + Researcher researcher= researcherRepo.findById(researcherId); + if (researcher== null){ + return false; + } + if (!toAdd.contains(researcher)) + { + toAdd.add(researcher); + } + } + research.getCoAuthors().addAll(toAdd); + articleRepo.save(research); + return true; } + public boolean deleteCoAuthor(Research research,Researcher coAuthor) { + + if (!research.getCoAuthors().contains(coAuthor)) + return false; + + research.getCoAuthors().remove(coAuthor); + articleRepo.save(research); + return true; + } + + // Other stuff + public Research addView(Research research) { research.setViews(research.getViews()+1); return articleRepo.save(research); } + + 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 (user.getRole() == Role.Admin) return false; + + + Researcher researcher = getResearcherByUser(user); + if (researcher !=null ){ + if (research.getAuthor().getId() == researcher.getId()) + return false; + + for (Researcher coAuthor: research.getCoAuthors()){ + if (coAuthor.getId() == researcher.getId()) + return false; + } + } + + if (research.getAccess() == Access.Restricted && ( + 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 + + return true; + // if the researcher is the author or one of the co-authors of the research will return false + } } 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 f2fa8ff..da0d714 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 @@ -16,10 +16,14 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import java.util.Date; +import java.util.List; +import java.util.Set; + @Getter @Setter @NoArgsConstructor @Entity +@Table(name = "Research") public class Research { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -51,8 +55,15 @@ public class Research { private int views; + @ManyToMany(cascade = {CascadeType.MERGE}) + @JoinTable(name = "ResearchCoAuhors", + joinColumns = @JoinColumn(name = "research_id"), + inverseJoinColumns = @JoinColumn(name = "researcher_id") + ) + private Set coAuthors; + public Research(String title, Researcher author, Date releaseDate, PaperType paperType, - String pdfLocation, String bibTexLocation, String language, Access access, String domain, String summary){ + String pdfLocation, String bibTexLocation, String language, Access access, String domain, String summary,Set coauthors){ this.author = author; this.title = title; this.releaseDate = releaseDate; @@ -63,6 +74,7 @@ public class Research { this.access = access; this.domain = domain; this.summary = summary; + this.coAuthors = coauthors; } } 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 deleted file mode 100644 index dbc293d..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/ResearchCoAuthors.java +++ /dev/null @@ -1,42 +0,0 @@ -package ovh.herisson.Clyde.Tables.ScientificPublications; - -/****************************************************** - * @file ResearchCoAuthors - * @author Maxime Bartha - * @scope Extension Publications scientifiques - * - * Co-Authors List entity (will be accessed by Articles) - * - ******************************************************/ -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import org.hibernate.annotations.OnDelete; -import org.hibernate.annotations.OnDeleteAction; - -@Getter -@Setter -@NoArgsConstructor -@Entity -public class ResearchCoAuthors { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - Long id; - - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "Researcher") - private Researcher coAuthor; - - @ManyToOne(fetch = FetchType.EAGER) - @OnDelete(action = OnDeleteAction.CASCADE) - @JoinColumn(name = "Article") - private Research research; - - public ResearchCoAuthors(Researcher coAuthor, Research research){ - this.coAuthor = coAuthor; - this.research = research; - } -} diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java index 1da105c..4fe8a9d 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/ScientificPublications/Researcher.java @@ -20,6 +20,7 @@ import ovh.herisson.Clyde.Tables.User; @Setter @NoArgsConstructor @Entity +@Table(name = "Researcher") public class Researcher { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/frontend/src/Apps/ScientificPublications/ListResearches.vue b/frontend/src/Apps/ScientificPublications/ListResearches.vue index 8a04c15..0997383 100644 --- a/frontend/src/Apps/ScientificPublications/ListResearches.vue +++ b/frontend/src/Apps/ScientificPublications/ListResearches.vue @@ -106,14 +106,14 @@ const emit = defineEmits(["modified"]);
+
-
  • {{n.title}}
    @@ -128,6 +128,11 @@ const emit = defineEmits(["modified"]);