Project

General

Profile

1 54525 panagiotis
package eu.dnetlib.repo.manager.controllers;
2 49236 panagiotis
3 50051 panagiotis
import eu.dnetlib.domain.data.Repository;
4
import eu.dnetlib.domain.data.RepositoryInterface;
5 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.*;
6 54525 panagiotis
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
7 54690 panagiotis
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
8 54525 panagiotis
import io.swagger.annotations.Api;
9 58858 antonis.le
import org.apache.log4j.Logger;
10 49362 panagiotis
import org.json.JSONException;
11 57907 ioannis.di
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12 49450 panagiotis
import org.springframework.beans.factory.annotation.Autowired;
13 54525 panagiotis
import org.springframework.http.MediaType;
14 61329 spyroukon
import org.springframework.security.access.prepost.PostAuthorize;
15 51656 panagiotis
import org.springframework.security.access.prepost.PreAuthorize;
16 53933 panagiotis
import org.springframework.security.core.Authentication;
17 57907 ioannis.di
import org.springframework.security.core.context.SecurityContextHolder;
18 54525 panagiotis
import org.springframework.web.bind.annotation.*;
19 50051 panagiotis
20 53113 panagiotis
import java.io.IOException;
21 54525 panagiotis
import java.util.List;
22
import java.util.Map;
23 49236 panagiotis
24 54525 panagiotis
@RestController
25
@RequestMapping(value = "/repository")
26 61318 spyroukon
@Api(description = "Repository API", tags = {"repository"})
27 54525 panagiotis
public class RepositoryController {
28 49236 panagiotis
29 61318 spyroukon
    private static final Logger logger = Logger.getLogger(RepositoryController.class);
30 58858 antonis.le
31 49450 panagiotis
    @Autowired
32 56661 ioannis.di
    private RepositoryServiceImpl repositoryService;
33 49450 panagiotis
34 60748 akoulalis
35 54525 panagiotis
    @RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
36
    @ResponseBody
37 49960 panagiotis
    public Country[] getCountries() {
38 54690 panagiotis
        return repositoryService.getCountries();
39 49236 panagiotis
    }
40
41 54525 panagiotis
    @RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET,
42
            produces = MediaType.APPLICATION_JSON_VALUE)
43
    @ResponseBody
44 53113 panagiotis
    public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
45
                                                            @PathVariable("mode") String mode,
46 61318 spyroukon
                                                            @RequestParam(value = "managed", required = false) Boolean managed) throws JSONException, IOException {
47 54690 panagiotis
        return repositoryService.getRepositoriesByCountry(country, mode, managed);
48 49236 panagiotis
    }
49
50 61318 spyroukon
    @RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}", method = RequestMethod.GET,
51 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
52
    @ResponseBody
53 51656 panagiotis
    @PreAuthorize("hasRole('ROLE_USER')")
54 61318 spyroukon
    public List<RepositorySnippet> getRepositoriesSnippetOfUser(
55
            @PathVariable("page") String page,
56
            @PathVariable("size") String size) throws JSONException, IOException {
57
        return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
58 49236 panagiotis
    }
59
60 61318 spyroukon
    @RequestMapping(value = "/user/repositories/{page}/{size}", method = RequestMethod.GET,
61 56636 antonis.le
            produces = MediaType.APPLICATION_JSON_VALUE)
62
    @ResponseBody
63 61318 spyroukon
    @PreAuthorize("hasRole('ROLE_USER')")
64
    public List<Repository> getRepositoriesOfUser(
65
            @PathVariable("page") String page,
66
            @PathVariable("size") String size) throws JSONException, IOException {
67
        return repositoryService.getRepositoriesOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
68
    }
69
70
    @RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,
71
            produces = MediaType.APPLICATION_JSON_VALUE)
72
    @ResponseBody
73 56636 antonis.le
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
74 61318 spyroukon
    public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name = "country", required = false) String country,
75
                                                                @RequestParam(name = "typology", required = false) String typology,
