Project

General

Profile

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

    
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.data.RepositoryInterface;
6
import eu.dnetlib.repo.manager.domain.RepositorySnippet;
7
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
8
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
9
import eu.dnetlib.repo.manager.shared.*;
10
import io.swagger.annotations.Api;
11
import org.json.JSONException;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.MediaType;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.security.core.Authentication;
16
import org.springframework.web.bind.annotation.*;
17

    
18
import javax.ws.rs.Path;
19
import java.io.IOException;
20
import java.util.List;
21
import java.util.Map;
22

    
23
@RestController
24
@RequestMapping(value = "/repository")
25
@Api(description = "Repository API",  tags = {"repository"})
26
public class RepositoryController {
27

    
28
    @Autowired
29
    private RepositoryServiceImpl repositoryService;
30

    
31
    @RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
32
    @ResponseBody
33
    public Country[] getCountries() {
34
        return repositoryService.getCountries();
35
    }
36

    
37
    @RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET,
38
            produces = MediaType.APPLICATION_JSON_VALUE)
39
    @ResponseBody
40
    public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
41
                                                            @PathVariable("mode") String mode,
42
                                                            @RequestParam(value = "managed",required=false) Boolean managed) throws JSONException, IOException {
43
        return repositoryService.getRepositoriesByCountry(country, mode, managed);
44
    }
45

    
46
    @RequestMapping(value = "/getRepositoriesOfUser/{userEmail}/{page}/{size}",method = RequestMethod.GET,
47
            produces = MediaType.APPLICATION_JSON_VALUE)
48
    @ResponseBody
49
    @PreAuthorize("hasRole('ROLE_USER')")
50
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
51
                                                  @PathVariable("page") String page,
52
                                                  @PathVariable("size") String size) throws JSONException {
53
       return repositoryService.getRepositoriesOfUser(userEmail, page, size);
54
    }
55

    
56
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}",method = RequestMethod.GET,
57
            produces = MediaType.APPLICATION_JSON_VALUE)
58
    @ResponseBody
59
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
60
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name="country", required=false) String country,
61
                                                                @RequestParam(name="typology", required=false) String typology,
62
                                                                @RequestParam(name="englishName", required=false) String englishName,
63
                                                                @RequestParam(name="officialName", required=false) String officialName,
64
                                                                @RequestParam("requestSortBy") String requestSortBy,
65
                                                                @RequestParam("order") String order,
66
                                                                @PathVariable("page") int page,
67
                                                                @PathVariable("size") int pageSize) throws Exception {
68

    
69
        return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
70
    }
71

    
72
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
73
            produces = MediaType.APPLICATION_JSON_VALUE)
74
    @ResponseBody
75
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
76
        return repositoryService.getRepositoryById(id);
77
    }
78

    
79
    @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET,
80
            produces = MediaType.APPLICATION_JSON_VALUE)
81
    @ResponseBody
82
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
83
        return repositoryService.getRepositoryAggregations(id);
84
    }
85

    
86
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
87
            produces = MediaType.APPLICATION_JSON_VALUE)
88
    @ResponseBody
89
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
90
        return repositoryService.getRepositoryAggregationsByYear(id);
91
    }
92

    
93
    @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
94
            produces = MediaType.APPLICATION_JSON_VALUE)
95
    @ResponseBody
96
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
97
                                                  @PathVariable("page") String page,
98
                                                  @PathVariable("size") String size) throws JSONException {
99
        return repositoryService.getRepositoriesByName(name, page, size);
100
    }
101

    
102
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
103
            produces = MediaType.APPLICATION_JSON_VALUE)
104
    @ResponseBody
105
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
106
        return repositoryService.getRepositoryInterface(id);
107
    }
108

    
109
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
110
            consumes = MediaType.APPLICATION_JSON_VALUE)
111
    @ResponseBody
112
//    @PreAuthorize("hasRole('ROLE_USER') and #repository.registeredBy == authentication.userInfo.email")
113
    public Repository addRepository(@RequestParam("datatype") String datatype,
114
                                    @RequestBody Repository repository) throws Exception {
115

    
116
        return repositoryService.addRepository(datatype, repository);
117
    }
