1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.mock;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.beans.factory.annotation.Qualifier;
|
20
|
import org.springframework.stereotype.Service;
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
import org.springframework.transaction.annotation.Transactional;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.name.Rank;
|
27
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
28
|
import eu.etaxonomy.cdm.vaadin.presenter.phycobank.RegistrationDTO;
|
29
|
|
30
|
/**
|
31
|
* @author a.kohlbecker
|
32
|
* @since Mar 10, 2017
|
33
|
*
|
34
|
*/
|
35
|
@Service("registrationServiceMock")
|
36
|
@Transactional
|
37
|
public class RegistrationService {
|
38
|
|
39
|
@Autowired
|
40
|
@Qualifier("cdmRepository")
|
41
|
private CdmRepository repo;
|
42
|
|
43
|
private Map<UUID, Registration> registrationsByUUID = new HashMap<>();
|
44
|
private Map<String, Registration> registrationsByRegID = new HashMap<>();
|
45
|
private Map<String, RegistrationDTO> registrationDTOsByRegID = new HashMap<>();
|
46
|
|
47
|
private Collection<CdmBase> cdmEntities = new HashSet<>();
|
48
|
|
49
|
public RegistrationService() {
|
50
|
}
|
51
|
|
52
|
|
53
|
int minTypeDesignationCount = 1;
|
54
|
|
55
|
protected void init(){
|
56
|
if(registrationsByUUID.size() == 0){
|
57
|
TransactionStatus tx = repo.startTransaction(true);
|
58
|
while(registrationsByUUID.size() < 20){
|
59
|
List<TaxonNameBase> names = repo.getNameService().list(TaxonNameBase.class, 100, 0, null, null);
|
60
|
for(TaxonNameBase name : names){
|
61
|
if(name != null && name.getRank() != null && name.getRank().isLower(Rank.SUBFAMILY())){
|
62
|
if(name.getTypeDesignations().size() > minTypeDesignationCount - 1) {
|
63
|
|
64
|
// name
|
65
|
Registration nameReg = new Registration();
|
66
|
nameReg.setName(name);
|
67
|
cdmEntities.add(name);
|
68
|
put(nameReg, new RegistrationDTO(nameReg, null));
|
69
|
|
70
|
// typedesignation
|
71
|
Registration typedesignationReg = new Registration();
|
72
|
typedesignationReg.addTypeDesignations(name.getTypeDesignations());
|
73
|
cdmEntities.addAll(name.getTypeDesignations());
|
74
|
put(typedesignationReg, new RegistrationDTO(typedesignationReg, name));
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
repo.commitTransaction(tx);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* @param reg
|
85
|
*/
|
86
|
private void put(Registration reg, RegistrationDTO dto) {
|
87
|
registrationsByUUID.put(reg.getUuid(), reg);
|
88
|
registrationsByRegID.put(reg.getSpecificIdentifier(), reg);
|
89
|
registrationDTOsByRegID.put(reg.getSpecificIdentifier(), dto);
|
90
|
}
|
91
|
|
92
|
private void mergeBack(){
|
93
|
cdmEntities.forEach(e -> repo.getNameService().getSession().merge(e));
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* {@inheritDoc}
|
98
|
*/
|
99
|
public Registration load(UUID uuid) {
|
100
|
init();
|
101
|
return registrationsByUUID.get(uuid);
|
102
|
}
|
103
|
|
104
|
public Collection<Registration> list(){
|
105
|
init();
|
106
|
return registrationsByUUID.values();
|
107
|
}
|
108
|
|
109
|
public Collection<RegistrationDTO> listDTOs() {
|
110
|
init();
|
111
|
return registrationDTOsByRegID.values();
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* @param registrationID
|
116
|
* @return
|
117
|
*/
|
118
|
public Registration loadByRegistrationID(Integer registrationID) {
|
119
|
init();
|
120
|
return registrationsByRegID.get(registrationID.toString());
|
121
|
}
|
122
|
|
123
|
|
124
|
}
|