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