1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.CuratorDAO;
|
4
|
import eu.dnetlib.uoaadmintools.entities.curator.Curator;
|
5
|
import eu.dnetlib.uoaadmintools.entities.curator.CuratorResponse;
|
6
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.security.access.prepost.PreAuthorize;
|
10
|
import org.springframework.web.bind.annotation.*;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
import java.util.Optional;
|
15
|
|
16
|
@RestController
|
17
|
@CrossOrigin(origins = "*")
|
18
|
public class CuratorController {
|
19
|
private final Logger log = Logger.getLogger(this.getClass());
|
20
|
|
21
|
@Autowired
|
22
|
private CuratorDAO curatorDAO;
|
23
|
|
24
|
@Autowired
|
25
|
private RolesUtils rolesUtils;
|
26
|
|
27
|
/**
|
28
|
* Return a list with curator. If list of emails does not existed return all curators, else return
|
29
|
* curators based on given list.
|
30
|
*
|
31
|
* @param emails
|
32
|
* @return
|
33
|
*/
|
34
|
@RequestMapping(value = "/curator", method = RequestMethod.GET)
|
35
|
public List<CuratorResponse> getCurators(@RequestParam(required = false) Optional<String> emails) {
|
36
|
List<CuratorResponse> curators = new ArrayList<>();
|
37
|
if(emails.isPresent()) {
|
38
|
for(String email: emails.get().split(",")) {
|
39
|
Curator curator = curatorDAO.findByEmail(email);
|
40
|
if(curator != null) {
|
41
|
curators.add(new CuratorResponse(curator));
|
42
|
}
|
43
|
}
|
44
|
} else {
|
45
|
for(Curator curator: curatorDAO.findAll()) {
|
46
|
curators.add(new CuratorResponse(curator));
|
47
|
}
|
48
|
}
|
49
|
return curators;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* Return a Curator with the given id.
|
54
|
*
|
55
|
* @param id
|
56
|
* @return
|
57
|
*/
|
58
|
@PreAuthorize("isAuthenticated()")
|
59
|
@RequestMapping(value = "/curator/{id}", method = RequestMethod.GET)
|
60
|
public Curator getCuratorById(@PathVariable String id) {
|
61
|
return curatorDAO.findById(id);
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Create or update a curator, base on Curator object given on Request Body.
|
66
|
*
|
67
|
* @param curator
|
68
|
* @return
|
69
|
*/
|
70
|
@PreAuthorize("isAuthenticated()")
|
71
|
@RequestMapping(value = "/curator", method = RequestMethod.POST)
|
72
|
public Curator insertCurator(@RequestBody Curator curator) {
|
73
|
String aaiId = rolesUtils.getAaiId();
|
74
|
curator.setId(aaiId);
|
75
|
return curatorDAO.save(curator);
|
76
|
}
|
77
|
|
78
|
|
79
|
/**
|
80
|
* Delete all curators if list of emails does not exist or curators based on given list.
|
81
|
*
|
82
|
* @param emails
|
83
|
*/
|
84
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
85
|
@RequestMapping(value = "/curator", method = RequestMethod.DELETE)
|
86
|
public void deleteCurators(@RequestBody(required = false) Optional<List<String>> emails) {
|
87
|
if(emails.isPresent()) {
|
88
|
for(String email: emails.get()) {
|
89
|
Curator curator = curatorDAO.findByEmail(email);
|
90
|
if(curator != null) {
|
91
|
curatorDAO.delete(curator.getId());
|
92
|
}
|
93
|
}
|
94
|
} else {
|
95
|
curatorDAO.deleteAll();
|
96
|
}
|
97
|
}
|
98
|
|
99
|
}
|