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.RepositorySnippet;
7
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
8
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
9
import eu.dnetlib.repo.manager.shared.*;
10
import io.swagger.annotations.Api;
11
import org.json.JSONException;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.MediaType;
14
import org.springframework.security.access.prepost.PostAuthorize;
15
import org.springframework.security.access.prepost.PostFilter;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.security.core.Authentication;
18
import org.springframework.security.core.context.SecurityContextHolder;
19
import org.springframework.web.bind.annotation.*;
20

    
21
import javax.ws.rs.Path;
22
import java.io.IOException;
23
import java.util.List;
24
import java.util.Map;
25

    
26
@RestController
27
@RequestMapping(value = "/repository")
28
@Api(description = "Repository API",  tags = {"repository"})
29
public class RepositoryController {
30

    
31
    @Autowired
32
    private RepositoryServiceImpl repositoryService;
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/{userEmail}/{page}/{size}",method = RequestMethod.GET,
50
            produces = MediaType.APPLICATION_JSON_VALUE)
51
    @ResponseBody
52
    @PreAuthorize("hasRole('ROLE_USER')")
53
    public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
54
                                                  @PathVariable("page") String page,
55
                                                  @PathVariable("size") String size) throws JSONException {
56
       return repositoryService.getRepositoriesOfUser(userEmail, page, size);
57
    }
58

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

    
72
        return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
73
    }
74

    
75
    @RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
76
            produces = MediaType.APPLICATION_JSON_VALUE)
77
    @ResponseBody
78
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (returnObject.registeredBy==authentication.userInfo.email and hasRole('ROLE_USER'))")
79
    public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
80
        return repositoryService.getRepositoryById(id);
81
    }
82

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

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

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

    
106
    @RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
107
            produces = MediaType.APPLICATION_JSON_VALUE)
108
    @ResponseBody
109
    @PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (@repositoryService.getRepositoryById(#id).registeredBy==authentication.userInfo.email and hasRole('ROLE_USER'))")
110
    public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
111
        return repositoryService.getRepositoryInterface(id);
112
    }
113

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

    
121
        return repositoryService.addRepository(datatype, repository);
122
    }
123

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

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

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

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

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

    
160
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
161
            consumes = MediaType.APPLICATION_JSON_VALUE)
162
    @ResponseBody
163
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email and hasRole('ROLE_USER'))")
164
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
165
                                                      @RequestParam("repoId") String repoId,
166
                                                      @RequestParam("registeredBy") String registeredBy,
167
                                                      @RequestBody RepositoryInterface repositoryInterface) throws JSONException, ResourceNotFoundException, ValidatorServiceException {
168
        return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, repositoryInterface);
169
    }
170

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

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

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

    
193
        return repositoryService.getCompatibilityClasses(mode);
194
    }
195

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

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

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

    
217
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
218
            consumes = MediaType.APPLICATION_JSON_VALUE)
219
    @ResponseBody
220
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email and hasRole('ROLE_USER'))")
221
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
222
                                                         @RequestParam("registeredBy") String registeredBy,
223
                                                         @RequestBody RepositoryInterface repositoryInterface) throws Exception {
224
        return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface);
225
    }
226
}
(6-6/11)