master #173
@ -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<Research> 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<Research> 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<String> patchResearch(@RequestHeader("Authorization") String token,
|
||||
@RequestBody Map<String,Object> 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<String> 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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<ResearchCoAuthors,Long> {
|
||||
@Query("select distinct rca.coAuthor from ResearchCoAuthors rca where rca.research = ?1")
|
||||
public Iterable<Researcher> findResearchCoAuthors(Research research);
|
||||
}
|
@ -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<Research,Long> {
|
||||
|
||||
public Research findById(long id);
|
||||
}
|
@ -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<Researcher,Long> {
|
||||
public Researcher findByUser(User user);
|
||||
}
|
@ -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<Researcher> 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<String, Object> updates) {
|
||||
for (Map.Entry<String, Object> 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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user