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.*;
7
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
8
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
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.PostAuthorize;
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 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
    private 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
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER'))")
75
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
76
        Repository repo = repositoryService.getRepositoryById(id);
77
        return repo;
78
    }
79

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

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

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

    
103
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
104
            produces = MediaType.APPLICATION_JSON_VALUE)
105
    @ResponseBody
106
    @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'))")
107
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
108
        return repositoryService.getRepositoryInterface(id);
109
    }
110

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

    
118
        return repositoryService.addRepository(datatype, repository);
119
    }
120

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

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

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

    
142
    @RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
143
            consumes = MediaType.APPLICATION_JSON_VALUE)
144
    @ResponseBody
145
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or #repository.registeredBy=='null') and hasRole('ROLE_USER'))")
146
    public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception {
147
        return repositoryService.updateRepository(repository, authentication);
148
    }
149

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

    
157
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
158
            consumes = MediaType.APPLICATION_JSON_VALUE)
159
    @ResponseBody
160
    @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'))")
161
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
162
                                                      @RequestParam("repoId") String repoId,
163
                                                      @RequestParam("registeredBy") String registeredBy,
164
                                                      @RequestBody RepositoryInterface repositoryInterface) throws JSONException, ResourceNotFoundException, ValidatorServiceException {
165
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, repositoryInterface);
166
    }
167

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

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

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

    
190
        return repositoryService.getCompatibilityClasses(mode);
191
    }
192

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

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

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

    
214
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
215
            consumes = MediaType.APPLICATION_JSON_VALUE)
216
    @ResponseBody
217
    @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'))")
218
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
219
                                                         @RequestParam("registeredBy") String registeredBy,
220
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
221
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface);
222
    }
223
}
(6-6/11)