118

    
119
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
120
            produces = MediaType.APPLICATION_JSON_VALUE)
121
    @ResponseBody
122
    public List<String> getDnetCountries(){
123
        return repositoryService.getDnetCountries();
124
    }
125

    
126
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
127
            produces = MediaType.APPLICATION_JSON_VALUE)
128
    @ResponseBody
129
    public List<String> getTypologies(){
130
        return repositoryService.getTypologies();
131
    }
132

    
133
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
134
            produces = MediaType.APPLICATION_JSON_VALUE)
135
    @ResponseBody
136
    public List<Timezone> getTimezones(){
137
        return repositoryService.getTimezones();
138
    }
139

    
140
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
141
            consumes = MediaType.APPLICATION_JSON_VALUE)
142
    @ResponseBody
143
    public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception {
144
        return repositoryService.updateRepository(repository, authentication);
145
    }
146

    
147
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
148
    @PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
149
    public void deleteRepositoryInterface(@RequestParam("id") String id ,
150
                                          @RequestParam("registeredBy") String registeredBy){
151
        repositoryService.deleteRepositoryInterface(id, registeredBy);
152
    }
153

    
154
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
155
            consumes = MediaType.APPLICATION_JSON_VALUE)
156
    @ResponseBody
157
    @PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
158
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
159
                                                      @RequestParam("repoId") String repoId,
160
                                                      @RequestParam("registeredBy") String registeredBy,
161
                                                      @RequestBody RepositoryInterface repositoryInterface) throws JSONException, ResourceNotFoundException, ValidatorServiceException {
162
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, repositoryInterface);
163
    }
164

    
165
    @RequestMapping(value = "/getUrlsOfUserRepos/{user_email}/{page}/{size}/",method = RequestMethod.GET,
166
            produces = MediaType.APPLICATION_JSON_VALUE)
167
    @ResponseBody
168
    @PreAuthorize("hasRole('ROLE_USER')")
169
    public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String userEmail,
170
                                           @PathVariable("page") String page,
171
                                           @PathVariable("size") String size) throws JSONException {
172
        return repositoryService.getUrlsOfUserRepos(userEmail, page, size);
173
    }
174

    
175
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}",method = RequestMethod.GET,
176
            produces = MediaType.APPLICATION_JSON_VALUE)
177
    @ResponseBody
178
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
179
       return repositoryService.getDatasourceVocabularies(mode);
180
    }
181

    
182
    @RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET,
183
            produces = MediaType.APPLICATION_JSON_VALUE)
184
    @ResponseBody
185
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode)  {
186

    
187
        return repositoryService.getCompatibilityClasses(mode);
188
    }
189

    
190
    @RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET,
191
            produces = MediaType.APPLICATION_JSON_VALUE)
192
    @ResponseBody
193
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode)  {
194
       return repositoryService.getDatasourceClasses(mode);
195
    }
196

    
197
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}",method = RequestMethod.GET,
198
            produces = MediaType.APPLICATION_JSON_VALUE)
199
    @ResponseBody
200
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId")  String repoId) throws RepositoryServiceException {
201
        return repositoryService.getMetricsInfoForRepository(repoId);
202
    }
203

    
204
    @RequestMapping(value = "/getListLatestUpdate/{mode}",method = RequestMethod.GET,
205
            produces = MediaType.APPLICATION_JSON_VALUE)
206
    @ResponseBody
207
    public Map<String, String> getListLatestUpdate(@PathVariable("mode")  String mode) throws JSONException {
208
        return repositoryService.getListLatestUpdate(mode);
209
    }
210

    
211
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
212
            consumes = MediaType.APPLICATION_JSON_VALUE)
213
    @ResponseBody
214
    @PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
215
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
216
                                                         @RequestParam("registeredBy") String registeredBy,
217
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
218
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface);
219
    }
220
}
(6-6/11)