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