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.*;
6
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
7
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
8
import io.swagger.annotations.Api;
9
import org.apache.log4j.Logger;
10
import org.json.JSONException;
11
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
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.security.core.context.SecurityContextHolder;
17
import org.springframework.web.bind.annotation.*;
18

    
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
    private static final Logger logger = Logger.getLogger(RepositoryController.class);
29

    
30
    @Autowired
31
    private RepositoryServiceImpl repositoryService;
32

    
33

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

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

    
49
    @RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}", method = RequestMethod.GET,
50
            produces = MediaType.APPLICATION_JSON_VALUE)
51
    @ResponseBody
52
    @PreAuthorize("hasRole('ROLE_USER')")
53
    public List<RepositorySnippet> getRepositoriesSnippetOfUser(
54
            @PathVariable("page") String page,
55
            @PathVariable("size") String size) throws JSONException, IOException {
56
        return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
57
    }
58

    
59
    @RequestMapping(value = "/user/repositories/{page}/{size}", method = RequestMethod.GET,
60
            produces = MediaType.APPLICATION_JSON_VALUE)
61
    @ResponseBody
62
    @PreAuthorize("hasRole('ROLE_USER')")
63
    public List<Repository> getRepositoriesOfUser(
64
            @PathVariable("page") String page,
65
            @PathVariable("size") String size) throws JSONException, IOException {
66
        return repositoryService.getRepositoriesOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
67
    }
68

    
69
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,
70
            produces = MediaType.APPLICATION_JSON_VALUE)
71
    @ResponseBody
72
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
73
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name = "country", required = false) String country,
74
                                                                @RequestParam(name = "typology", required = false) String typology,
75
                                                                @RequestParam(name = "englishName", required = false) String englishName,
76
                                                                @RequestParam(name = "officialName", required = false) String officialName,
77
                                                                @RequestParam("requestSortBy") String requestSortBy,
78
                                                                @RequestParam("order") String order,
79
                                                                @PathVariable("page") int page,
80
                                                                @PathVariable("size") int pageSize) throws Exception {
81

    
82
        return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
83
    }
84

    
85
    // TODO: Antonis K - Replace here the registeredBy
86

    
87
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
88
            produces = MediaType.APPLICATION_JSON_VALUE)
89
    @ResponseBody
90
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id)")
91
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException, ResourceNotFoundException {
92
        Repository repo = repositoryService.getRepositoryById(id);
93

    
94
        if (repo != null)
95
            logger.info("Returning repository " + repo.getId() + " registered by " + repo.getRegisteredBy());
96
        else
97
            logger.info("Requested repository " + id + " not found");
98
        return repo;
99
    }
100

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

    
108
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
109
            produces = MediaType.APPLICATION_JSON_VALUE)
110
    @ResponseBody
111
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
112
        return repositoryService.getRepositoryAggregationsByYear(id);
113
    }
114

    
115
    @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
116
            produces = MediaType.APPLICATION_JSON_VALUE)
117
    @ResponseBody
118
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
119
                                                  @PathVariable("page") String page,
120
                                                  @PathVariable("size") String size) throws JSONException {
121
        return repositoryService.getRepositoriesByName(name, page, size);
122
    }
123

    
124
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
125
            produces = MediaType.APPLICATION_JSON_VALUE)
126
    @ResponseBody
127
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id)")
128
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
129
        return repositoryService.getRepositoryInterface(id);
130
    }
131

    
132
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
133
            consumes = MediaType.APPLICATION_JSON_VALUE)
134
    @ResponseBody
135
//    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole(@aaiUserRoleService.convertRepoIdToRoleId(#repository.id)) or hasRole(@aaiUserRoleService.convertRepoIdToRoleId(returnObject.id)))")
136
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or hasRole('ROLE_USER')")
137
    public Repository addRepository(@RequestParam("datatype") String datatype,
138
                                    @RequestBody Repository repository) throws Exception {
139

    
140
        // TODO:
141
        //  1) add repository
142
        //  2) get repository id and create new role
143
        //  3) assign new role to authenticated user
144
        return repositoryService.addRepository(datatype, repository);
145
    }
