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.api.service.dto;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.LinkedHashMap;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.commons.lang3.StringUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.joda.time.DateTime;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.exception.RegistrationValidationException;
|
25
|
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager;
|
26
|
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSetManager.TypeDesignationWorkingSet;
|
27
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
28
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
|
29
|
import eu.etaxonomy.cdm.model.name.Registration;
|
30
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
32
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
33
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
34
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
35
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
36
|
import eu.etaxonomy.cdm.ref.EntityReference;
|
37
|
import eu.etaxonomy.cdm.ref.TypedEntityReference;
|
38
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
39
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
40
|
|
41
|
public class RegistrationDTO{
|
42
|
|
43
|
private static final Logger logger = Logger.getLogger(RegistrationDTO.class);
|
44
|
|
45
|
private String summary = "";
|
46
|
|
47
|
private RegistrationType registrationType;
|
48
|
|
49
|
private Reference citation = null;
|
50
|
|
51
|
private String citationDetail = null;
|
52
|
|
53
|
private String submitterUserName = null;
|
54
|
|
55
|
private EntityReference name = null;
|
56
|
|
57
|
private TypeDesignationSetManager typeDesignationManager;
|
58
|
|
59
|
private Registration reg;
|
60
|
|
61
|
private List<String> validationProblems = new ArrayList<>();
|
62
|
|
63
|
private Set<TypedEntityReference<Registration>> blockedBy;
|
64
|
|
65
|
private List<TaggedText> summaryTaggedText;
|
66
|
|
67
|
private String nomenclaturalCitationString;
|
68
|
|
69
|
private String bibliographicCitationString;
|
70
|
|
71
|
private String bibliographicInRefCitationString;
|
72
|
|
73
|
|
74
|
/**
|
75
|
* @param reg
|
76
|
* @param typifiedName should be provided in for Registrations for TypeDesignations
|
77
|
* @throws RegistrationValidationException
|
78
|
*/
|
79
|
public RegistrationDTO(Registration reg) {
|
80
|
|
81
|
this.reg = reg;
|
82
|
|
83
|
registrationType = RegistrationType.from(reg);
|
84
|
|
85
|
if(reg.getSubmitter() != null ){
|
86
|
submitterUserName = reg.getSubmitter().getUsername();
|
87
|
}
|
88
|
|
89
|
if(hasName(reg)){
|
90
|
citation = reg.getName().getNomenclaturalReference();
|
91
|
citationDetail = reg.getName().getNomenclaturalMicroReference();
|
92
|
name = new EntityReference(reg.getName().getUuid(), reg.getName().getTitleCache());
|
93
|
}
|
94
|
if(hasTypifications(reg)){
|
95
|
if(!reg.getTypeDesignations().isEmpty()){
|
96
|
for(TypeDesignationBase<?> td : reg.getTypeDesignations()){
|
97
|
if(citation == null) {
|
98
|
citation = td.getCitation();
|
99
|
citationDetail = td.getCitationMicroReference();
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
switch(registrationType) {
|
105
|
case EMPTY:
|
106
|
summary = "BLANK REGISTRATION";
|
107
|
summaryTaggedText = Arrays.asList(new TaggedText(TagEnum.label, summary));
|
108
|
break;
|
109
|
case NAME:
|
110
|
summary = reg.getName().getTitleCache();
|
111
|
summaryTaggedText = reg.getName().getTaggedName();
|
112
|
break;
|
113
|
case NAME_AND_TYPIFICATION:
|
114
|
case TYPIFICATION:
|
115
|
default:
|
116
|
try {
|
117
|
typeDesignationManager = new TypeDesignationSetManager(reg.getTypeDesignations());
|
118
|
summary = typeDesignationManager.print(true, true, true);
|
119
|
summaryTaggedText = typeDesignationManager.toTaggedText(true, true, true);
|
120
|
} catch (RegistrationValidationException e) {
|
121
|
validationProblems.add("Validation errors: " + e.getMessage());
|
122
|
}
|
123
|
break;
|
124
|
}
|
125
|
|
126
|
makeBibliographicCitationStrings();
|
127
|
makeNomenclaturalCitationString();
|
128
|
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* To create an initially empty DTO for which only the <code>typifiedName</code> and the <code>publication</code> are defined.
|
133
|
* All TypeDesignations added to the <code>Registration</code> need to refer to the same <code>typifiedName</code> and must be
|
134
|
* published in the same <code>publication</code>.
|
135
|
*
|
136
|
* @param reg
|
137
|
* @param typifiedName
|
138
|
*/
|
139
|
public RegistrationDTO(Registration reg, TaxonName typifiedName, Reference publication) {
|
140
|
this.reg = reg;
|
141
|
citation = publication;
|
142
|
// create a TypeDesignationSetManager with only a reference to the typifiedName for validation
|
143
|
typeDesignationManager = new TypeDesignationSetManager(typifiedName);
|
144
|
makeBibliographicCitationStrings();
|
145
|
makeNomenclaturalCitationString();
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* @param reg
|
150
|
* @return
|
151
|
*/
|
152
|
private boolean hasTypifications(Registration reg) {
|
153
|
return reg.getTypeDesignations() != null && reg.getTypeDesignations().size() > 0;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* @param reg
|
158
|
* @return
|
159
|
*/
|
160
|
private boolean hasName(Registration reg) {
|
161
|
return reg.getName() != null;
|
162
|
}
|
163
|
|
164
|
|
165
|
/**
|
166
|
* Provides access to the Registration entity this DTO has been build from.
|
167
|
* This method is purposely not a getter to hide the original Registration
|
168
|
* from generic processes which are exposing, binding bean properties.
|
169
|
*IReference
|
170
|
* @return
|
171
|
*/
|
172
|
public Registration registration() {
|
173
|
return reg;
|
174
|
}
|
175
|
|
176
|
|
177
|
/**
|
178
|
* @return the summary
|
179
|
*/
|
180
|
public String getSummary() {
|
181
|
return summary;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* @return the summary
|
186
|
*/
|
187
|
public List<TaggedText> getSummaryTaggedText() {
|
188
|
return summaryTaggedText;
|
189
|
}
|
190
|
|
191
|
public String getSubmitterUserName(){
|
192
|
return submitterUserName;
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* @return the registrationType
|
197
|
*/
|
198
|
public RegistrationType getRegistrationType() {
|
199
|
return registrationType;
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* @return the status
|
204
|
*/
|
205
|
public RegistrationStatus getStatus() {
|
206
|
return reg.getStatus();
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* @return the identifier
|
211
|
*/
|
212
|
public String getIdentifier() {
|
213
|
return reg.getIdentifier();
|
214
|
}
|
215
|
|
216
|
|
217
|
public UUID getUuid() {
|
218
|
return reg.getUuid();
|
219
|
}
|
220
|
|
221
|
/**
|
222
|
* @return the specificIdentifier
|
223
|
*/
|
224
|
public String getSpecificIdentifier() {
|
225
|
return reg.getSpecificIdentifier();
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* @return the registrationDate
|
230
|
*/
|
231
|
public DateTime getRegistrationDate() {
|
232
|
return reg.getRegistrationDate();
|
233
|
}
|
234
|
|
235
|
public String getInstitutionTitleCache(){
|
236
|
return reg.getInstitution() != null ? reg.getInstitution().getName() : null;
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* @return the registrationDate
|
241
|
*/
|
242
|
public VerbatimTimePeriod getDatePublished() {
|
243
|
return citation == null ? null : citation.getDatePublished();
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* @return the created
|
248
|
*/
|
249
|
public DateTime getCreated() {
|
250
|
return reg.getCreated();
|
251
|
}
|
252
|
|
253
|
public Reference getCitation() {
|
254
|
return citation;
|
255
|
}
|
256
|
|
257
|
public void setCitation(Reference citation) throws Exception {
|
258
|
if(this.citation == null){
|
259
|
this.citation = citation;
|
260
|
} else {
|
261
|
throw new Exception("Can not set the citation on a non emtpy RegistrationDTO");
|
262
|
}
|
263
|
makeBibliographicCitationStrings();
|
264
|
makeNomenclaturalCitationString();
|
265
|
}
|
266
|
|
267
|
|
268
|
public UUID getCitationUuid() {
|
269
|
return citation == null ? null : citation.getUuid();
|
270
|
}
|
271
|
|
272
|
public EntityReference getTypifiedNameRef() {
|
273
|
return typeDesignationManager != null ? typeDesignationManager.getTypifiedNameRef() : null;
|
274
|
}
|
275
|
|
276
|
public TaxonName getTypifiedName() {
|
277
|
return typeDesignationManager != null ? typeDesignationManager.getTypifiedName() : null;
|
278
|
}
|
279
|
|
280
|
public EntityReference getNameRef() {
|
281
|
return name;
|
282
|
}
|
283
|
|
284
|
public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationWorkingSets() {
|
285
|
return typeDesignationManager != null ? typeDesignationManager.getOrderdTypeDesignationWorkingSets() : null;
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* @param baseEntityReference
|
290
|
*/
|
291
|
public TypeDesignationWorkingSet getTypeDesignationWorkingSet(TypedEntityReference baseEntityReference) {
|
292
|
return typeDesignationManager != null ? typeDesignationManager.getOrderdTypeDesignationWorkingSets().get(baseEntityReference) : null;
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* @param baseEntityReference
|
297
|
*/
|
298
|
public Set<TypeDesignationBase> getTypeDesignationsInWorkingSet(TypedEntityReference baseEntityReference) {
|
299
|
Set<TypeDesignationBase> typeDesignations = new HashSet<>();
|
300
|
TypeDesignationWorkingSet workingSet = getTypeDesignationWorkingSet(baseEntityReference);
|
301
|
for(EntityReference ref : workingSet.getTypeDesignations()){
|
302
|
typeDesignations.add(findTypeDesignation(ref));
|
303
|
}
|
304
|
return typeDesignations;
|
305
|
}
|
306
|
|
307
|
public NameTypeDesignation getNameTypeDesignation(TypedEntityReference baseEntityReference) {
|
308
|
Set<TypeDesignationBase> typeDesignations = getTypeDesignationsInWorkingSet(baseEntityReference);
|
309
|
if(typeDesignations.size() == 1){
|
310
|
TypeDesignationBase item = typeDesignations.iterator().next();
|
311
|
return (NameTypeDesignation)item ;
|
312
|
}
|
313
|
if(typeDesignations.size() == 0){
|
314
|
return null;
|
315
|
}
|
316
|
if(typeDesignations.size() > 1){
|
317
|
throw new RuntimeException("Workingsets of NameTypeDesignations must contain exactly one item.");
|
318
|
}
|
319
|
return null;
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* @param ref
|
324
|
* @return
|
325
|
*/
|
326
|
private TypeDesignationBase<?> findTypeDesignation(EntityReference ref) {
|
327
|
return typeDesignationManager != null ? typeDesignationManager.findTypeDesignation(ref) : null;
|
328
|
}
|
329
|
|
330
|
public Collection<TypeDesignationBase<?>> typeDesignations() {
|
331
|
return typeDesignationManager != null ? typeDesignationManager.getTypeDesignations() : null;
|
332
|
}
|
333
|
|
334
|
private void makeNomenclaturalCitationString() {
|
335
|
if(citation == null){
|
336
|
nomenclaturalCitationString = null;
|
337
|
} else {
|
338
|
if(INomenclaturalReference.class.isAssignableFrom(citation.getClass())){
|
339
|
nomenclaturalCitationString = ((INomenclaturalReference)citation).getNomenclaturalCitation(citationDetail);
|
340
|
} else {
|
341
|
logger.error("The citation is not a NomenclaturalReference");
|
342
|
nomenclaturalCitationString = citation.generateTitle();
|
343
|
}
|
344
|
}
|
345
|
}
|
346
|
|
347
|
private void makeBibliographicCitationStrings() {
|
348
|
if(citation == null){
|
349
|
bibliographicCitationString = null;
|
350
|
} else {
|
351
|
Reference bibliographicCitation;
|
352
|
String bibliographicCitationDetail = citationDetail;
|
353
|
if((citation.getType() == ReferenceType.Section || citation.getType() == ReferenceType.BookSection) && citation.getInReference() != null){
|
354
|
bibliographicCitation = citation.getInReference();
|
355
|
bibliographicCitationDetail = null; // can possibly be known once https://dev.e-taxonomy.eu/redmine/issues/6623 is solved
|
356
|
} else {
|
357
|
bibliographicCitation = citation;
|
358
|
}
|
359
|
if(StringUtils.isNotEmpty(bibliographicCitationDetail)){
|
360
|
// TODO see https://dev.e-taxonomy.eu/redmine/issues/6623
|
361
|
bibliographicInRefCitationString = bibliographicCitation.generateTitle().replaceAll("\\.$", "") + (StringUtils.isNotEmpty(bibliographicCitationDetail) ? ": " + bibliographicCitationDetail : "");
|
362
|
} else {
|
363
|
bibliographicInRefCitationString = bibliographicCitation.generateTitle();
|
364
|
}
|
365
|
if(StringUtils.isNotEmpty(citationDetail)){
|
366
|
// TODO see https://dev.e-taxonomy.eu/redmine/issues/6623
|
367
|
bibliographicCitationString = citation.generateTitle().replaceAll("\\.$", "") + (StringUtils.isNotEmpty(citationDetail) ? ": " + citationDetail : "");
|
368
|
} else {
|
369
|
bibliographicCitationString = citation.generateTitle();
|
370
|
}
|
371
|
|
372
|
}
|
373
|
}
|
374
|
|
375
|
/**
|
376
|
* The nomenclatural citation is always the nomenclaturalCitation of the reference which is directly
|
377
|
* associated with the registration.
|
378
|
* <p>
|
379
|
* <b>Note:</b>Compare with {@link #getBibliographicCitationString()}
|
380
|
*
|
381
|
* @return the nomenclaturalCitationString
|
382
|
*/
|
383
|
public String getNomenclaturalCitationString() {
|
384
|
return nomenclaturalCitationString;
|
385
|
}
|
386
|
|
387
|
/**
|
388
|
* The bibliographic in-reference citation is either taken from the reference which is directly
|
389
|
* associated with the registration. In case this reference is a {@link eu.etaxonomy.cdm.model.reference.ReferenceType#Section} or
|
390
|
* {@link eu.etaxonomy.cdm.model.reference.ReferenceType#BookSection} the inReference will be taken instead.
|
391
|
* <p>
|
392
|
* <b>Note:</b>Compare with {@link #getBibliographicCitationString()}
|
393
|
*
|
394
|
* @return the bibliographicInRefCitationString
|
395
|
*/
|
396
|
public String getBibliographicInRefCitationString() {
|
397
|
return bibliographicInRefCitationString;
|
398
|
}
|
399
|
|
400
|
/**
|
401
|
* The bibliographic citation is either reference which is directly
|
402
|
* associated with the registration.
|
403
|
* <p>
|
404
|
* <b>Note:</b>Compare with {@link #getBibliographicInRefCitationString()}
|
405
|
*
|
406
|
* @return the bibliographicCitationString
|
407
|
*/
|
408
|
public String getBibliographicCitationString() {
|
409
|
return bibliographicCitationString;
|
410
|
}
|
411
|
|
412
|
public boolean isBlocked() {
|
413
|
return reg.getBlockedBy() != null && !reg.getBlockedBy().isEmpty();
|
414
|
}
|
415
|
|
416
|
/**
|
417
|
* @return the blockedBy
|
418
|
*/
|
419
|
public Set<TypedEntityReference<Registration>> getBlockedBy() {
|
420
|
|
421
|
if(blockedBy == null){
|
422
|
blockedBy = new HashSet<>();
|
423
|
if(reg.getBlockedBy() != null){
|
424
|
for(Registration blockReg : reg.getBlockedBy()){
|
425
|
blockedBy.add(new TypedEntityReference<Registration>(Registration.class, blockReg.getUuid(), blockReg.getIdentifier()));
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
return blockedBy;
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
* @return
|
434
|
*/
|
435
|
public List<String> getValidationProblems() {
|
436
|
return validationProblems;
|
437
|
}
|
438
|
|
439
|
/**
|
440
|
* @return
|
441
|
*/
|
442
|
public boolean isPersisted() {
|
443
|
return reg.isPersited();
|
444
|
}
|
445
|
|
446
|
}
|