21 |
21 |
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
|
22 |
22 |
import eu.dnetlib.organizations.model.view.OrganizationView;
|
23 |
23 |
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
|
|
24 |
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
24 |
25 |
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
|
25 |
26 |
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
|
26 |
27 |
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
... | ... | |
31 |
32 |
public class OrganizationController {
|
32 |
33 |
|
33 |
34 |
@Autowired
|
|
35 |
private OrganizationRepository organizationRepository;
|
|
36 |
@Autowired
|
34 |
37 |
private OrganizationViewRepository organizationViewRepository;
|
35 |
38 |
@Autowired
|
36 |
39 |
private OrganizationSimpleViewRepository organizationSimpleViewRepository;
|
... | ... | |
41 |
44 |
|
42 |
45 |
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
43 |
46 |
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
|
44 |
|
final String user = authentication != null ? authentication.getName() : "anonymous";
|
45 |
|
final String orgId = databaseUtils.insertOrUpdateOrganization(org, user, StringUtils.isNotBlank(org.getId()));
|
46 |
|
return Arrays.asList(orgId);
|
|
47 |
if (StringUtils.isBlank(org.getName())) {
|
|
48 |
throw new RuntimeException("Missing field: name");
|
|
49 |
} else if (StringUtils.isBlank(org.getCountry())) {
|
|
50 |
throw new RuntimeException("Missing field: country");
|
|
51 |
} else if (StringUtils.isBlank(org.getType())) {
|
|
52 |
throw new RuntimeException("Missing field: type");
|
|
53 |
} else if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
|
|
54 |
final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName(), StringUtils.isNotBlank(org.getId()));
|
|
55 |
return Arrays.asList(orgId);
|
|
56 |
} else {
|
|
57 |
throw new RuntimeException("User not authorized");
|
|
58 |
}
|
47 |
59 |
}
|
48 |
60 |
|
49 |
61 |
@RequestMapping(value = "/get", method = RequestMethod.GET)
|
50 |
|
public OrganizationView findById(@RequestParam final String id) {
|
51 |
|
return organizationViewRepository.findById(id).get();
|
|
62 |
public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
|
|
63 |
final OrganizationView org = organizationViewRepository.findById(id).get();
|
|
64 |
|
|
65 |
if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
|
|
66 |
return org;
|
|
67 |
} else {
|
|
68 |
throw new RuntimeException("User not authorized");
|
|
69 |
}
|
52 |
70 |
}
|
53 |
71 |
|
54 |
72 |
@RequestMapping(value = "/relations", method = RequestMethod.GET)
|
55 |
|
public List<RelationByOrg> findRelationsById(@RequestParam final String id) {
|
56 |
|
return organizationViewRepository.findRelations(id);
|
|
73 |
public List<RelationByOrg> findRelationsById(@RequestParam final String id, final Authentication authentication) {
|
|
74 |
if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
|
|
75 |
return organizationViewRepository.findRelations(id);
|
|
76 |
} else {
|
|
77 |
throw new RuntimeException("User not authorized");
|
|
78 |
}
|
57 |
79 |
}
|
58 |
80 |
|
59 |
81 |
@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 !!!"); }
|
|
82 |
public List<RelationByOrg> addRelation(@RequestParam final String from,
|
|
83 |
@RequestParam final String to,
|
|
84 |
@RequestParam final RelationType type,
|
|
85 |
final Authentication authentication) {
|
|
86 |
if (from.equals(to)) {
|
|
87 |
throw new IllegalArgumentException("Invalid relation !!!");
|
|
88 |
} else if (User.isSuperUser(authentication)
|
|
89 |
|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
|
|
90 |
|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
|
62 |
91 |
|
63 |
|
databaseUtils.addRelation(from, to, type);
|
64 |
|
return organizationViewRepository.findRelations(from);
|
|
92 |
databaseUtils.addRelation(from, to, type);
|
|
93 |
|
|
94 |
return organizationViewRepository.findRelations(from);
|
|
95 |
} else {
|
|
96 |
throw new RuntimeException("User not authorized");
|
|
97 |
}
|
|
98 |
|
65 |
99 |
}
|
66 |
100 |
|
67 |
101 |
@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 !!!"); }
|
|
102 |
public List<RelationByOrg> deleteRelation(@RequestParam final String from,
|
|
103 |
@RequestParam final String to,
|
|
104 |
@RequestParam final RelationType type,
|
|
105 |
final Authentication authentication) {
|
|
106 |
if (from.equals(to)) {
|
|
107 |
throw new IllegalArgumentException("Invalid relation !!!");
|
|
108 |
} else if (User.isSuperUser(authentication)
|
|
109 |
|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
|
|
110 |
|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
|
70 |
111 |
|
71 |
|
databaseUtils.deleteRelation(from, to, type);
|
72 |
|
return organizationViewRepository.findRelations(from);
|
|
112 |
databaseUtils.deleteRelation(from, to, type);
|
|
113 |
|
|
114 |
return organizationViewRepository.findRelations(from);
|
|
115 |
} else {
|
|
116 |
throw new RuntimeException("User not authorized");
|
|
117 |
}
|
|
118 |
|
73 |
119 |
}
|
74 |
120 |
|
75 |
121 |
@RequestMapping(value = "/similarities", method = RequestMethod.GET)
|
76 |
|
public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id) {
|
77 |
|
return openaireSimRelRepository.findByLocalId(id);
|
|
122 |
public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id, final Authentication authentication) {
|
|
123 |
if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
|
|
124 |
return openaireSimRelRepository.findByLocalId(id);
|
|
125 |
} else {
|
|
126 |
throw new RuntimeException("User not authorized");
|
|
127 |
}
|
78 |
128 |
}
|
79 |
129 |
|
80 |
130 |
@RequestMapping(value = "/similarities", method = RequestMethod.POST)
|
81 |
|
public List<OpenaireSimRel> saveSimilarities(@RequestBody final List<OpenaireSimRel> simrels) {
|
82 |
|
return openaireSimRelRepository.saveAll(simrels);
|
|
131 |
public List<OpenaireSimRel> saveSimilarities(@RequestBody final List<OpenaireSimRel> simrels, final Authentication authentication) {
|
|
132 |
|
|
133 |
final boolean b = simrels.stream()
|
|
134 |
.map(OpenaireSimRel::getLocalId)
|
|
135 |
.distinct()
|
|
136 |
.allMatch(id -> organizationRepository.verifyAuthorizationForId(id, authentication.getName()));
|
|
137 |
|
|
138 |
if (b) {
|
|
139 |
return openaireSimRelRepository.saveAll(simrels);
|
|
140 |
} else {
|
|
141 |
throw new RuntimeException("User not authorized");
|
|
142 |
}
|
83 |
143 |
}
|
84 |
144 |
|
85 |
145 |
@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
|
... | ... | |
93 |
153 |
}
|
94 |
154 |
|
95 |
155 |
@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
|
96 |
|
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code, @PathVariable final int page, @PathVariable final int size) {
|
97 |
|
|
98 |
|
return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
|
|
156 |
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
|
|
157 |
@PathVariable final int page,
|
|
158 |
@PathVariable final int size,
|
|
159 |
final Authentication authentication) {
|
|
160 |
if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
|
|
161 |
return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
|
|
162 |
} else {
|
|
163 |
throw new RuntimeException("User not authorized");
|
|
164 |
}
|
99 |
165 |
}
|
100 |
166 |
|
101 |
167 |
@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
|