Project

General

Profile

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

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

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

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

    
27
    @Autowired
28
    RepositoryServiceImpl repositoryService;
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
139
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
140
            consumes = MediaType.APPLICATION_JSON_VALUE)
141
    @ResponseBody
142
    //@PreAuthorize("hasRole('ROLE_USER') and #repository.registeredBy == authentication.userInfo.email")
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 {
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)