1
|
package eu.dnetlib.organizations.utils;
|
2
|
|
3
|
import java.time.OffsetDateTime;
|
4
|
import java.util.UUID;
|
5
|
|
6
|
import javax.transaction.Transactional;
|
7
|
|
8
|
import org.apache.commons.codec.digest.DigestUtils;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.stereotype.Component;
|
11
|
|
12
|
import eu.dnetlib.organizations.model.Acronym;
|
13
|
import eu.dnetlib.organizations.model.Organization;
|
14
|
import eu.dnetlib.organizations.model.OtherIdentifier;
|
15
|
import eu.dnetlib.organizations.model.OtherName;
|
16
|
import eu.dnetlib.organizations.model.Type;
|
17
|
import eu.dnetlib.organizations.model.Url;
|
18
|
import eu.dnetlib.organizations.model.view.OrganizationView;
|
19
|
import eu.dnetlib.organizations.repository.AcronymRepository;
|
20
|
import eu.dnetlib.organizations.repository.OrganizationRepository;
|
21
|
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
|
22
|
import eu.dnetlib.organizations.repository.OtherNameRepository;
|
23
|
import eu.dnetlib.organizations.repository.TypeRepository;
|
24
|
import eu.dnetlib.organizations.repository.UrlRepository;
|
25
|
|
26
|
@Component
|
27
|
public class DatabaseUtils {
|
28
|
|
29
|
@Autowired
|
30
|
private AcronymRepository acronymRepository;
|
31
|
@Autowired
|
32
|
private OrganizationRepository organizationRepository;
|
33
|
@Autowired
|
34
|
private OtherIdentifierRepository otherIdentifierRepository;
|
35
|
@Autowired
|
36
|
private OtherNameRepository otherNameRepository;
|
37
|
@Autowired
|
38
|
private TypeRepository typeRepository;
|
39
|
@Autowired
|
40
|
private UrlRepository urlRepository;
|
41
|
|
42
|
@Transactional
|
43
|
public String insertOrUpdateOrganization(final OrganizationView org, final String user, final boolean update) {
|
44
|
|
45
|
final String orgId = update ? org.getId() : "openorgs____::" + DigestUtils.md5Hex(UUID.randomUUID().toString());
|
46
|
final OffsetDateTime now = OffsetDateTime.now();
|
47
|
|
48
|
if (update) {
|
49
|
acronymRepository.deleteByOrgId(orgId);
|
50
|
otherNameRepository.deleteByOrgId(orgId);
|
51
|
otherIdentifierRepository.deleteByOrgId(orgId);
|
52
|
typeRepository.deleteByOrgId(orgId);
|
53
|
urlRepository.deleteByOrgId(orgId);
|
54
|
}
|
55
|
|
56
|
organizationRepository.save(new Organization(orgId, org.getName(), org.getLat(), org.getLng(), org.getCity(), org.getCountry()));
|
57
|
org.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
|
58
|
org.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
|
59
|
org.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
|
60
|
org.getTypes().forEach(t -> typeRepository.save(new Type(orgId, t)));
|
61
|
org.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
|
62
|
|
63
|
if (update) {
|
64
|
organizationRepository.updateModificationDate(orgId, user, now);
|
65
|
} else {
|
66
|
organizationRepository.updateCreationDate(orgId, user, now);
|
67
|
organizationRepository.updateModificationDate(orgId, user, now);
|
68
|
}
|
69
|
|
70
|
return orgId;
|
71
|
}
|
72
|
|
73
|
}
|