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