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.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
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.model.registration.RegistrationWorkingSet;
|
30
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
|
31
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
|
32
|
|
33
|
/**
|
34
|
* @author a.kohlbecker
|
35
|
* @since Mar 10, 2017
|
36
|
*
|
37
|
*/
|
38
|
@Service("registrationServiceMock")
|
39
|
@Transactional(readOnly=true)
|
40
|
public class RegistrationService {
|
41
|
|
42
|
private static final int SIZE = 50; // FIXME test performance with 50 !!!!!
|
43
|
|
44
|
@Autowired
|
45
|
@Qualifier("cdmRepository")
|
46
|
private CdmRepository repo;
|
47
|
|
48
|
private Map<UUID, Registration> registrationsByUUID = new HashMap<>();
|
49
|
private Map<Integer, Registration> registrationsByRegID = new HashMap<>();
|
50
|
private Map<Integer, RegistrationDTO> registrationDTOsById = new HashMap<>();
|
51
|
private Map<String, RegistrationDTO> registrationDTOsByIdentifier = new HashMap<>();
|
52
|
private Map<Integer, List<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
|
if(names.isEmpty()){
|
69
|
break;
|
70
|
}
|
71
|
for(TaxonNameBase name : names){
|
72
|
if(name != null && name.getRank() != null && name.getRank().isLower(Rank.SUBFAMILY())){
|
73
|
if(name.getTypeDesignations().size() > minTypeDesignationCount - 1) {
|
74
|
|
75
|
// name
|
76
|
Registration nameReg = new Registration();
|
77
|
nameReg.setName(name);
|
78
|
cdmEntities.add(name);
|
79
|
put(new RegistrationDTO(nameReg));
|
80
|
|
81
|
// typedesignation
|
82
|
Registration typedesignationReg = new Registration();
|
83
|
typedesignationReg.addTypeDesignations(name.getTypeDesignations());
|
84
|
cdmEntities.addAll(name.getTypeDesignations());
|
85
|
put(new RegistrationDTO(typedesignationReg));
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
repo.commitTransaction(tx);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* @param reg
|
96
|
*/
|
97
|
private void put(RegistrationDTO dto) {
|
98
|
Registration reg = dto.registration();
|
99
|
registrationsByUUID.put(dto.getUuid(), reg);
|
100
|
registrationsByRegID.put(reg.getId(), reg);
|
101
|
|
102
|
registrationDTOsById.put(reg.getId(), dto);
|
103
|
registrationDTOsByIdentifier.put(reg.getIdentifier(), dto);
|
104
|
|
105
|
if(! registrationDTOsByCitationId.containsKey(dto.getCitationID())){
|
106
|
registrationDTOsByCitationId.put(dto.getCitationID(), new ArrayList<RegistrationDTO>());
|
107
|
}
|
108
|
registrationDTOsByCitationId.get(dto.getCitationID()).add(dto);
|
109
|
}
|
110
|
|
111
|
private void mergeBack(){
|
112
|
cdmEntities.forEach(e -> repo.getNameService().getSession().merge(e));
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* {@inheritDoc}
|
117
|
*/
|
118
|
public Registration load(UUID uuid) {
|
119
|
init();
|
120
|
return registrationsByUUID.get(uuid);
|
121
|
}
|
122
|
|
123
|
|
124
|
public Collection<Registration> list(){
|
125
|
init();
|
126
|
return registrationsByUUID.values();
|
127
|
}
|
128
|
|
129
|
public Collection<RegistrationDTO> listDTOs() {
|
130
|
init();
|
131
|
return registrationDTOsById.values();
|
132
|
}
|
133
|
|
134
|
public Map<Integer, List<RegistrationDTO>> listDTOsByWorkingSet() {
|
135
|
init();
|
136
|
return registrationDTOsByCitationId;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* @param id the CDM Entity id
|
141
|
* @return
|
142
|
*/
|
143
|
public Registration loadByRegistrationID(Integer id) {
|
144
|
init();
|
145
|
return registrationsByRegID.get(id);
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* @param identifier the Registration Identifier String
|
150
|
* @return
|
151
|
*/
|
152
|
public RegistrationDTO loadDtoByIdentifier(String identifier) {
|
153
|
init();
|
154
|
return registrationDTOsById.get(identifier);
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* @param id the CDM Entity id
|
159
|
* @return
|
160
|
*/
|
161
|
public RegistrationDTO loadDtoById(Integer id) {
|
162
|
init();
|
163
|
return registrationDTOsById.get(id);
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* @param id the CDM Entity id
|
168
|
* @return
|
169
|
* @throws RegistrationValidationException
|
170
|
*/
|
171
|
public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException {
|
172
|
init();
|
173
|
RegistrationDTO dto = registrationDTOsById.get(id);
|
174
|
|
175
|
return new RegistrationWorkingSet(registrationDTOsByCitationId.get(dto.getCitationID()));
|
176
|
}
|
177
|
|
178
|
|
179
|
}
|