1
|
package eu.dnetlib.organizations.utils;
|
2
|
|
3
|
import java.time.OffsetDateTime;
|
4
|
import java.util.List;
|
5
|
|
6
|
import javax.transaction.Transactional;
|
7
|
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.cache.annotation.Cacheable;
|
11
|
import org.springframework.jdbc.core.JdbcTemplate;
|
12
|
import org.springframework.stereotype.Component;
|
13
|
|
14
|
import eu.dnetlib.organizations.model.Acronym;
|
15
|
import eu.dnetlib.organizations.model.Organization;
|
16
|
import eu.dnetlib.organizations.model.OtherIdentifier;
|
17
|
import eu.dnetlib.organizations.model.OtherName;
|
18
|
import eu.dnetlib.organizations.model.Relationship;
|
19
|
import eu.dnetlib.organizations.model.Url;
|
20
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
21
|
import eu.dnetlib.organizations.repository.AcronymRepository;
|
22
|
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
23
|
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
|
24
|
import eu.dnetlib.organizations.repository.OtherNameRepository;
|
25
|
import eu.dnetlib.organizations.repository.RelationshipRepository;
|
26
|
import eu.dnetlib.organizations.repository.UrlRepository;
|
27
|
|
28
|
@Component
|
29
|
public class DatabaseUtils {
|
30
|
|
31
|
@Autowired
|
32
|
private AcronymRepository acronymRepository;
|
33
|
@Autowired
|
34
|
private OrganizationRepository organizationRepository;
|
35
|
@Autowired
|
36
|
private OtherIdentifierRepository otherIdentifierRepository;
|
37
|
@Autowired
|
38
|
private OtherNameRepository otherNameRepository;
|
39
|
@Autowired
|
40
|
private UrlRepository urlRepository;
|
41
|
@Autowired
|
42
|
private RelationshipRepository relationshipRepository;
|
43
|
|
44
|
@Autowired
|
45
|
private JdbcTemplate jdbcTemplate;
|
46
|
|
47
|
public enum VocabularyTable {
|
48
|
languages, countries, org_types, id_types, rel_types, simrel_types
|
49
|
}
|
50
|
|
51
|
@Transactional
|
52
|
public String insertOrUpdateOrganization(final OrganizationView orgView, final String user, final boolean update) {
|
53
|
|
54
|
if (update) {
|
55
|
cleanOldRelations(orgView.getId());
|
56
|
}
|
57
|
|
58
|
final Organization org = new Organization(update ? orgView.getId() : null,
|
59
|
orgView.getName(),
|
60
|
StringUtils.isNotBlank(orgView.getType()) ? orgView.getType() : "UNKNOWN",
|
61
|
orgView.getLat(), orgView.getLng(),
|
62
|
orgView.getCity(), orgView.getCountry());
|
63
|
|
64
|
final String orgId = organizationRepository.save(org).getId();
|
65
|
|
66
|
makeNewRelations(orgView, orgId);
|
67
|
|
68
|
updateHistoryFields(orgId, user, update);
|
69
|
|
70
|
return orgId;
|
71
|
}
|
72
|
|
73
|
private void updateHistoryFields(final String id, final String user, final boolean update) {
|
74
|
final OffsetDateTime now = OffsetDateTime.now();
|
75
|
if (update) {
|
76
|
organizationRepository.updateModificationDate(id, user, now);
|
77
|
} else {
|
78
|
organizationRepository.updateCreationDate(id, user, now);
|
79
|
organizationRepository.updateModificationDate(id, user, now);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
private void makeNewRelations(final OrganizationView orgView, final String orgId) {
|
84
|
orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
|
85
|
orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
|
86
|
orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
|
87
|
orgView.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
|
88
|
}
|
89
|
|
90
|
private void cleanOldRelations(final String id) {
|
91
|
acronymRepository.deleteByOrgId(id);
|
92
|
otherNameRepository.deleteByOrgId(id);
|
93
|
otherIdentifierRepository.deleteByOrgId(id);
|
94
|
urlRepository.deleteByOrgId(id);
|
95
|
}
|
96
|
|
97
|
@Cacheable("vocs")
|
98
|
public List<String> listValuesOfVocabularyTable(final VocabularyTable table) {
|
99
|
return jdbcTemplate.queryForList("select val from " + table, String.class);
|
100
|
}
|
101
|
|
102
|
@Transactional
|
103
|
public void addRelation(final String from, final String to, final RelationType type) {
|
104
|
final Relationship r1 = new Relationship(from, to, type.toString());
|
105
|
final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
|
106
|
relationshipRepository.save(r1);
|
107
|
relationshipRepository.save(r2);
|
108
|
}
|
109
|
|
110
|
@Transactional
|
111
|
public void deleteRelation(final String from, final String to, final RelationType type) {
|
112
|
final Relationship r1 = new Relationship(from, to, type.toString());
|
113
|
final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
|
114
|
relationshipRepository.delete(r1);
|
115
|
relationshipRepository.delete(r2);
|
116
|
}
|
117
|
|
118
|
}
|