100 lines
3.1 KiB
Java
100 lines
3.1 KiB
Java
package eu.dnetlib.uoaadmintools.controllers;
|
|
|
|
import eu.dnetlib.uoaadmintools.dao.CuratorDAO;
|
|
import eu.dnetlib.uoaadmintools.entities.curator.Curator;
|
|
import eu.dnetlib.uoaadmintools.entities.curator.CuratorResponse;
|
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class CuratorController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private CuratorDAO curatorDAO;
|
|
|
|
@Autowired
|
|
private RolesUtils rolesUtils;
|
|
|
|
/**
|
|
* Return a list with curator. If list of emails does not existed return all curators, else return
|
|
* curators based on given list.
|
|
*
|
|
* @param emails
|
|
* @return
|
|
*/
|
|
@RequestMapping(value = "/curator", method = RequestMethod.GET)
|
|
public List<CuratorResponse> getCurators(@RequestParam(required = false) Optional<String> emails) {
|
|
List<CuratorResponse> curators = new ArrayList<>();
|
|
if(emails.isPresent()) {
|
|
for(String email: emails.get().split(",")) {
|
|
Curator curator = curatorDAO.findByEmail(email);
|
|
if(curator != null) {
|
|
curators.add(new CuratorResponse(curator));
|
|
}
|
|
}
|
|
} else {
|
|
for(Curator curator: curatorDAO.findAll()) {
|
|
curators.add(new CuratorResponse(curator));
|
|
}
|
|
}
|
|
return curators;
|
|
}
|
|
|
|
/**
|
|
* Return a Curator with the given id.
|
|
*
|
|
* @param id
|
|
* @return
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@RequestMapping(value = "/curator/{id}", method = RequestMethod.GET)
|
|
public Curator getCuratorById(@PathVariable String id) {
|
|
return curatorDAO.findById(id);
|
|
}
|
|
|
|
/**
|
|
* Create or update a curator, base on Curator object given on Request Body.
|
|
*
|
|
* @param curator
|
|
* @return
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@RequestMapping(value = "/curator", method = RequestMethod.POST)
|
|
public Curator insertCurator(@RequestBody Curator curator) {
|
|
String aaiId = rolesUtils.getAaiId();
|
|
curator.setId(aaiId);
|
|
return curatorDAO.save(curator);
|
|
}
|
|
|
|
|
|
/**
|
|
* Delete all curators if list of emails does not exist or curators based on given list.
|
|
*
|
|
* @param emails
|
|
*/
|
|
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
|
@RequestMapping(value = "/curator", method = RequestMethod.DELETE)
|
|
public void deleteCurators(@RequestBody(required = false) Optional<List<String>> emails) {
|
|
if(emails.isPresent()) {
|
|
for(String email: emails.get()) {
|
|
Curator curator = curatorDAO.findByEmail(email);
|
|
if(curator != null) {
|
|
curatorDAO.delete(curator.getId());
|
|
}
|
|
}
|
|
} else {
|
|
curatorDAO.deleteAll();
|
|
}
|
|
}
|
|
|
|
}
|