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.apache.log4j.Logger;
|
20
|
import org.joda.time.DateTime;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Qualifier;
|
23
|
import org.springframework.stereotype.Service;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
import org.springframework.transaction.annotation.Transactional;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.User;
|
30
|
import eu.etaxonomy.cdm.model.name.Rank;
|
31
|
import eu.etaxonomy.cdm.model.name.Registration;
|
32
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
|
35
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
|
36
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
|
37
|
|
38
|
/**
|
39
|
* Mock service which creates Registration on the fly.
|
40
|
* Registrations are never persisted they are only kept in memory.
|
41
|
*
|
42
|
* @author a.kohlbecker
|
43
|
* @since Mar 10, 2017
|
44
|
*
|
45
|
*/
|
46
|
@Service("registrationServiceMock")
|
47
|
@Transactional(readOnly=true)
|
48
|
public class RegistrationService implements IRegistrationWorkingSetService {
|
49
|
|
50
|
private static final int SIZE = 50; // FIXME test performance with 50 !!!!!
|
51
|
|
52
|
private static final Logger logger = Logger.getLogger(RegistrationService.class);
|
53
|
|
54
|
@Autowired
|
55
|
@Qualifier("cdmRepository")
|
56
|
private CdmRepository repo;
|
57
|
|
58
|
private Map<UUID, Registration> registrationsByUUID = new HashMap<>();
|
59
|
private Map<Integer, Registration> registrationsByRegID = new HashMap<>();
|
60
|
private Map<Integer, RegistrationDTO> registrationDTOsById = new HashMap<>();
|
61
|
private Map<String, RegistrationDTO> registrationDTOsByIdentifier = new HashMap<>();
|
62
|
private Map<Integer, List<RegistrationDTO>> registrationDTOsByCitationId = new HashMap<>();
|
63
|
|
64
|
private Collection<CdmBase> cdmEntities = new HashSet<>();
|
65
|
|
66
|
int autoincrementId = 100000;
|
67
|
|
68
|
public RegistrationService() {
|
69
|
}
|
70
|
|
71
|
|
72
|
int minTypeDesignationCount = 1;
|
73
|
|
74
|
protected void init(){
|
75
|
if(isCleanSweep()){
|
76
|
autoincrementId = 100000;
|
77
|
registrationsByUUID = new HashMap<>();
|
78
|
registrationsByRegID = new HashMap<>();
|
79
|
registrationDTOsById = new HashMap<>();
|
80
|
registrationDTOsByIdentifier = new HashMap<>();
|
81
|
registrationDTOsByCitationId = new HashMap<>();
|
82
|
}
|
83
|
if(registrationsByUUID.size() == 0){
|
84
|
TransactionStatus tx = repo.startTransaction(true);
|
85
|
int pageIndex = 0;
|
86
|
while(registrationsByUUID.size() < SIZE){
|
87
|
List<TaxonNameBase> names = repo.getNameService().list(TaxonNameBase.class, 100, pageIndex++, null, null);
|
88
|
if(names.isEmpty()){
|
89
|
break;
|
90
|
}
|
91
|
for(TaxonNameBase name : names){
|
92
|
if(name != null && name.getRank() != null && name.getRank().isLower(Rank.SUBFAMILY()) && name.getNomenclaturalReference() != null){
|
93
|
if(name.getTypeDesignations().size() > minTypeDesignationCount - 1) {
|
94
|
|
95
|
// name
|
96
|
logger.debug("creating Registration for " + name.getTitleCache());
|
97
|
Registration reg = newMockRegistration();
|
98
|
reg.setName(name);
|
99
|
cdmEntities.add(name);
|
100
|
|
101
|
// typedesignation
|
102
|
reg.setTypeDesignations(name.getTypeDesignations());
|
103
|
cdmEntities.addAll(name.getTypeDesignations());
|
104
|
|
105
|
put(new RegistrationDTO(reg));
|
106
|
logger.debug("\t\t\tdone");
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
repo.commitTransaction(tx);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* @return
|
117
|
*/
|
118
|
private Registration newMockRegistration() {
|
119
|
Registration reg = Registration.NewInstance();
|
120
|
reg.setId(autoincrementId);
|
121
|
reg.setSpecificIdentifier(String.valueOf(autoincrementId));
|
122
|
reg.setIdentifier("http://phycobank/" + reg.getSpecificIdentifier());
|
123
|
autoincrementId++;
|
124
|
reg.setStatus(RegistrationStatus.values()[(int) (Math.random() * RegistrationStatus.values().length)]);
|
125
|
reg.setRegistrationDate(DateTime.now());
|
126
|
return reg;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* @return
|
131
|
*/
|
132
|
private boolean isCleanSweep() {
|
133
|
|
134
|
return false;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* @param reg
|
139
|
*/
|
140
|
private void put(RegistrationDTO dto) {
|
141
|
logger.debug("putting DTO " + dto.getSummary());
|
142
|
Registration reg = dto.registration();
|
143
|
registrationsByUUID.put(dto.getUuid(), reg);
|
144
|
registrationsByRegID.put(reg.getId(), reg);
|
145
|
|
146
|
registrationDTOsById.put(reg.getId(), dto);
|
147
|
registrationDTOsByIdentifier.put(reg.getIdentifier(), dto);
|
148
|
|
149
|
if(! registrationDTOsByCitationId.containsKey(dto.getCitationID())){
|
150
|
registrationDTOsByCitationId.put(dto.getCitationID(), new ArrayList<RegistrationDTO>());
|
151
|
}
|
152
|
registrationDTOsByCitationId.get(dto.getCitationID()).add(dto);
|
153
|
}
|
154
|
|
155
|
private void mergeBack(){
|
156
|
cdmEntities.forEach(e -> repo.getNameService().getSession().merge(e));
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* {@inheritDoc}
|
161
|
*/
|
162
|
public Registration load(UUID uuid) {
|
163
|
init();
|
164
|
return registrationsByUUID.get(uuid);
|
165
|
}
|
166
|
|
167
|
|
168
|
public Collection<Registration> list(){
|
169
|
init();
|
170
|
return registrationsByUUID.values();
|
171
|
}
|
172
|
|
173
|
@Override
|
174
|
public Collection<RegistrationDTO> listDTOs() {
|
175
|
init();
|
176
|
return registrationDTOsById.values();
|
177
|
}
|
178
|
|
179
|
public Map<Integer, List<RegistrationDTO>> listDTOsByWorkingSet() {
|
180
|
init();
|
181
|
return registrationDTOsByCitationId;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* @param id the CDM Entity id
|
186
|
* @return
|
187
|
*/
|
188
|
public Registration loadByRegistrationID(Integer id) {
|
189
|
init();
|
190
|
return registrationsByRegID.get(id);
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* @param identifier the Registration Identifier String
|
195
|
* @return
|
196
|
*/
|
197
|
public RegistrationDTO loadDtoByIdentifier(String identifier) {
|
198
|
init();
|
199
|
return registrationDTOsById.get(identifier);
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* @param id the CDM Entity id
|
204
|
* @return
|
205
|
*/
|
206
|
@Override
|
207
|
public RegistrationDTO loadDtoById(Integer id) {
|
208
|
init();
|
209
|
return registrationDTOsById.get(id);
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* @param id the CDM Entity id
|
214
|
* @return
|
215
|
* @throws RegistrationValidationException
|
216
|
*/
|
217
|
@Override
|
218
|
public RegistrationWorkingSet loadWorkingSetByRegistrationID(Integer id) throws RegistrationValidationException {
|
219
|
init();
|
220
|
RegistrationDTO dto = registrationDTOsById.get(id);
|
221
|
|
222
|
return new RegistrationWorkingSet(registrationDTOsByCitationId.get(dto.getCitationID()));
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* {@inheritDoc}
|
227
|
*/
|
228
|
@Override
|
229
|
public Collection<RegistrationDTO> listDTOsFor(User submitter, Collection<RegistrationStatus> includedStatus) {
|
230
|
// TODO Auto-generated method stub
|
231
|
return null;
|
232
|
}
|
233
|
|
234
|
|
235
|
}
|