Project

General

Profile

1
package eu.dnetlib.repo.manager.service.controllers;
2

    
3
import eu.dnetlib.repo.manager.service.utils.Converter;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.data.RepositoryInterface;
6
import org.json.JSONArray;
7
import org.json.JSONException;
8
import org.json.JSONObject;
9
import org.springframework.beans.factory.annotation.Value;
10
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
11
import org.springframework.stereotype.Component;
12
import org.springframework.web.bind.annotation.PathVariable;
13
import org.springframework.web.bind.annotation.RequestBody;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.client.RestTemplate;
16
import org.springframework.web.util.UriComponents;
17
import org.springframework.web.util.UriComponentsBuilder;
18

    
19
import javax.annotation.PostConstruct;
20
import java.util.List;
21

    
22

    
23
@Component
24
public class RepositoryApiImpl implements RepositoryApi {
25

    
26
    @Value("${api.baseAddress}")
27
    private String baseAddress;
28

    
29
    private RestTemplate restTemplate = null;
30

    
31
    @PostConstruct
32
    private void init(){
33
        restTemplate = new RestTemplate();
34
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
35
    }
36

    
37
    @Override
38
    public String getCountries()  {
39
        UriComponents uriComponents = UriComponentsBuilder
40
                .fromHttpUrl(baseAddress + "/ds/countries")
41
                .build().encode();
42
        return restTemplate.getForObject(uriComponents.toUri(), String.class);
43
    }
44

    
45

    
46
    @Override
47
    public List<Repository> getRepositoriesByCountry(@PathVariable("country") String country,
48
                                                     @PathVariable("page") String page,
49
                                                     @PathVariable("size") String size) throws JSONException {
50
       /* String vars = page+"/"+size+"?country="+country;
51
        String uri = baseAddress+ "/ds/search/country/" + vars;
52
        JSONArray jsonArray = new JSONArray(RestService.executeGET(uri));
53
        return Converter.jsonToRepositoryList(jsonArray);*/
54

    
55
        UriComponents uriComponents = UriComponentsBuilder
56
                                            .fromHttpUrl(baseAddress + "/ds/search/country/")
57
                                            .path("/{page}/{size}/")
58
                                            .queryParam("country",country)
59
                                            .build().expand(page,size).encode();
60

    
61
        RestTemplate template = new RestTemplate();
62
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
63

    
64
        String rs = template.getForObject(uriComponents.toUri(), String.class);
65
        return Converter.jsonToRepositoryList(new JSONArray(rs));
66
    }
67

    
68
    @Override
69
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
70
                                                  @PathVariable("page") String page,
71
                                                  @PathVariable("size") String size) throws JSONException {
72
        String vars = page+"/"+size+"?contactemail="+userEmail;
73
        String uri = baseAddress+ "/ds/search/email/" + vars;
74
        JSONArray jsonArray = new JSONArray(RestService.executeGET(uri));
75
        return Converter.jsonToRepositoryList(jsonArray);
76
    }
77

    
78
    @Override
79
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException {
80
        String uri = baseAddress+ "/ds/get/" + id;
81
        JSONObject jsonObject = new JSONObject( RestService.executeGET(uri) );
82
        return  Converter.jsonToRepositoryObject(jsonObject);
83
    }
84

    
85
    @Override
86
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
87
                                                  @PathVariable("page") String page,
88
                                                  @PathVariable("size") String size) throws JSONException {
89
        String vars = page+"/"+size+"?name="+name;
90
        String uri = baseAddress+ "/ds/search/name/" + vars;
91
        JSONArray jsonArray = new JSONArray(RestService.executeGET(uri));
92
        return Converter.jsonToRepositoryList(jsonArray);
93
    }
94

    
95
    @Override
96
    public RepositoryInterface getRepositoyInterface(@PathVariable("id") String id) throws JSONException {
97
        String uri = baseAddress+ "/ds/service/" + id;
98
        JSONArray jsonArray = new JSONArray( RestService.executeGET(uri) );
99
        return  Converter.jsonToRepositoryInterfaceObject(jsonArray.getJSONObject(0));
100
    }
101

    
102
    @Override
103
    public String addRepository(@RequestBody Repository repository) throws JSONException {
104
        String uri = baseAddress+ "/ds/add/";
105
        return RestService.executePOST(uri, Converter.repositoryObjectToJson(repository));
106
    }
107

    
108
    @Override
109
    public String addRepositoryInterface(RepositoryInterface repositoryInterface) throws JSONException {
110
        String uri = baseAddress+ "/ds/service/add/";
111
        return RestService.executePOST(uri, Converter.repositoryInterfaceObjectToJson(repositoryInterface));
112
    }
113

    
114
    @Override
115
    public List<String> getDnetCountries() {
116
        return Converter.readFile("countries.txt");
117
    }
118

    
119
    @Override
120
    public List<String> getTypologies() {
121
        System.out.println("Typologies");
122
        return Converter.readFile("typologies.txt");
123
    }
124

    
125
    @Override
126
    public List<String> getTimezones() {
127
        return Converter.readFile("timezones.txt");
128
    }
129

    
130
    @Override
131
    public String updateManagedStatus(@RequestParam(value = "id")   String id,
132
                                      @RequestParam(value = "managed")  String managed) {
133
        String uri = baseAddress+ "/ds/manage?id="+id+"&managed="+managed;
134
        return RestService.executePOST(uri,null);
135
    }
136

    
137
    @Override
138
    public String updateEnglishName(@RequestParam(value = "id")   String id,
139
                                    @RequestParam(value = "englishname")  String englishName) {
140
        String uri = baseAddress+ "/ds/manage?dsId="+id+"&englishname="+englishName;
141
        return RestService.executePOST(uri,null);
142
    }
143

    
144
    @Override
145
    public String updateLatitude(@RequestParam(value = "id")   String id,
146
                                 @RequestParam(value = "managed")  String latitude) {
147
        String uri = baseAddress+ "/ds/manage?dsId="+id+"&latitude="+latitude;
148
        return RestService.executePOST(uri,null);
149
    }
150

    
151
    @Override
152
    public String updateLongitude(@RequestParam(value = "id")   String id,
153
                                  @RequestParam(value = "longitude")  String longitude) {
154
        String uri = baseAddress+ "/ds/manage?dsId="+id+"&longitude="+longitude;
155
        return RestService.executePOST(uri,null);
156
    }
157

    
158
    @Override
159
    public String updateOfficialName(@RequestParam(value = "id")   String id,
160
                                     @RequestParam(value = "officialname")  String officialname) {
161
        String uri = baseAddress+ "/ds/manage?dsId="+id+"&officialname="+officialname;
162
        return RestService.executePOST(uri,null);
163
    }
164

    
165
    @Override
166
    public String getUrlsOfUserRepos(@PathVariable("user_email") String user_email) {
167

    
168
        //TODO create service a join datasource d for user u method
169

    
170
        return null;
171
    }
172

    
173

    
174
}
(8-8/11)