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 49362 panagiotis
import org.json.JSONException;
10 57907 ioannis.di
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
11 49450 panagiotis
import org.springframework.beans.factory.annotation.Autowired;
12 54525 panagiotis
import org.springframework.http.MediaType;
13 57350 ioannis.di
import org.springframework.security.access.prepost.PostAuthorize;
14 51656 panagiotis
import org.springframework.security.access.prepost.PreAuthorize;
15 53933 panagiotis
import org.springframework.security.core.Authentication;
16 57907 ioannis.di
import org.springframework.security.core.context.SecurityContextHolder;
17 54525 panagiotis
import org.springframework.web.bind.annotation.*;
18 50051 panagiotis
19 53113 panagiotis
import java.io.IOException;
20 54525 panagiotis
import java.util.List;
21
import java.util.Map;
22 49236 panagiotis
23 54525 panagiotis
@RestController
24
@RequestMapping(value = "/repository")
25
@Api(description = "Repository API",  tags = {"repository"})
26
public class RepositoryController {
27 49236 panagiotis
28 49450 panagiotis
    @Autowired
29 56661 ioannis.di
    private RepositoryServiceImpl repositoryService;
30 49450 panagiotis
31 54525 panagiotis
    @RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
32
    @ResponseBody
33 49960 panagiotis
    public Country[] getCountries() {
34 54690 panagiotis
        return repositoryService.getCountries();
35 49236 panagiotis
    }
36
37 54525 panagiotis
    @RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET,
38
            produces = MediaType.APPLICATION_JSON_VALUE)
39
    @ResponseBody
40 53113 panagiotis
    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 54690 panagiotis
        return repositoryService.getRepositoriesByCountry(country, mode, managed);
44 49236 panagiotis
    }
45
46 57966 ioannis.di
    @RequestMapping(value = "/getRepositoriesOfUser/{page}/{size}",method = RequestMethod.GET,
47 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
48
    @ResponseBody
49 51656 panagiotis
    @PreAuthorize("hasRole('ROLE_USER')")
50 57966 ioannis.di
    public List<RepositorySnippet> getRepositoriesOfUser(
51 49236 panagiotis
                                                  @PathVariable("page") String page,
52 57874 ioannis.di
                                                  @PathVariable("size") String size) throws JSONException, IOException {
53 57966 ioannis.di
       return repositoryService.getRepositoriesSnippetOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
54 49236 panagiotis
    }
55
56 56636 antonis.le
    @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 54525 panagiotis
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
73
            produces = MediaType.APPLICATION_JSON_VALUE)
74
    @ResponseBody
75 57549 ioannis.di
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER'))")
76 52781 panagiotis
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
77 57549 ioannis.di
        Repository repo = repositoryService.getRepositoryById(id);
78
        return repo;
79 49236 panagiotis
    }
80
81 54525 panagiotis
    @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET,
82
            produces = MediaType.APPLICATION_JSON_VALUE)
83
    @ResponseBody
84 52781 panagiotis
    public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
85 57176 ioannis.di
        return repositoryService.getRepositoryAggregations(id,0,20);
86 52781 panagiotis
    }
87
88 54525 panagiotis
    @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
89
            produces = MediaType.APPLICATION_JSON_VALUE)
90
    @ResponseBody
91 52781 panagiotis
    public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
92 54690 panagiotis
        return repositoryService.getRepositoryAggregationsByYear(id);
93 52781 panagiotis
    }
94 49935 panagiotis
95 54525 panagiotis
    @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
96
            produces = MediaType.APPLICATION_JSON_VALUE)
97
    @ResponseBody
98 49236 panagiotis
    public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
99
                                                  @PathVariable("page") String page,
100 49362 panagiotis
                                                  @PathVariable("size") String size) throws JSONException {
101 54690 panagiotis
        return repositoryService.getRepositoriesByName(name, page, size);
102 49236 panagiotis
    }
103
104 54525 panagiotis
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
105
            produces = MediaType.APPLICATION_JSON_VALUE)
106
    @ResponseBody
107 57549 ioannis.di
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#id).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#id).registeredBy=='null' ) and hasRole('ROLE_USER'))")
108 49988 panagiotis
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
109 54690 panagiotis
        return repositoryService.getRepositoryInterface(id);
110 49236 panagiotis
    }
111
112 54525 panagiotis
    @RequestMapping(value = "/addRepository", method = RequestMethod.POST,
113
            consumes = MediaType.APPLICATION_JSON_VALUE)
114
    @ResponseBody
115 57549 ioannis.di
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or returnObject.registeredBy=='null') and hasRole('ROLE_USER'))")
116 50945 panagiotis
    public Repository addRepository(@RequestParam("datatype") String datatype,
117
                                    @RequestBody Repository repository) throws Exception {
118 49898 panagiotis
119 54690 panagiotis
        return repositoryService.addRepository(datatype, repository);
120 49963 panagiotis
    }
121 49960 panagiotis
122 54525 panagiotis
    @RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
123
            produces = MediaType.APPLICATION_JSON_VALUE)
124
    @ResponseBody
