1
|
package eu.dnetlib.organizations.controller;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.Collection;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.LinkedHashSet;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
10
|
import java.util.TreeMap;
|
11
|
import java.util.stream.Collectors;
|
12
|
|
13
|
import org.apache.commons.lang3.StringUtils;
|
14
|
import org.springframework.beans.factory.annotation.Autowired;
|
15
|
import org.springframework.data.domain.Page;
|
16
|
import org.springframework.data.domain.PageRequest;
|
17
|
import org.springframework.security.core.Authentication;
|
18
|
import org.springframework.web.bind.annotation.PathVariable;
|
19
|
import org.springframework.web.bind.annotation.RequestBody;
|
20
|
import org.springframework.web.bind.annotation.RequestMapping;
|
21
|
import org.springframework.web.bind.annotation.RequestMethod;
|
22
|
import org.springframework.web.bind.annotation.RequestParam;
|
23
|
import org.springframework.web.bind.annotation.RestController;
|
24
|
|
25
|
import eu.dnetlib.organizations.model.OpenaireSimRel;
|
26
|
import eu.dnetlib.organizations.model.utils.BrowseEntry;
|
27
|
import eu.dnetlib.organizations.model.utils.RelationByOrg;
|
28
|
import eu.dnetlib.organizations.model.view.ConflictView;
|
29
|
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
|
30
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
31
|
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
|
32
|
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
33
|
import eu.dnetlib.organizations.repository.readonly.ConflictViewRepository;
|
34
|
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
|
35
|
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
|
36
|
import eu.dnetlib.organizations.utils.DatabaseUtils;
|
37
|
import eu.dnetlib.organizations.utils.OpenOrgsConstants;
|
38
|
import eu.dnetlib.organizations.utils.RelationType;
|
39
|
|
40
|
@RestController
|
41
|
@RequestMapping("/api/organizations")
|
42
|
public class OrganizationController {
|
43
|
|
44
|
@Autowired
|
45
|
private OrganizationRepository organizationRepository;
|
46
|
@Autowired
|
47
|
private OrganizationViewRepository organizationViewRepository;
|
48
|
@Autowired
|
49
|
private OrganizationSimpleViewRepository organizationSimpleViewRepository;
|
50
|
@Autowired
|
51
|
private OpenaireSimRelRepository openaireSimRelRepository;
|
52
|
@Autowired
|
53
|
private ConflictViewRepository conflictViewRepository;
|
54
|
@Autowired
|
55
|
private DatabaseUtils databaseUtils;
|
56
|
|
57
|
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
58
|
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
|
59
|
if (StringUtils.isBlank(org.getName())) {
|
60
|
throw new RuntimeException("Missing field: name");
|
61
|
} else if (StringUtils.isBlank(org.getCountry())) {
|
62
|
throw new RuntimeException("Missing field: country");
|
63
|
} else if (StringUtils.isBlank(org.getType())) {
|
64
|
throw new RuntimeException("Missing field: type");
|
65
|
} else if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
|
66
|
final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName(), StringUtils.isNotBlank(org.getId()));
|
67
|
return Arrays.asList(orgId);
|
68
|
} else {
|
69
|
throw new RuntimeException("User not authorized");
|
70
|
}
|
71
|
}
|
72
|
|
73
|
@RequestMapping(value = "/get", method = RequestMethod.GET)
|
74
|
public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
|
75
|
final OrganizationView org = organizationViewRepository.findById(id).get();
|
76
|
|
77
|
if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
|
78
|
return org;
|
79
|
} else {
|
80
|
throw new RuntimeException("User not authorized");
|
81
|
}
|
82
|
}
|
83
|
|
84
|
@RequestMapping(value = "/relations", method = RequestMethod.GET)
|
85
|
public List<RelationByOrg> findRelationsById(@RequestParam final String id, final Authentication authentication) {
|
86
|
if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
|
87
|
return organizationViewRepository.findRelations(id);
|
88
|
} else {
|
89
|
throw new RuntimeException("User not authorized");
|
90
|
}
|
91
|
}
|
92
|
|
93
|
@RequestMapping(value = "/relations", method = RequestMethod.PUT)
|
94
|
public List<RelationByOrg> addRelation(@RequestParam final String from,
|
95
|
@RequestParam final String to,
|
96
|
@RequestParam final RelationType type,
|
97
|
final Authentication authentication) {
|
98
|
if (from.equals(to)) {
|
99
|
throw new IllegalArgumentException("Invalid relation !!!");
|
100
|
} else if (UserInfo.isSuperAdmin(authentication)
|
101
|
|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
|
102
|
|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
|
103
|
|
104
|
databaseUtils.addRelation(from, to, type);
|
105
|
|
106
|
return organizationViewRepository.findRelations(from);
|
107
|
} else {
|
108
|
throw new RuntimeException("User not authorized");
|
109
|
}
|
110
|
|
111
|
}
|
112
|
|
113
|
@RequestMapping(value = "/relations", method = RequestMethod.DELETE)
|
114
|
public List<RelationByOrg> deleteRelation(@RequestParam final String from,
|
115
|
@RequestParam final String to,
|
116
|
@RequestParam final RelationType type,
|
117
|
final Authentication authentication) {
|
118
|
if (from.equals(to)) {
|
119
|
throw new IllegalArgumentException("Invalid relation !!!");
|
120
|
} else if (UserInfo.isSuperAdmin(authentication)
|
121
|
|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
|
122
|
|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
|
123
|
|
124
|
databaseUtils.deleteRelation(from, to, type);
|
125
|
|
126
|
return organizationViewRepository.findRelations(from);
|
127
|
} else {
|
128
|
throw new RuntimeException("User not authorized");
|
129
|
}
|
130
|
|
131
|
}
|
132
|
|
133
|
@RequestMapping(value = "/enrichments", method = RequestMethod.GET)
|
134
|
public List<OpenaireSimRel> findEnrichmentsById(@RequestParam final String id, final Authentication authentication) {
|
135
|
if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
|
136
|
return openaireSimRelRepository.findByLocalId(id)
|
137
|
.stream()
|
138
|
.filter(s -> !s.getOaOriginalId().startsWith(OpenOrgsConstants.OPENORGS_PREFIX))
|
139
|
.collect(Collectors.toList());
|
140
|
} else {
|
141
|
throw new RuntimeException("User not authorized");
|
142
|
}
|
143
|
}
|
144
|
|
145
|
@RequestMapping(value = "/conflicts", method = RequestMethod.GET)
|
146
|
public List<OpenaireSimRel> findConflictsById(@RequestParam final String id, final Authentication authentication) {
|
147
|
if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
|
148
|
return openaireSimRelRepository.findByLocalId(id)
|
149
|
.stream()
|
150
|
.filter(s -> s.getOaOriginalId().startsWith(OpenOrgsConstants.OPENORGS_PREFIX))
|
151
|
.collect(Collectors.toList());
|
152
|
} else {
|
153
|
throw new RuntimeException("User not authorized");
|
154
|
}
|
155
|
}
|
156
|
|
157
|
@RequestMapping(value = "/conflicts/all", method = RequestMethod.GET)
|
158
|
public Collection<Set<OrganizationSimpleView>> findWarningsById(final Authentication authentication) {
|
159
|
|
160
|
final Map<String, Set<OrganizationSimpleView>> res = new TreeMap<>();
|
161
|
|
162
|
final Map<String, String> roots = new HashMap<>();
|
163
|
|
164
|
if (UserInfo.isSuperAdmin(authentication)) {
|
165
|
for (final ConflictView w : conflictViewRepository.findAll()) {
|
166
|
final String root = findWarningRoot(w, roots);
|
167
|
if (!res.containsKey(root)) {
|
168
|
res.put(root, new LinkedHashSet<OrganizationSimpleView>());
|
169
|
res.get(root).add(new OrganizationSimpleView(w.getId1(), w.getName1(), w.getType1(), w.getCity1(), w.getCountry1(), new String[] {}));
|
170
|
}
|
171
|
res.get(root).add(new OrganizationSimpleView(w.getId2(), w.getName2(), w.getType2(), w.getCity2(), w.getCountry2(), new String[] {}));
|
172
|
}
|
173
|
return res.values();
|
174
|
} else {
|
175
|
throw new RuntimeException("User not authorized");
|
176
|
}
|
177
|
}
|
178
|
|
179
|
private String findWarningRoot(final ConflictView w, final Map<String, String> roots) {
|
180
|
if (roots.containsKey(w.getId1())) {
|
181
|
return roots.get(w.getId1());
|
182
|
} else if (roots.containsKey(w.getId2())) {
|
183
|
return roots.get(w.getId2());
|
184
|
} else {
|
185
|
// id1 is the new root
|
186
|
roots.put(w.getId1(), w.getId1());
|
187
|
roots.put(w.getId2(), w.getId1());
|
188
|
return w.getId1();
|
189
|
}
|
190
|
|
191
|
}
|
192
|
|
193
|
@RequestMapping(value = "/enrichments", method = RequestMethod.POST)
|
194
|
public List<OpenaireSimRel> saveEnrichments(@RequestBody final List<OpenaireSimRel> simrels, final Authentication authentication) {
|
195
|
|
196
|
final boolean b = UserInfo.isSuperAdmin(authentication)
|
197
|
|| simrels.stream()
|
198
|
.map(OpenaireSimRel::getLocalId)
|
199
|
.distinct()
|
200
|
.allMatch(id -> organizationRepository.verifyAuthorizationForId(id, authentication.getName()));
|
201
|
|
202
|
if (b) {
|
203
|
return openaireSimRelRepository.saveAll(simrels);
|
204
|
} else {
|
205
|
throw new RuntimeException("User not authorized");
|
206
|
}
|
207
|
}
|
208
|
|
209
|
@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
|
210
|
public Page<OrganizationSimpleView> search(@PathVariable final int page,
|
211
|
@PathVariable final int size,
|
212
|
@RequestParam final String q,
|
213
|
final Authentication authentication) {
|
214
|
return UserInfo.isSuperAdmin(authentication)
|
215
|
? organizationSimpleViewRepository.findByNameContainingIgnoreCase(q, PageRequest.of(page, size))
|
216
|
: organizationSimpleViewRepository.findByNameForUser(q, authentication.getName(), PageRequest.of(page, size));
|
217
|
}
|
218
|
|
219
|
@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
|
220
|
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
|
221
|
@PathVariable final int page,
|
222
|
@PathVariable final int size,
|
223
|
final Authentication authentication) {
|
224
|
if (UserInfo.isSuperAdmin(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
|
225
|
return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
|
226
|
} else {
|
227
|
throw new RuntimeException("User not authorized");
|
228
|
}
|
229
|
}
|
230
|
|
231
|
@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
|
232
|
public Page<OrganizationSimpleView> findByType(@PathVariable final String type,
|
233
|
@PathVariable final int page,
|
234
|
@PathVariable final int size,
|
235
|
final Authentication authentication) {
|
236
|
return UserInfo.isSuperAdmin(authentication)
|
237
|
? organizationSimpleViewRepository.findByType(type, PageRequest.of(page, size))
|
238
|
: organizationSimpleViewRepository.findByTypeForUser(type, authentication.getName(), PageRequest.of(page, size));
|
239
|
}
|
240
|
|
241
|
@RequestMapping(value = "/browse/countries", method = RequestMethod.GET)
|
242
|
public List<BrowseEntry> browseCountries(final Authentication authentication) {
|
243
|
return UserInfo.isSuperAdmin(authentication)
|
244
|
? organizationSimpleViewRepository.browseCountries()
|
245
|
: organizationSimpleViewRepository.browseCountriesForUser(authentication.getName());
|
246
|
}
|
247
|
|
248
|
@RequestMapping(value = "/browse/types", method = RequestMethod.GET)
|
249
|
public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
|
250
|
return UserInfo.isSuperAdmin(authentication)
|
251
|
? organizationSimpleViewRepository.browseTypes()
|
252
|
: organizationSimpleViewRepository.browseTypesForUser(authentication.getName());
|
253
|
}
|
254
|
|
255
|
}
|