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.service;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.hibernate.Session;
|
20
|
import org.jboss.logging.Logger;
|
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.annotation.Transactional;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
27
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
28
|
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
|
29
|
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
|
30
|
import eu.etaxonomy.cdm.api.service.name.TypeDesignationComparator;
|
31
|
import eu.etaxonomy.cdm.api.service.name.TypeDesignationWorkingSet;
|
32
|
import eu.etaxonomy.cdm.api.service.registration.IRegistrationWorkingSetService;
|
33
|
import eu.etaxonomy.cdm.api.service.registration.RegistrationWorkingSetService;
|
34
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
35
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
36
|
import eu.etaxonomy.cdm.model.name.Registration;
|
37
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
39
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
40
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
|
41
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
|
45
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
46
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
47
|
import eu.etaxonomy.cdm.ref.TypedEntityReference;
|
48
|
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO;
|
49
|
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
|
50
|
|
51
|
/**
|
52
|
* @author a.kohlbecker
|
53
|
* @since Nov 13, 2017
|
54
|
*
|
55
|
*/
|
56
|
@Service("specimenTypeDesignationWorkingSetService")
|
57
|
@Transactional(readOnly=true)
|
58
|
public class SpecimenTypeDesignationWorkingSetServiceImpl implements ISpecimenTypeDesignationWorkingSetService {
|
59
|
|
60
|
private final Logger logger = Logger.getLogger(SpecimenTypeDesignationWorkingSetServiceImpl.class);
|
61
|
|
62
|
static SpecimenDeleteConfigurator specimenDeleteConfigurer = new SpecimenDeleteConfigurator();
|
63
|
static {
|
64
|
specimenDeleteConfigurer.setDeleteChildren(true);
|
65
|
specimenDeleteConfigurer.setDeleteFromDescription(true);
|
66
|
specimenDeleteConfigurer.setDeleteFromIndividualsAssociation(true);
|
67
|
specimenDeleteConfigurer.setDeleteFromTypeDesignation(true);
|
68
|
specimenDeleteConfigurer.setDeleteMolecularData(true);
|
69
|
}
|
70
|
|
71
|
public static final List<String> TAXON_NAME_INIT_STRATEGY = Arrays.asList(new String []{
|
72
|
"$",
|
73
|
"nomenclaturalSource.citation.authorship.$",
|
74
|
"nomenclaturalSource.citation.inReference.authorship.$",
|
75
|
"nomenclaturalSource.citation.inReference.inReference.authorship.$",
|
76
|
"status.type",
|
77
|
"typeDesignations"
|
78
|
}
|
79
|
);
|
80
|
|
81
|
@Autowired
|
82
|
IRegistrationWorkingSetService registrationWorkingSetService;
|
83
|
|
84
|
@Qualifier("cdmRepository")
|
85
|
@Autowired
|
86
|
CdmRepository repo;
|
87
|
|
88
|
@Override
|
89
|
public SpecimenTypeDesignationWorkingSetDTO<Registration> create(UUID registrationUuid, UUID publicationUuid, UUID typifiedNameUuid) {
|
90
|
FieldUnit newfieldUnit = FieldUnit.NewInstance();
|
91
|
Registration reg = repo.getRegistrationService().load(registrationUuid, RegistrationWorkingSetService.REGISTRATION_DTO_INIT_STRATEGY.getPropertyPaths());
|
92
|
if(reg == null){
|
93
|
reg = repo.getRegistrationService().newRegistration();
|
94
|
reg.setUuid(registrationUuid);
|
95
|
}
|
96
|
TaxonName typifiedName = repo.getNameService().load(typifiedNameUuid, TAXON_NAME_INIT_STRATEGY);
|
97
|
Reference citation = repo.getReferenceService().load(publicationUuid, Arrays.asList("$"));
|
98
|
SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto = new SpecimenTypeDesignationWorkingSetDTO<>(reg, newfieldUnit, citation, typifiedName);
|
99
|
return workingSetDto;
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
@Transactional(readOnly=true)
|
104
|
public SpecimenTypeDesignationWorkingSetDTO<Registration> load(UUID registrationUuid, TypedEntityReference<? extends IdentifiableEntity<?>> baseEntityRef) {
|
105
|
|
106
|
RegistrationDTO regDTO = registrationWorkingSetService.loadDtoByUuid(registrationUuid);
|
107
|
// find the working set
|
108
|
TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(baseEntityRef);
|
109
|
SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto = specimenTypeDesignationWorkingSetDTO(regDTO, typeDesignationWorkingSet.getBaseEntityReference());
|
110
|
return workingSetDto;
|
111
|
}
|
112
|
|
113
|
protected SpecimenTypeDesignationWorkingSetDTO<Registration> specimenTypeDesignationWorkingSetDTO(
|
114
|
RegistrationDTO regDTO, TypedEntityReference baseEntityReference) {
|
115
|
|
116
|
Set<TypeDesignationBase> typeDesignations = regDTO.getTypeDesignationsInWorkingSet(baseEntityReference);
|
117
|
List<SpecimenTypeDesignation> specimenTypeDesignations = new ArrayList<>(typeDesignations.size());
|
118
|
typeDesignations.forEach(td -> specimenTypeDesignations.add((SpecimenTypeDesignation)td));
|
119
|
specimenTypeDesignations.sort(new TypeDesignationComparator());
|
120
|
VersionableEntity baseEntity = regDTO.getTypeDesignationWorkingSet(baseEntityReference).getBaseEntity();
|
121
|
|
122
|
SpecimenTypeDesignationWorkingSetDTO<Registration> dto = new SpecimenTypeDesignationWorkingSetDTO<Registration>(regDTO.registration(),
|
123
|
baseEntity, specimenTypeDesignations, regDTO.getCitation(), regDTO.getTypifiedName());
|
124
|
return dto;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public SpecimenTypeDesignationWorkingSetDTO<Registration> fixMissingFieldUnit(SpecimenTypeDesignationWorkingSetDTO<Registration> bean) {
|
129
|
|
130
|
if(bean.getFieldUnit() == null){
|
131
|
// in case the base unit of the working set is not a FieldUnit all contained TypeDesignations must be modified
|
132
|
// so that they are based on an empty FieldUnit with an associated Gathering Event
|
133
|
|
134
|
Registration reg = repo.getRegistrationService().find(bean.getOwner().getUuid());
|
135
|
RegistrationDTO regDTO = new RegistrationDTO(reg);
|
136
|
|
137
|
FieldUnit fieldUnit = FieldUnit.NewInstance();
|
138
|
GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
|
139
|
fieldUnit.setGatheringEvent(gatheringEvent);
|
140
|
fieldUnit = (FieldUnit) repo.getOccurrenceService().save(fieldUnit);
|
141
|
|
142
|
VersionableEntity baseEntity = bean.getBaseEntity();
|
143
|
Set<TypeDesignationBase> typeDesignations = regDTO.getTypeDesignationsInWorkingSet(
|
144
|
new TypedEntityReference(baseEntity.getClass(), baseEntity.getUuid(), baseEntity.toString())
|
145
|
);
|
146
|
for(TypeDesignationBase td : typeDesignations){
|
147
|
DerivationEvent de = DerivationEvent.NewInstance();//
|
148
|
de.addOriginal(fieldUnit);
|
149
|
de.addDerivative(((SpecimenTypeDesignation)td).getTypeSpecimen());
|
150
|
de.setType(DerivationEventType.GATHERING_IN_SITU());
|
151
|
}
|
152
|
|
153
|
repo.getRegistrationService().saveOrUpdate(reg);
|
154
|
}
|
155
|
return bean;
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
@Transactional(readOnly=false)
|
160
|
public void save(SpecimenTypeDesignationWorkingSetDTO<? extends VersionableEntity> dto) {
|
161
|
|
162
|
if(dto.getOwner() instanceof Registration){
|
163
|
Registration regPremerge = (Registration) dto.getOwner();
|
164
|
|
165
|
regPremerge = repo.getRegistrationService().assureIsPersisted(regPremerge);
|
166
|
|
167
|
// find the newly created type designations
|
168
|
Set<SpecimenTypeDesignation> newTypeDesignations = findNewTypeDesignations((SpecimenTypeDesignationWorkingSetDTO<Registration>) dto);
|
169
|
|
170
|
FieldUnit fieldUnit = (FieldUnit) dto.getBaseEntity();
|
171
|
|
172
|
// associate the new typeDesignations with the registration
|
173
|
for(SpecimenTypeDesignation std : newTypeDesignations){
|
174
|
assureFieldUnit(fieldUnit, std);
|
175
|
std.setCitation(dto.getCitation());
|
176
|
dto.getTypifiedName().addTypeDesignation(std, false);
|
177
|
regPremerge.addTypeDesignation(std);
|
178
|
}
|
179
|
|
180
|
for(SpecimenTypeDesignationDTO stdDTO : dto.getSpecimenTypeDesignationDTOs()){
|
181
|
SpecimenTypeDesignation specimenTypeDesignation = stdDTO.asSpecimenTypeDesignation();
|
182
|
// associate all type designations with the fieldUnit
|
183
|
assureFieldUnit(fieldUnit, specimenTypeDesignation);
|
184
|
}
|
185
|
|
186
|
Session session = repo.getSession();
|
187
|
|
188
|
session.merge(dto.getOwner());
|
189
|
session.flush();
|
190
|
|
191
|
// ------------------------ perform delete of removed SpecimenTypeDesignations
|
192
|
// this step also includes the deletion of DerivedUnits which have been converted by
|
193
|
// the DerivedUnitConverter in turn of a kindOfUnit change
|
194
|
for(SpecimenTypeDesignation std : dto.deletedSpecimenTypeDesignations()){
|
195
|
deleteSpecimenTypeDesignation(dto, std);
|
196
|
}
|
197
|
}
|
198
|
|
199
|
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* @param dto
|
204
|
* @param specimenDeleteConfigurer
|
205
|
* @param std
|
206
|
*/
|
207
|
protected void deleteSpecimenTypeDesignation(SpecimenTypeDesignationWorkingSetDTO<? extends VersionableEntity> dto, SpecimenTypeDesignation std) {
|
208
|
|
209
|
// if(dto.getOwner() instanceof Registration){
|
210
|
// Registration registration = (Registration) dto.getOwner();
|
211
|
// registration.getTypeDesignations().clear();
|
212
|
// repo.getRegistrationService().save(registration);
|
213
|
// } else {
|
214
|
// throw new RuntimeException("Unimplemented owner type");
|
215
|
// }
|
216
|
DerivedUnit du = std.getTypeSpecimen();
|
217
|
// DerivationEvent derivationEvent = du.getDerivedFrom();
|
218
|
|
219
|
//du.removeSpecimenTypeDesignation(std);
|
220
|
//derivationEvent.removeDerivative(du);
|
221
|
std.setTypeSpecimen(null);
|
222
|
repo.getOccurrenceService().delete(du, specimenDeleteConfigurer);
|
223
|
repo.getNameService().deleteTypeDesignation(dto.getTypifiedName(), std);
|
224
|
// if(derivationEvent.getDerivatives().size() == 0){
|
225
|
// getRepo().getEventBaseService().delete(derivationEvent);
|
226
|
// }
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* @param session
|
231
|
* @param fieldUnit
|
232
|
* @param specimenTypeDesignation
|
233
|
*/
|
234
|
protected void assureFieldUnit(FieldUnit fieldUnit,
|
235
|
SpecimenTypeDesignation specimenTypeDesignation) {
|
236
|
try {
|
237
|
SpecimenOrObservationBase<?> original = findEarliestOriginal(specimenTypeDesignation.getTypeSpecimen());
|
238
|
if(original instanceof DerivedUnit){
|
239
|
DerivedUnit du = (DerivedUnit)original;
|
240
|
du.getDerivedFrom().addOriginal(fieldUnit);
|
241
|
}
|
242
|
} catch (Exception e) {
|
243
|
// has more than one originals !!!
|
244
|
logger.error(e);
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* @param std
|
250
|
* @return
|
251
|
* @throws Exception
|
252
|
*/
|
253
|
private SpecimenOrObservationBase<?> findEarliestOriginal(DerivedUnit du) throws Exception {
|
254
|
|
255
|
SpecimenOrObservationBase original = du;
|
256
|
|
257
|
while(du != null && du.getDerivedFrom() != null && !du.getDerivedFrom().getOriginals().isEmpty()) {
|
258
|
Iterator<SpecimenOrObservationBase> it = du.getDerivedFrom().getOriginals().iterator();
|
259
|
SpecimenOrObservationBase nextOriginal = it.next();
|
260
|
if(nextOriginal == null){
|
261
|
break;
|
262
|
}
|
263
|
original = nextOriginal;
|
264
|
if(original instanceof DerivedUnit){
|
265
|
du = (DerivedUnit)original;
|
266
|
} else {
|
267
|
// so this must be a FieldUnit,
|
268
|
break;
|
269
|
}
|
270
|
if(it.hasNext()){
|
271
|
throw new Exception(String.format("%s has more than one originals", du.toString()));
|
272
|
}
|
273
|
}
|
274
|
return original;
|
275
|
}
|
276
|
|
277
|
private Set<SpecimenTypeDesignation> findNewTypeDesignations(SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto) {
|
278
|
|
279
|
Registration reg = workingSetDto.getOwner();
|
280
|
Set<SpecimenTypeDesignation> addCandidates = new HashSet<>();
|
281
|
for(SpecimenTypeDesignationDTO stdDTO : workingSetDto.getSpecimenTypeDesignationDTOs()){
|
282
|
SpecimenTypeDesignation std = stdDTO.asSpecimenTypeDesignation();
|
283
|
if(reg.getTypeDesignations().isEmpty() || !reg.getTypeDesignations().stream().filter(td -> td.equals(std)).findFirst().isPresent()){
|
284
|
addCandidates.add(std);
|
285
|
}
|
286
|
}
|
287
|
return addCandidates;
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* {@inheritDoc}
|
292
|
*/
|
293
|
@Override
|
294
|
@Transactional(readOnly=false)
|
295
|
public void delete(SpecimenTypeDesignationWorkingSetDTO bean, boolean deleteFieldUnit) {
|
296
|
|
297
|
@SuppressWarnings("unchecked")
|
298
|
List<SpecimenTypeDesignationDTO> specimenTypeDesignationDTOs = bean.getSpecimenTypeDesignationDTOs();
|
299
|
for(SpecimenTypeDesignationDTO stdDTO : specimenTypeDesignationDTOs){
|
300
|
SpecimenTypeDesignation std = stdDTO.asSpecimenTypeDesignation();
|
301
|
deleteSpecimenTypeDesignation(bean, std);
|
302
|
if(bean.getOwner() instanceof Registration){
|
303
|
((Registration)bean.getOwner()).getTypeDesignations().remove(std);
|
304
|
}
|
305
|
}
|
306
|
|
307
|
if(deleteFieldUnit){
|
308
|
FieldUnit fu = bean.getFieldUnit();
|
309
|
// delete the fieldunit and all derivatives
|
310
|
DeleteResult result = repo.getOccurrenceService().delete(fu.getUuid(), specimenDeleteConfigurer);
|
311
|
String msg = result.toString();
|
312
|
}
|
313
|
}
|
314
|
|
315
|
}
|