1
|
package eu.dnetlib.organizations.utils;
|
2
|
|
3
|
import java.time.OffsetDateTime;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.HashSet;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Map.Entry;
|
10
|
import java.util.Set;
|
11
|
import java.util.TreeSet;
|
12
|
import java.util.UUID;
|
13
|
import java.util.stream.Collectors;
|
14
|
|
15
|
import javax.transaction.Transactional;
|
16
|
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
import org.springframework.cache.annotation.Cacheable;
|
19
|
import org.springframework.jdbc.core.JdbcTemplate;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
import org.springframework.web.bind.annotation.RequestBody;
|
22
|
|
23
|
import eu.dnetlib.organizations.controller.UserRole;
|
24
|
import eu.dnetlib.organizations.model.Acronym;
|
25
|
import eu.dnetlib.organizations.model.OpenaireConflict;
|
26
|
import eu.dnetlib.organizations.model.OpenaireConflictPK;
|
27
|
import eu.dnetlib.organizations.model.Organization;
|
28
|
import eu.dnetlib.organizations.model.OtherIdentifier;
|
29
|
import eu.dnetlib.organizations.model.OtherName;
|
30
|
import eu.dnetlib.organizations.model.Relationship;
|
31
|
import eu.dnetlib.organizations.model.Url;
|
32
|
import eu.dnetlib.organizations.model.User;
|
33
|
import eu.dnetlib.organizations.model.UserCountry;
|
34
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
35
|
import eu.dnetlib.organizations.model.view.UserView;
|
36
|
import eu.dnetlib.organizations.repository.AcronymRepository;
|
37
|
import eu.dnetlib.organizations.repository.OpenaireConflictRepository;
|
38
|
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
39
|
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
|
40
|
import eu.dnetlib.organizations.repository.OtherNameRepository;
|
41
|
import eu.dnetlib.organizations.repository.RelationshipRepository;
|
42
|
import eu.dnetlib.organizations.repository.UrlRepository;
|
43
|
import eu.dnetlib.organizations.repository.UserCountryRepository;
|
44
|
import eu.dnetlib.organizations.repository.UserRepository;
|
45
|
|
46
|
@Component
|
47
|
public class DatabaseUtils {
|
48
|
|
49
|
@Autowired
|
50
|
private AcronymRepository acronymRepository;
|
51
|
@Autowired
|
52
|
private OrganizationRepository organizationRepository;
|
53
|
@Autowired
|
54
|
private OtherIdentifierRepository otherIdentifierRepository;
|
55
|
@Autowired
|
56
|
private OtherNameRepository otherNameRepository;
|
57
|
@Autowired
|
58
|
private UrlRepository urlRepository;
|
59
|
@Autowired
|
60
|
private RelationshipRepository relationshipRepository;
|
61
|
@Autowired
|
62
|
private UserRepository userRepository;
|
63
|
@Autowired
|
64
|
private UserCountryRepository userCountryRepository;
|
65
|
@Autowired
|
66
|
private OpenaireConflictRepository openaireConflictRepository;
|
67
|
@Autowired
|
68
|
private JdbcTemplate jdbcTemplate;
|
69
|
|
70
|
public enum VocabularyTable {
|
71
|
languages, countries, org_types, id_types, rel_types, simrel_types
|
72
|
}
|
73
|
|
74
|
@Transactional
|
75
|
public String insertOrUpdateOrganization(final OrganizationView orgView, final String user, final boolean update) {
|
76
|
|
77
|
if (update) {
|
78
|
cleanOldRelations(orgView.getId());
|
79
|
}
|
80
|
|
81
|
final Organization org = new Organization(update ? orgView.getId() : null,
|
82
|
orgView.getName(),
|
83
|
orgView.getType(),
|
84
|
orgView.getLat(), orgView.getLng(),
|
85
|
orgView.getCity(), orgView.getCountry());
|
86
|
|
87
|
final String orgId = organizationRepository.save(org).getId();
|
88
|
|
89
|
makeNewRelations(orgView, orgId);
|
90
|
|
91
|
updateHistoryFields(orgId, user, update);
|
92
|
|
93
|
return orgId;
|
94
|
}
|
95
|
|
96
|
private void updateHistoryFields(final String id, final String user, final boolean update) {
|
97
|
final OffsetDateTime now = OffsetDateTime.now();
|
98
|
if (update) {
|
99
|
organizationRepository.updateModificationDate(id, user, now);
|
100
|
} else {
|
101
|
organizationRepository.updateCreationDate(id, user, now);
|
102
|
organizationRepository.updateModificationDate(id, user, now);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
private void makeNewRelations(final OrganizationView orgView, final String orgId) {
|
107
|
orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
|
108
|
orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
|
109
|
orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
|
110
|
orgView.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
|
111
|
orgView.getRelations().forEach(r -> makeRelation(orgId, r.getRelatedOrgId(), RelationType.valueOf(r.getType())));
|
112
|
}
|
113
|
|
114
|
private void cleanOldRelations(final String id) {
|
115
|
acronymRepository.deleteByOrgId(id);
|
116
|
otherNameRepository.deleteByOrgId(id);
|
117
|
otherIdentifierRepository.deleteByOrgId(id);
|
118
|
urlRepository.deleteByOrgId(id);
|
119
|
relationshipRepository.deleteById1(id);
|
120
|
relationshipRepository.deleteById2(id);
|
121
|
}
|
122
|
|
123
|
@Cacheable("vocs")
|
124
|
public List<String> listValuesOfVocabularyTable(final VocabularyTable table) {
|
125
|
return jdbcTemplate.queryForList("select val from " + table, String.class);
|
126
|
}
|
127
|
|
128
|
@Cacheable("countries_for_user")
|
129
|
public List<String> listCountriesForUser(final String name) {
|
130
|
return jdbcTemplate.queryForList("select country from user_countries where email = ?", String.class, name);
|
131
|
}
|
132
|
|
133
|
@Transactional
|
134
|
public void saveUser(@RequestBody final UserView userView) {
|
135
|
final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
|
136
|
user.setRole(userView.getRole());
|
137
|
user.setValid(userView.isValid());
|
138
|
userRepository.save(user);
|
139
|
userCountryRepository.deleteByEmail(userView.getEmail());
|
140
|
if (userView.getCountries() != null) {
|
141
|
userCountryRepository
|
142
|
.saveAll(Arrays.stream(userView.getCountries()).map(c -> new UserCountry(userView.getEmail(), c)).collect(Collectors.toList()));
|
143
|
}
|
144
|
}
|
145
|
|
146
|
@Transactional
|
147
|
public void deleteUser(final String email) {
|
148
|
userCountryRepository.deleteByEmail(email);
|
149
|
userRepository.deleteById(email);
|
150
|
}
|
151
|
|
152
|
@Transactional
|
153
|
public void newUser(final String email, final List<String> countries) {
|
154
|
final User user = new User();
|
155
|
user.setEmail(email);
|
156
|
user.setRole(UserRole.PENDING.name());
|
157
|
user.setValid(false);
|
158
|
userRepository.save(user);
|
159
|
if (countries != null) {
|
160
|
userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
|
161
|
}
|
162
|
}
|
163
|
|
164
|
@Transactional
|
165
|
public void verifyConflictGroups(final boolean forceUpdate) {
|
166
|
|
167
|
if (forceUpdate || openaireConflictRepository.countByGroupNull() > 0) {
|
168
|
|
169
|
openaireConflictRepository.resetGroupIds();
|
170
|
|
171
|
final Map<String, Set<String>> groups = new HashMap<>();
|
172
|
for (final OpenaireConflict w : openaireConflictRepository.findAll()) {
|
173
|
final List<String> list = findExistingGroupsForRel(w, groups);
|
174
|
if (list.isEmpty()) {
|
175
|
final String idGroup = generateGroupId();
|
176
|
groups.put(idGroup, new HashSet<>());
|
177
|
addToGroup(groups, idGroup, w);
|
178
|
} else if (list.size() == 1) {
|
179
|
addToGroup(groups, list.get(0), w);
|
180
|
} else {
|
181
|
final String idGroup = generateGroupId();
|
182
|
groups.put(idGroup, new TreeSet<>());
|
183
|
list.forEach(id -> groups.get(idGroup).addAll(groups.get(id)));
|
184
|
list.forEach(id -> groups.remove(id));
|
185
|
addToGroup(groups, idGroup, w);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
for (final Entry<String, Set<String>> e : groups.entrySet()) {
|
190
|
final String gid = e.getKey();
|
191
|
for (final String orgId : e.getValue()) {
|
192
|
for (final OpenaireConflict oc : openaireConflictRepository.findById1AndGroupIsNull(orgId)) {
|
193
|
oc.setGroup(gid);
|
194
|
openaireConflictRepository.save(oc);
|
195
|
}
|
196
|
for (final OpenaireConflict oc : openaireConflictRepository.findById2AndGroupIsNull(orgId)) {
|
197
|
oc.setGroup(gid);
|
198
|
openaireConflictRepository.save(oc);
|
199
|
}
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
|
205
|
private String generateGroupId() {
|
206
|
return "group::" + UUID.randomUUID();
|
207
|
}
|
208
|
|
209
|
private List<String> findExistingGroupsForRel(final OpenaireConflict w, final Map<String, Set<String>> groups) {
|
210
|
return groups.entrySet()
|
211
|
.stream()
|
212
|
.filter(e -> {
|
213
|
return e.getValue().contains(w.getId1()) || e.getValue().contains(w.getId2());
|
214
|
})
|
215
|
.map(e -> e.getKey())
|
216
|
.distinct()
|
217
|
.collect(Collectors.toList());
|
218
|
}
|
219
|
|
220
|
private void addToGroup(final Map<String, Set<String>> groups, final String gid, final OpenaireConflict w) {
|
221
|
groups.get(gid).add(w.getId1());
|
222
|
groups.get(gid).add(w.getId2());
|
223
|
}
|
224
|
|
225
|
@Transactional
|
226
|
public List<Relationship> makeRelation(final String id1, final String id2, final RelationType type) {
|
227
|
final Relationship r1 = new Relationship(id1, id2, type.toString());
|
228
|
final Relationship r2 = new Relationship(id2, id1, type.getInverse().toString());
|
229
|
relationshipRepository.save(r1);
|
230
|
relationshipRepository.save(r2);
|
231
|
|
232
|
if (type == RelationType.Merged_In || type == RelationType.Merges) {
|
233
|
openaireConflictRepository.findById(new OpenaireConflictPK(id1, id2)).ifPresent(openaireConflictRepository::delete);
|
234
|
openaireConflictRepository.findById(new OpenaireConflictPK(id2, id1)).ifPresent(openaireConflictRepository::delete);
|
235
|
}
|
236
|
|
237
|
return Arrays.asList(r1, r2);
|
238
|
}
|
239
|
}
|