1
|
package eu.dnetlib.organizations.controller;
|
2
|
|
3
|
import java.security.Principal;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
|
7
|
import org.apache.commons.lang3.StringUtils;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.data.domain.Page;
|
10
|
import org.springframework.data.domain.PageRequest;
|
11
|
import org.springframework.web.bind.annotation.PathVariable;
|
12
|
import org.springframework.web.bind.annotation.RequestBody;
|
13
|
import org.springframework.web.bind.annotation.RequestMapping;
|
14
|
import org.springframework.web.bind.annotation.RequestMethod;
|
15
|
import org.springframework.web.bind.annotation.RequestParam;
|
16
|
import org.springframework.web.bind.annotation.RestController;
|
17
|
|
18
|
import eu.dnetlib.organizations.model.OpenaireSimRel;
|
19
|
import eu.dnetlib.organizations.model.utils.BrowseEntry;
|
20
|
import eu.dnetlib.organizations.model.utils.RelationByOrg;
|
21
|
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
|
22
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
23
|
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
|
24
|
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
|
25
|
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
|
26
|
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
27
|
import eu.dnetlib.organizations.utils.RelationType;
|
28
|
|
29
|
@RestController
|
30
|
@RequestMapping("/api/organizations")
|
31
|
public class OrganizationController {
|
32
|
|
33
|
@Autowired
|
34
|
private OrganizationViewRepository organizationViewRepository;
|
35
|
@Autowired
|
36
|
private OrganizationSimpleViewRepository organizationSimpleViewRepository;
|
37
|
@Autowired
|
38
|
private OpenaireSimRelRepository openaireSimRelRepository;
|
39
|
@Autowired
|
40
|
private DatabaseUtils databaseUtils;
|
41
|
|
42
|
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
43
|
public List<String> save(@RequestBody final OrganizationView org, final Principal principal) {
|
44
|
final String user = principal != null ? principal.getName() : "anonymous";
|
45
|
final String orgId = databaseUtils.insertOrUpdateOrganization(org, user, StringUtils.isNotBlank(org.getId()));
|
46
|
return Arrays.asList(orgId);
|
47
|
}
|
48
|
|
49
|
@RequestMapping(value = "/get", method = RequestMethod.GET)
|
50
|
public OrganizationView findById(@RequestParam final String id) {
|
51
|
return organizationViewRepository.findById(id).get();
|
52
|
}
|
53
|
|
54
|
@RequestMapping(value = "/relations", method = RequestMethod.GET)
|
55
|
public List<RelationByOrg> findRelationsById(@RequestParam final String id) {
|
56
|
return organizationViewRepository.findRelations(id);
|
57
|
}
|
58
|
|
59
|
@RequestMapping(value = "/relations", method = RequestMethod.PUT)
|
60
|
public List<RelationByOrg> addRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) {
|
61
|
if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); }
|
62
|
|
63
|
databaseUtils.addRelation(from, to, type);
|
64
|
return organizationViewRepository.findRelations(from);
|
65
|
}
|
66
|
|
67
|
@RequestMapping(value = "/relations", method = RequestMethod.DELETE)
|
68
|
public List<RelationByOrg> deleteRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) {
|
69
|
if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); }
|
70
|
|
71
|
databaseUtils.deleteRelation(from, to, type);
|
72
|
return organizationViewRepository.findRelations(from);
|
73
|
}
|
74
|
|
75
|
@RequestMapping(value = "/similarities", method = RequestMethod.GET)
|
76
|
public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id) {
|
77
|
return openaireSimRelRepository.findByLocalId(id);
|
78
|
}
|
79
|
|
80
|
@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
|
81
|
public Page<OrganizationSimpleView> search(@PathVariable final int page, @PathVariable final int size, @RequestParam final String q) {
|
82
|
return organizationSimpleViewRepository.findByNameContainingIgnoreCase(q, PageRequest.of(page, size));
|
83
|
}
|
84
|
|
85
|
@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
|
86
|
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code, @PathVariable final int page, @PathVariable final int size) {
|
87
|
return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
|
88
|
}
|
89
|
|
90
|
@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
|
91
|
public Page<OrganizationSimpleView> findByType(@PathVariable final String type, @PathVariable final int page, @PathVariable final int size) {
|
92
|
return organizationSimpleViewRepository.findByType(type, PageRequest.of(page, size));
|
93
|
}
|
94
|
|
95
|
@RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
|
96
|
public List<BrowseEntry> browseCountries() {
|
97
|
return organizationSimpleViewRepository.browseCountries();
|
98
|
}
|
99
|
|
100
|
@RequestMapping(value = "/browse/types", method = RequestMethod.GET)
|
101
|
public List<BrowseEntry> browseOrganizationTypes() {
|
102
|
return organizationSimpleViewRepository.browseTypes();
|
103
|
}
|
104
|
}
|