Project

General

Profile

1
package eu.dnetlib.uoaadmintools.services;
2

    
3
import eu.dnetlib.uoaadmintools.configuration.properties.ManagersApiConfig;
4
import eu.dnetlib.uoaadmintools.entities.Manager;
5
import eu.dnetlib.uoaadmintools.entities.curator.Response;
6
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.HttpStatus;
9
import org.springframework.http.ResponseEntity;
10
import org.springframework.stereotype.Service;
11
import org.springframework.web.client.RestTemplate;
12

    
13
@Service
14
public class ManagerService {
15

    
16
    enum Type {
17
        EMAIL,
18
        ID
19
    }
20

    
21
    @Autowired
22
    private ManagersApiConfig config;
23

    
24
    @Autowired
25
    private RestTemplate restTemplate;
26

    
27
    public Manager[] getManagers(String pid) {
28
        return getManagers(pid, Type.EMAIL);
29
    }
30

    
31
    public Manager[] getManagers(String pid, Type type) {
32
        String url = (type == Type.EMAIL)?config.getEmail():config.getId();
33
        ResponseEntity<Response> responseEntity = restTemplate.getForEntity(url.replace("{community}", pid), Response.class);
34
        Response response = responseEntity.getBody();
35
        if (response != null && responseEntity.getStatusCode() == HttpStatus.OK) {
36
            return response.getResponse();
37
        } else {
38
            throw new ContentNotFoundException("No managers has been found for community " + pid);
39
        }
40
    }
41
}
(3-3/6)