Project

General

Profile

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 org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.web.bind.annotation.*;
9

    
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.Optional;
13

    
14
@RestController
15
@CrossOrigin(origins = "*")
16
public class CuratorController {
17
    private final Logger log = Logger.getLogger(this.getClass());
18

    
19
    @Autowired
20
    private CuratorDAO curatorDAO;
21

    
22

    
23
    /**
24
     * Return a list with curator. If list of emails does not existed return all curators, else return
25
     * curators based on given list.
26
     *
27
     * @param emails
28
     * @return
29
     */
30
    @RequestMapping(value = "/curator", method = RequestMethod.GET)
31
    public List<CuratorResponse> getCurators(@RequestParam(required = false) Optional<String> emails) {
32
        List<CuratorResponse> curators = new ArrayList<>();
33
        if(emails.isPresent()) {
34
            for(String email: emails.get().split(",")) {
35
                Curator curator = curatorDAO.findByEmail(email);
36
                if(curator != null) {
37
                    curators.add(new CuratorResponse(curator));
38
                }
39
            }
40
        } else {
41
            for(Curator curator: curatorDAO.findAll()) {
42
                curators.add(new CuratorResponse(curator));
43
            }
44
        }
45
        return curators;
46
    }
47

    
48
    /**
49
     * Return a Curator with the given id.
50
     *
51
     * @param id
52
     * @return
53
     */
54
    @RequestMapping(value = "/curator/{id}", method = RequestMethod.GET)
55
    public Curator getCuratorById(@PathVariable String id) {
56
        return curatorDAO.findById(id);
57
    }
58

    
59
    /**
60
     * Create or update a curator, base on Curator object given on Request Body.
61
     *
62
     * @param curator
63
     * @return
64
     */
65
    @RequestMapping(value = "/curator", method = RequestMethod.POST)
66
    public Curator insertCurator(@RequestBody Curator curator) {
67
        return curatorDAO.save(curator);
68
    }
69

    
70

    
71
    /**
72
     * Delete all curators if list of emails does not exist or curators based on given list.
73
     *
74
     * @param emails
75
     */
76
    @RequestMapping(value = "/curator", method = RequestMethod.DELETE)
77
    public void deleteCurators(@RequestBody(required = false) Optional<List<String>> emails) {
78
        if(emails.isPresent()) {
79
            for(String email: emails.get()) {
80
                Curator curator = curatorDAO.findByEmail(email);
81
                if(curator != null) {
82
                    curatorDAO.delete(curator.getId());
83
                }
84
            }
85
        } else {
86
            curatorDAO.deleteAll();
87
        }
88
    }
89

    
90
}
(3-3/10)