76
                                                                @RequestParam(name = "englishName", required = false) String englishName,
77
                                                                @RequestParam(name = "officialName", required = false) String officialName,
78 56636 antonis.le
                                                                @RequestParam("requestSortBy") String requestSortBy,
79
                                                                @RequestParam("order") String order,
80
                                                                @PathVariable("page") int page,
81
                                                                @PathVariable("size") int pageSize) throws Exception {
82
83
        return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
84
    }
85
86 60748 akoulalis
87 54525 panagiotis
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
88
            produces = MediaType.APPLICATION_JSON_VALUE)
89
    @ResponseBody
90 61329 spyroukon
    @PostAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id) or (returnObject.registeredBy=='null' and hasRole('ROLE_USER'))")
91 61318 spyroukon
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException, ResourceNotFoundException {
92 57549 ioannis.di
        Repository repo = repositoryService.getRepositoryById(id);
93 58858 antonis.le
94 61318 spyroukon
        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 57549 ioannis.di
        return repo;
99 49236 panagiotis
    }
100
101 54525 panagiotis
    @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET,
102
            produces = MediaType.APPLICATION_JSON_VALUE)
103
    @ResponseBody
104 52781 panagiotis
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
105 61318 spyroukon
        return repositoryService.getRepositoryAggregations(id, 0, 20);
106 52781 panagiotis
    }
107
108 54525 panagiotis
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
109
            produces = MediaType.APPLICATION_JSON_VALUE)
110
    @ResponseBody
111 52781 panagiotis
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
112 54690 panagiotis
        return repositoryService.getRepositoryAggregationsByYear(id);
113 52781 panagiotis
    }
114 49935 panagiotis
115 54525 panagiotis
    @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
116
            produces = MediaType.APPLICATION_JSON_VALUE)
117
    @ResponseBody
118 49236 panagiotis
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
119
                                                  @PathVariable("page") String page,
120 49362 panagiotis
                                                  @PathVariable("size") String size) throws JSONException {
121 54690 panagiotis
        return repositoryService.getRepositoriesByName(name, page, size);
122 49236 panagiotis
    }
123
124 54525 panagiotis
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
125
            produces = MediaType.APPLICATION_JSON_VALUE)
126
    @ResponseBody
127 61318 spyroukon
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id)")
128 49988 panagiotis
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
129 54690 panagiotis
        return repositoryService.getRepositoryInterface(id);
130 49236 panagiotis
    }
131
132 54525 panagiotis
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
133
            consumes = MediaType.APPLICATION_JSON_VALUE)
134
    @ResponseBody
135 61318 spyroukon
//    @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 50945 panagiotis
    public Repository addRepository(@RequestParam("datatype") String datatype,
138
                                    @RequestBody Repository repository) throws Exception {
139 49898 panagiotis
140 54690 panagiotis
        return repositoryService.addRepository(datatype, repository);
141 49963 panagiotis
    }
142 49960 panagiotis
143 54525 panagiotis
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
144
            produces = MediaType.APPLICATION_JSON_VALUE)
145
    @ResponseBody
146 61318 spyroukon
    public List<String> getDnetCountries() {
147 54690 panagiotis
        return repositoryService.getDnetCountries();
148 50075 panagiotis
    }
149
150 54525 panagiotis
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
151
            produces = MediaType.APPLICATION_JSON_VALUE)
152
    @ResponseBody
153 61318 spyroukon
    public List<String> getTypologies() {
154 54690 panagiotis
        return repositoryService.getTypologies();
155 49960 panagiotis
    }
156
157 54525 panagiotis
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
158
            produces = MediaType.APPLICATION_JSON_VALUE)
159
    @ResponseBody
160 61318 spyroukon
    public List<Timezone> getTimezones() {
161 54690 panagiotis
        return repositoryService.getTimezones();
162 49963 panagiotis
    }
163
164 54525 panagiotis
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
165
            consumes = MediaType.APPLICATION_JSON_VALUE)
