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.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Qualifier;
|
21
|
import org.springframework.stereotype.Service;
|
22
|
import org.springframework.transaction.TransactionStatus;
|
23
|
import org.springframework.transaction.annotation.Transactional;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.name.Rank;
|
28
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
29
|
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationDTO;
|
30
|
|
31
|
/**
|
32
|
* @author a.kohlbecker
|
33
|
* @since Mar 10, 2017
|
34
|
*
|
35
|
*/
|
36
|
@Service("registrationServiceMock")
|
37
|
@Transactional
|
38
|
public class RegistrationService {
|
39
|
|
40
|
/**
|
41
|
*
|
42
|
*/
|
43
|
private static final int SIZE = 50; // FIXME test performance with 50 !!!!!
|
44
|
|
45
|
@Autowired
|
46
|
@Qualifier("cdmRepository")
|
47
|
private CdmRepository repo;
|
48
|
|
49
|
private Map<UUID, Registration> registrationsByUUID = new HashMap<>();
|
50
|
private Map<String, Registration> registrationsByRegID = new HashMap<>();
|
51
|
private Map<String, RegistrationDTO> registrationDTOsByRegID = new HashMap<>();
|
52
|
private Map<Integer, Set<RegistrationDTO>> registrationDTOsByCitationID = new HashMap<>();
|
53
|
|
54
|
private Collection<CdmBase> cdmEntities = new HashSet<>();
|
55
|
|
56
|
public RegistrationService() {
|
57
|
}
|
58
|
|
59
|
|
60
|
int minTypeDesignationCount = 1;
|
61
|
|
62
|
protected void init(){
|
63
|
if(registrationsByUUID.size() == 0){
|
64
|
TransactionStatus tx = repo.startTransaction(true);
|
65
|
int pageIndex = 0;
|
66
|
while(registrationsByUUID.size() < SIZE){
|
67
|
List<TaxonNameBase> names = repo.getNameService().list(TaxonNameBase.class, 100, pageIndex++, null, null);
|
68
|
for(TaxonNameBase name : names){
|
69
|
if(name != null && name.getRank() != null && name.getRank().isLower(Rank.SUBFAMILY())){
|
70
|
if(name.getTypeDesignations().size() > minTypeDesignationCount - 1) {
|
71
|
|
72
|
// name
|
73
|
Registration nameReg = new Registration();
|
74
|
nameReg.setName(name);
|
75
|
cdmEntities.add(name);
|
76
|
put(nameReg, new RegistrationDTO(nameReg, null));
|
77
|
|
78
|
// typedesignation
|
79
|
Registration typedesignationReg = new Registration();
|
80
|
typedesignationReg.addTypeDesignations(name.getTypeDesignations());
|
81
|
cdmEntities.addAll(name.getTypeDesignations());
|
82
|
put(typedesignationReg, new RegistrationDTO(typedesignationReg, name));
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
repo.commitTransaction(tx);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* @param reg
|
93
|
*/
|
94
|
private void put(Registration reg, RegistrationDTO dto) {
|
95
|
registrationsByUUID.put(reg.getUuid(), reg);
|
96
|
registrationsByRegID.put(reg.getSpecificIdentifier(), reg);
|
97
|
registrationDTOsByRegID.put(reg.getSpecificIdentifier(), dto);
|
98
|
if(! registrationDTOsByCitationID.containsKey(dto.getCitationID())){
|
99
|
registrationDTOsByCitationID.put(dto.getCitationID(), new HashSet<RegistrationDTO>());
|
100
|
}
|
101
|
registrationDTOsByCitationID.get(dto.getCitationID()).add(dto);
|
102
|
}
|
103
|
|
104
|
private void mergeBack(){
|
105
|
cdmEntities.forEach(e -> repo.getNameService().getSession().merge(e));
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* {@inheritDoc}
|
110
|
*/
|
111
|
public Registration load(UUID uuid) {
|
112
|
init();
|
113
|
return registrationsByUUID.get(uuid);
|
114
|
}
|
115
|
|
116
|
public Collection<Registration> list(){
|
117
|
init();
|
118
|
return registrationsByUUID.values();
|
119
|
}
|
120
|
|
121
|
public Collection<RegistrationDTO> listDTOs() {
|
122
|
init();
|
123
|
return registrationDTOsByRegID.values();
|
124
|
}
|
125
|
|
126
|
public Map<Integer, Set<RegistrationDTO>> listDTOsByWorkingSet() {
|
127
|
init();
|
128
|
return registrationDTOsByCitationID;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* @param registrationID
|
133
|
* @return
|
134
|
*/
|
135
|
public Registration loadByRegistrationID(Integer registrationID) {
|
136
|
init();
|
137
|
return registrationsByRegID.get(registrationID.toString());
|
138
|
}
|
139
|
|
140
|
|
141
|
}
|