1
|
package eu.dnetlib.organizations.utils;
|
2
|
|
3
|
import java.time.OffsetDateTime;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import javax.transaction.Transactional;
|
9
|
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.cache.annotation.Cacheable;
|
12
|
import org.springframework.jdbc.core.JdbcTemplate;
|
13
|
import org.springframework.stereotype.Component;
|
14
|
import org.springframework.web.bind.annotation.RequestBody;
|
15
|
|
16
|
import eu.dnetlib.organizations.controller.UserRole;
|
17
|
import eu.dnetlib.organizations.model.Acronym;
|
18
|
import eu.dnetlib.organizations.model.Organization;
|
19
|
import eu.dnetlib.organizations.model.OtherIdentifier;
|
20
|
import eu.dnetlib.organizations.model.OtherName;
|
21
|
import eu.dnetlib.organizations.model.Relationship;
|
22
|
import eu.dnetlib.organizations.model.Url;
|
23
|
import eu.dnetlib.organizations.model.User;
|
24
|
import eu.dnetlib.organizations.model.UserCountry;
|
25
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
26
|
import eu.dnetlib.organizations.model.view.UserView;
|
27
|
import eu.dnetlib.organizations.repository.AcronymRepository;
|
28
|
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
29
|
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
|
30
|
import eu.dnetlib.organizations.repository.OtherNameRepository;
|
31
|
import eu.dnetlib.organizations.repository.RelationshipRepository;
|
32
|
import eu.dnetlib.organizations.repository.UrlRepository;
|
33
|
import eu.dnetlib.organizations.repository.UserCountryRepository;
|
34
|
import eu.dnetlib.organizations.repository.UserRepository;
|
35
|
|
36
|
@Component
|
37
|
public class DatabaseUtils {
|
38
|
|
39
|
@Autowired
|
40
|
private AcronymRepository acronymRepository;
|
41
|
@Autowired
|
42
|
private OrganizationRepository organizationRepository;
|
43
|
@Autowired
|
44
|
private OtherIdentifierRepository otherIdentifierRepository;
|
45
|
@Autowired
|
46
|
private OtherNameRepository otherNameRepository;
|
47
|
@Autowired
|
48
|
private UrlRepository urlRepository;
|
49
|
@Autowired
|
50
|
private RelationshipRepository relationshipRepository;
|
51
|
@Autowired
|
52
|
private UserRepository userRepository;
|
53
|
@Autowired
|
54
|
private UserCountryRepository userCountryRepository;
|
55
|
|
56
|
@Autowired
|
57
|
private JdbcTemplate jdbcTemplate;
|
58
|
|
59
|
public enum VocabularyTable {
|
60
|
languages, countries, org_types, id_types, rel_types, simrel_types
|
61
|
}
|
62
|
|
63
|
@Transactional
|
64
|
public String insertOrUpdateOrganization(final OrganizationView orgView, final String user, final boolean update) {
|
65
|
|
66
|
if (update) {
|
67
|
cleanOldRelations(orgView.getId());
|
68
|
}
|
69
|
|
70
|
final Organization org = new Organization(update ? orgView.getId() : null,
|
71
|
orgView.getName(),
|
72
|
orgView.getType(),
|
73
|
orgView.getLat(), orgView.getLng(),
|
74
|
orgView.getCity(), orgView.getCountry());
|
75
|
|
76
|
final String orgId = organizationRepository.save(org).getId();
|
77
|
|
78
|
makeNewRelations(orgView, orgId);
|
79
|
|
80
|
updateHistoryFields(orgId, user, update);
|
81
|
|
82
|
return orgId;
|
83
|
}
|
84
|
|
85
|
private void updateHistoryFields(final String id, final String user, final boolean update) {
|
86
|
final OffsetDateTime now = OffsetDateTime.now();
|
87
|
if (update) {
|
88
|
organizationRepository.updateModificationDate(id, user, now);
|
89
|
} else {
|
90
|
organizationRepository.updateCreationDate(id, user, now);
|
91
|
organizationRepository.updateModificationDate(id, user, now);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
private void makeNewRelations(final OrganizationView orgView, final String orgId) {
|
96
|
orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
|
97
|
orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
|
98
|
orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
|
99
|
orgView.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
|
100
|
}
|
101
|
|
102
|
private void cleanOldRelations(final String id) {
|
103
|
acronymRepository.deleteByOrgId(id);
|
104
|
otherNameRepository.deleteByOrgId(id);
|
105
|
otherIdentifierRepository.deleteByOrgId(id);
|
106
|
urlRepository.deleteByOrgId(id);
|
107
|
}
|
108
|
|
109
|
@Cacheable("vocs")
|
110
|
public List<String> listValuesOfVocabularyTable(final VocabularyTable table) {
|
111
|
return jdbcTemplate.queryForList("select val from " + table, String.class);
|
112
|
}
|
113
|
|
114
|
@Transactional
|
115
|
public void addRelation(final String from, final String to, final RelationType type) {
|
116
|
final Relationship r1 = new Relationship(from, to, type.toString());
|
117
|
final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
|
118
|
relationshipRepository.save(r1);
|
119
|
relationshipRepository.save(r2);
|
120
|
}
|
121
|
|
122
|
@Transactional
|
123
|
public void deleteRelation(final String from, final String to, final RelationType type) {
|
124
|
final Relationship r1 = new Relationship(from, to, type.toString());
|
125
|
final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
|
126
|
relationshipRepository.delete(r1);
|
127
|
relationshipRepository.delete(r2);
|
128
|
}
|
129
|
|
130
|
@Cacheable("countries_for_user")
|
131
|
public List<String> listCountriesForUser(final String name) {
|
132
|
return jdbcTemplate.queryForList("select country from user_countries where email = ?", String.class, name);
|
133
|
}
|
134
|
|
135
|
@Transactional
|
136
|
public void saveUser(@RequestBody final UserView userView) {
|
137
|
final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
|
138
|
user.setRole(userView.getRole());
|
139
|
user.setValid(userView.isValid());
|
140
|
userRepository.save(user);
|
141
|
userCountryRepository.deleteByEmail(userView.getEmail());
|
142
|
if (userView.getCountries() != null) {
|
143
|
userCountryRepository
|
144
|
.saveAll(Arrays.stream(userView.getCountries()).map(c -> new UserCountry(userView.getEmail(), c)).collect(Collectors.toList()));
|
145
|
}
|
146
|
}
|
147
|
|
148
|
@Transactional
|
149
|
public void deleteUser(final String email) {
|
150
|
userCountryRepository.deleteByEmail(email);
|
151
|
userRepository.deleteById(email);
|
152
|
}
|
153
|
|
154
|
@Transactional
|
155
|
public void newUser(final String email, final List<String> countries) {
|
156
|
final User user = new User();
|
157
|
user.setEmail(email);
|
158
|
user.setRole(UserRole.PENDING.name());
|
159
|
user.setValid(false);
|
160
|
userRepository.save(user);
|
161
|
if (countries != null) {
|
162
|
userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
|
163
|
}
|
164
|
}
|
165
|
|
166
|
}
|