166
    @ResponseBody
167 61318 spyroukon
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repository.id)")
168
    public Repository updateRepository(@RequestBody Repository repository, Authentication authentication) throws Exception {
169 54690 panagiotis
        return repositoryService.updateRepository(repository, authentication);
170 49236 panagiotis
    }
171
172 54525 panagiotis
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
173 61318 spyroukon
    @PreAuthorize("@aaiUserRoleService.isMemberOf(#id)")
174
    public void deleteRepositoryInterface(@RequestParam("id") String id,
175
                                          @RequestParam("registeredBy") String registeredBy) {
176 54690 panagiotis
        repositoryService.deleteRepositoryInterface(id, registeredBy);
177 49813 panagiotis
    }
178
179 54525 panagiotis
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
180
            consumes = MediaType.APPLICATION_JSON_VALUE)
181
    @ResponseBody
182 61318 spyroukon
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
183 50075 panagiotis
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
184
                                                      @RequestParam("repoId") String repoId,
185 52781 panagiotis
                                                      @RequestParam("registeredBy") String registeredBy,
186 59198 antonis.le
                                                      @RequestParam(value = "comment", required = false) String comment,
187 57966 ioannis.di
                                                      @RequestBody RepositoryInterface repositoryInterface) throws Exception {
188 59190 antonis.le
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, comment, repositoryInterface);
189 49963 panagiotis
    }
190
191 61318 spyroukon
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/", method = RequestMethod.GET,
192 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
193
    @ResponseBody
194 51656 panagiotis
    @PreAuthorize("hasRole('ROLE_USER')")
195 61318 spyroukon
    public List<String> getUrlsOfUserRepos(@PathVariable("page") String page, @PathVariable("size") String size) {
196 57966 ioannis.di
        return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
197 49236 panagiotis
    }
198
199 61318 spyroukon
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}", method = RequestMethod.GET,
200 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
201
    @ResponseBody
202 49450 panagiotis
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
203 61318 spyroukon
        return repositoryService.getDatasourceVocabularies(mode);
204 49450 panagiotis
    }
205
206 61318 spyroukon
    @RequestMapping(value = "/getCompatibilityClasses/{mode}", method = RequestMethod.GET,
207 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
208
    @ResponseBody
209 61318 spyroukon
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
210 49790 panagiotis
211 54690 panagiotis
        return repositoryService.getCompatibilityClasses(mode);
212 49790 panagiotis
    }
213
214 61318 spyroukon
    @RequestMapping(value = "/getDatasourceClasses/{mode}", method = RequestMethod.GET,
215 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
216
    @ResponseBody
217 61318 spyroukon
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
218
        return repositoryService.getDatasourceClasses(mode);
219 49790 panagiotis
    }
220
221 61318 spyroukon
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}", method = RequestMethod.GET,
222 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
223
    @ResponseBody
224 61318 spyroukon
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId") String repoId) throws RepositoryServiceException {
225 54690 panagiotis
        return repositoryService.getMetricsInfoForRepository(repoId);
226 50570 panagiotis
    }
227
228 61318 spyroukon
    @RequestMapping(value = "/getListLatestUpdate/{mode}", method = RequestMethod.GET,
229 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
230
    @ResponseBody
231 61318 spyroukon
    public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException {
232 54690 panagiotis
        return repositoryService.getListLatestUpdate(mode);
233 51330 panagiotis
    }
234
235 54525 panagiotis
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
236
            consumes = MediaType.APPLICATION_JSON_VALUE)
237
    @ResponseBody
238 61318 spyroukon
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#repoId)")
239 51525 panagiotis
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
240 52781 panagiotis
                                                         @RequestParam("registeredBy") String registeredBy,
241 59198 antonis.le
                                                         @RequestParam(value = "comment", required = false) String comment,
242 54702 panagiotis
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
243 59190 antonis.le
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, comment, repositoryInterface);
244 51525 panagiotis
    }
245 49236 panagiotis
}