125 56661 ioannis.di
    public List<String> getDnetCountries(){
126 54690 panagiotis
        return repositoryService.getDnetCountries();
127 50075 panagiotis
    }
128
129 54525 panagiotis
    @RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
130
            produces = MediaType.APPLICATION_JSON_VALUE)
131
    @ResponseBody
132 56661 ioannis.di
    public List<String> getTypologies(){
133 54690 panagiotis
        return repositoryService.getTypologies();
134 49960 panagiotis
    }
135
136 54525 panagiotis
    @RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
137
            produces = MediaType.APPLICATION_JSON_VALUE)
138
    @ResponseBody
139 56661 ioannis.di
    public List<Timezone> getTimezones(){
140 54690 panagiotis
        return repositoryService.getTimezones();
141 49963 panagiotis
    }
142
143 54525 panagiotis
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
144
            consumes = MediaType.APPLICATION_JSON_VALUE)
145
    @ResponseBody
146 57549 ioannis.di
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or #repository.registeredBy=='null') and hasRole('ROLE_USER'))")
147 54525 panagiotis
    public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception {
148 54690 panagiotis
        return repositoryService.updateRepository(repository, authentication);
149 49236 panagiotis
    }
150
151 54525 panagiotis
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
152 52781 panagiotis
    @PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
153
    public void deleteRepositoryInterface(@RequestParam("id") String id ,
154
                                          @RequestParam("registeredBy") String registeredBy){
155 54690 panagiotis
        repositoryService.deleteRepositoryInterface(id, registeredBy);
156 49813 panagiotis
    }
157
158 54525 panagiotis
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
159
            consumes = MediaType.APPLICATION_JSON_VALUE)
160
    @ResponseBody
161 57549 ioannis.di
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
162 50075 panagiotis
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
163
                                                      @RequestParam("repoId") String repoId,
164 52781 panagiotis
                                                      @RequestParam("registeredBy") String registeredBy,
165 57966 ioannis.di
                                                      @RequestBody RepositoryInterface repositoryInterface) throws Exception {
166 54690 panagiotis
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, repositoryInterface);
167 49963 panagiotis
    }
168
169 57966 ioannis.di
    @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/",method = RequestMethod.GET,
170 54525 panagiotis
            produces = MediaType.APPLICATION_JSON_VALUE)
171
    @ResponseBody
172 51656 panagiotis
    @PreAuthorize("hasRole('ROLE_USER')")
173 57966 ioannis.di
    public List<String> getUrlsOfUserRepos(
174 49763 panagiotis
                                           @PathVariable("page") String page,
175
                                           @PathVariable("size") String size) throws JSONException {
176 57966 ioannis.di
        return repositoryService.getUrlsOfUserRepos(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size);
177 49236 panagiotis
    }
178
179 54525 panagiotis
    @RequestMapping(value = "/getDatasourceVocabularies/{mode}",method = RequestMethod.GET,
180
            produces = MediaType.APPLICATION_JSON_VALUE)
181
    @ResponseBody
182 49450 panagiotis
    public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
183 54690 panagiotis
       return repositoryService.getDatasourceVocabularies(mode);
184 49450 panagiotis
    }
185
186 54525 panagiotis
    @RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET,
187
            produces = MediaType.APPLICATION_JSON_VALUE)
188
    @ResponseBody
189 49790 panagiotis
    public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode)  {
190
191 54690 panagiotis
        return repositoryService.getCompatibilityClasses(mode);
192 49790 panagiotis
    }
193
194 54525 panagiotis
    @RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET,
195
            produces = MediaType.APPLICATION_JSON_VALUE)
196
    @ResponseBody
197 49790 panagiotis
    public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode)  {
198 54690 panagiotis
       return repositoryService.getDatasourceClasses(mode);
199 49790 panagiotis
    }
200
201 54525 panagiotis
    @RequestMapping(value = "/getMetricsInfoForRepository/{repoId}",method = RequestMethod.GET,
202
            produces = MediaType.APPLICATION_JSON_VALUE)
203
    @ResponseBody
204 50570 panagiotis
    public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId")  String repoId) throws RepositoryServiceException {
205 54690 panagiotis
        return repositoryService.getMetricsInfoForRepository(repoId);
206 50570 panagiotis
    }
207
208 54525 panagiotis
    @RequestMapping(value = "/getListLatestUpdate/{mode}",method = RequestMethod.GET,
209
            produces = MediaType.APPLICATION_JSON_VALUE)
210
    @ResponseBody
211 51330 panagiotis
    public Map<String, String> getListLatestUpdate(@PathVariable("mode")  String mode) throws JSONException {
212 54690 panagiotis
        return repositoryService.getListLatestUpdate(mode);
213 51330 panagiotis
    }
214
215 54525 panagiotis
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
216
            consumes = MediaType.APPLICATION_JSON_VALUE)
217
    @ResponseBody
218 57549 ioannis.di
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
219 51525 panagiotis
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
220 52781 panagiotis
                                                         @RequestParam("registeredBy") String registeredBy,
221 54702 panagiotis
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
222 54690 panagiotis
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface);
223 51525 panagiotis
    }
224 49236 panagiotis
}