146

    
147
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
148
            produces = MediaType.APPLICATION_JSON_VALUE)
149
    @ResponseBody
150
    public List<String> getDnetCountries() {
151
        return repositoryService.getDnetCountries();
152
    }
153

    
154
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
155
            produces = MediaType.APPLICATION_JSON_VALUE)
156
    @ResponseBody
157
    public List<String> getTypologies() {
158
        return repositoryService.getTypologies();
159
    }
160

    
161
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
162
            produces = MediaType.APPLICATION_JSON_VALUE)
163
    @ResponseBody
164
    public List<Timezone> getTimezones() {
165
        return repositoryService.getTimezones();
166
    }
167

    
168
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
169
            consumes = MediaType.APPLICATION_JSON_VALUE)
170
    @ResponseBody
171
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repository.id)")
172
    public Repository updateRepository(@RequestBody Repository repository, Authentication authentication) throws Exception {
173
        return repositoryService.updateRepository(repository, authentication);
174
    }
175

    
176
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
177
    @PreAuthorize("@aaiUserRoleService.isMemberOf(#id)")
178
    public void deleteRepositoryInterface(@RequestParam("id") String id,
179
                                          @RequestParam("registeredBy") String registeredBy) {
180
        repositoryService.deleteRepositoryInterface(id, registeredBy);
181
    }
182

    
183
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
184
            consumes = MediaType.APPLICATION_JSON_VALUE)
185
    @ResponseBody
186
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
187
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
188
                                                      @RequestParam("repoId") String repoId,
189
                                                      @RequestParam("registeredBy") String registeredBy,
190
                                                      @RequestParam(value = "comment", required = false) String comment,
191
                                                      @RequestBody RepositoryInterface repositoryInterface) throws Exception {
192
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, comment, repositoryInterface);
193
    }
194

    
195
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/", method = RequestMethod.GET,
196
            produces = MediaType.APPLICATION_JSON_VALUE)
197
    @ResponseBody
198
    @PreAuthorize("hasRole('ROLE_USER')")
199
    public List<String> getUrlsOfUserRepos(@PathVariable("page") String page, @PathVariable("size") String size) {
200
        return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
201
    }
202

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

    
210
    @RequestMapping(value = "/getCompatibilityClasses/{mode}", method = RequestMethod.GET,
211
            produces = MediaType.APPLICATION_JSON_VALUE)
212
    @ResponseBody
213
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
214

    
215
        return repositoryService.getCompatibilityClasses(mode);
216
    }
217

    
218
    @RequestMapping(value = "/getDatasourceClasses/{mode}", method = RequestMethod.GET,
219
            produces = MediaType.APPLICATION_JSON_VALUE)
220
    @ResponseBody
221
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
222
        return repositoryService.getDatasourceClasses(mode);
223
    }
224

    
225
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}", method = RequestMethod.GET,
226
            produces = MediaType.APPLICATION_JSON_VALUE)
227
    @ResponseBody
228
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId") String repoId) throws RepositoryServiceException {
229
        return repositoryService.getMetricsInfoForRepository(repoId);
230
    }
231

    
232
    @RequestMapping(value = "/getListLatestUpdate/{mode}", method = RequestMethod.GET,
233
            produces = MediaType.APPLICATION_JSON_VALUE)
234
    @ResponseBody
235
    public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException {
236
        return repositoryService.getListLatestUpdate(mode);
237
    }
238

    
239
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
240
            consumes = MediaType.APPLICATION_JSON_VALUE)
241
    @ResponseBody
242
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
243
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
244
                                                         @RequestParam("registeredBy") String registeredBy,
245
                                                         @RequestParam(value = "comment", required = false) String comment,
246
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
247
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, comment, repositoryInterface);
248
    }
249
}
(7-7/12)