Project

General

Profile

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.model.Acronym;
17
import eu.dnetlib.organizations.model.Organization;
18
import eu.dnetlib.organizations.model.OtherIdentifier;
19
import eu.dnetlib.organizations.model.OtherName;
20
import eu.dnetlib.organizations.model.Relationship;
21
import eu.dnetlib.organizations.model.Url;
22
import eu.dnetlib.organizations.model.User;
23
import eu.dnetlib.organizations.model.UserCountry;
24
import eu.dnetlib.organizations.model.view.OrganizationView;
25
import eu.dnetlib.organizations.model.view.UserView;
26
import eu.dnetlib.organizations.repository.AcronymRepository;
27
import eu.dnetlib.organizations.repository.OrganizationRepository;
28
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
29
import eu.dnetlib.organizations.repository.OtherNameRepository;
30
import eu.dnetlib.organizations.repository.RelationshipRepository;
31
import eu.dnetlib.organizations.repository.UrlRepository;
32
import eu.dnetlib.organizations.repository.UserCountryRepository;
33
import eu.dnetlib.organizations.repository.UserRepository;
34

    
35
@Component
36
public class DatabaseUtils {
37

    
38
	@Autowired
39
	private AcronymRepository acronymRepository;
40
	@Autowired
41
	private OrganizationRepository organizationRepository;
42
	@Autowired
43
	private OtherIdentifierRepository otherIdentifierRepository;
44
	@Autowired
45
	private OtherNameRepository otherNameRepository;
46
	@Autowired
47
	private UrlRepository urlRepository;
48
	@Autowired
49
	private RelationshipRepository relationshipRepository;
50
	@Autowired
51
	private UserRepository userRepository;
52
	@Autowired
53
	private UserCountryRepository userCountryRepository;
54

    
55
	@Autowired
56
	private JdbcTemplate jdbcTemplate;
57

    
58
	public enum VocabularyTable {
59
		languages, countries, org_types, id_types, rel_types, simrel_types
60
	}
61

    
62
	@Transactional
63
	public String insertOrUpdateOrganization(final OrganizationView orgView, final String user, final boolean update) {
64

    
65
		if (update) {
66
			cleanOldRelations(orgView.getId());
67
		}
68

    
69
		final Organization org = new Organization(update ? orgView.getId() : null,
70
				orgView.getName(),
71
				orgView.getType(),
72
				orgView.getLat(), orgView.getLng(),
73
				orgView.getCity(), orgView.getCountry());
74

    
75
		final String orgId = organizationRepository.save(org).getId();
76

    
77
		makeNewRelations(orgView, orgId);
78

    
79
		updateHistoryFields(orgId, user, update);
80

    
81
		return orgId;
82
	}
83

    
84
	private void updateHistoryFields(final String id, final String user, final boolean update) {
85
		final OffsetDateTime now = OffsetDateTime.now();
86
		if (update) {
87
			organizationRepository.updateModificationDate(id, user, now);
88
		} else {
89
			organizationRepository.updateCreationDate(id, user, now);
90
			organizationRepository.updateModificationDate(id, user, now);
91
		}
92
	}
93

    
94
	private void makeNewRelations(final OrganizationView orgView, final String orgId) {
95
		orgView.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
96
		orgView.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
97
		orgView.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
98
		orgView.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
99
	}
100

    
101
	private void cleanOldRelations(final String id) {
102
		acronymRepository.deleteByOrgId(id);
103
		otherNameRepository.deleteByOrgId(id);
104
		otherIdentifierRepository.deleteByOrgId(id);
105
		urlRepository.deleteByOrgId(id);
106
	}
107

    
108
	@Cacheable("vocs")
109
	public List<String> listValuesOfVocabularyTable(final VocabularyTable table) {
110
		return jdbcTemplate.queryForList("select val from " + table, String.class);
111
	}
112

    
113
	@Transactional
114
	public void addRelation(final String from, final String to, final RelationType type) {
115
		final Relationship r1 = new Relationship(from, to, type.toString());
116
		final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
117
		relationshipRepository.save(r1);
118
		relationshipRepository.save(r2);
119
	}
120

    
121
	@Transactional
122
	public void deleteRelation(final String from, final String to, final RelationType type) {
123
		final Relationship r1 = new Relationship(from, to, type.toString());
124
		final Relationship r2 = new Relationship(to, from, type.getInverse().toString());
125
		relationshipRepository.delete(r1);
126
		relationshipRepository.delete(r2);
127
	}
128

    
129
	@Cacheable("countries_for_user")
130
	public List<String> listCountriesForUser(final String name) {
131
		return jdbcTemplate.queryForList("select country from user_countries where email = ?", String.class, name);
132
	}
133

    
134
	@Transactional
135
	public void saveUser(@RequestBody final UserView userView) {
136
		final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
137
		user.setRole(userView.getRole());
138
		user.setValid(userView.isValid());
139
		userRepository.save(user);
140
		userCountryRepository.deleteByEmail(userView.getEmail());
141
		if (userView.getCountries() != null) {
142
			userCountryRepository
143
					.saveAll(Arrays.stream(userView.getCountries()).map(c -> new UserCountry(userView.getEmail(), c)).collect(Collectors.toList()));
144
		}
145
	}
146

    
147
	@Transactional
148
	public void deleteUser(final String email) {
149
		userCountryRepository.deleteByEmail(email);
150
		userRepository.deleteById(email);
151
	}
152

    
153
	@Transactional
154
	public void newUser(final String email, final List<String> countries) {
155
		final User user = new User();
156
		user.setEmail(email);
157
		user.setRole(OpenOrgsConstants.pendingRole);
158
		user.setValid(false);
159
		userRepository.save(user);
160
		if (countries != null) {
161
			userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
162
		}
163
	}
164

    
165
}
(1-1/4)