2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.service
.dto
;
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
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang3
.StringUtils
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.joda
.time
.DateTime
;
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
.ref
.EntityReference
;
36 import eu
.etaxonomy
.cdm
.ref
.TypedEntityReference
;
37 import eu
.etaxonomy
.cdm
.strategy
.cache
.TagEnum
;
38 import eu
.etaxonomy
.cdm
.strategy
.cache
.TaggedText
;
40 public class RegistrationDTO
{
42 private static final Logger logger
= Logger
.getLogger(RegistrationDTO
.class);
44 private String summary
= "";
46 private RegistrationType registrationType
;
48 private Reference citation
= null;
50 private String citationDetail
= null;
52 private String submitterUserName
= null;
54 private EntityReference name
= null;
56 private TypeDesignationSetManager typeDesignationManager
;
58 private Registration reg
;
60 private List
<String
> validationProblems
= new ArrayList
<>();
62 private Set
<TypedEntityReference
<Registration
>> blockedBy
;
64 private List
<TaggedText
> summaryTaggedText
;
69 * @param typifiedName should be provided in for Registrations for TypeDesignations
70 * @throws RegistrationValidationException
72 public RegistrationDTO(Registration reg
) {
76 registrationType
= RegistrationType
.from(reg
);
78 if(reg
.getSubmitter() != null ){
79 submitterUserName
= reg
.getSubmitter().getUsername();
83 citation
= reg
.getName().getNomenclaturalReference();
84 citationDetail
= reg
.getName().getNomenclaturalMicroReference();
85 name
= new EntityReference(reg
.getName().getUuid(), reg
.getName().getTitleCache());
87 if(hasTypifications(reg
)){
88 if(!reg
.getTypeDesignations().isEmpty()){
89 for(TypeDesignationBase td
: reg
.getTypeDesignations()){
90 if(citation
== null) {
91 citation
= td
.getCitation();
92 citationDetail
= td
.getCitationMicroReference();
97 switch(registrationType
) {
99 summary
= "BLANK REGISTRATION";
100 summaryTaggedText
= Arrays
.asList(new TaggedText(TagEnum
.label
, summary
));
103 summary
= reg
.getName().getTitleCache();
104 summaryTaggedText
= reg
.getName().getTaggedName();
106 case NAME_AND_TYPIFICATION
:
110 typeDesignationManager
= new TypeDesignationSetManager(reg
.getTypeDesignations());
111 summary
= typeDesignationManager
.print();
112 summaryTaggedText
= typeDesignationManager
.toTaggedText();
113 } catch (RegistrationValidationException e
) {
114 validationProblems
.add("Validation errors: " + e
.getMessage());
119 // trigger initialization of the reference
120 getNomenclaturalCitationString();
125 * To create an initially empty DTO for which only the <code>typifiedName</code> and the <code>publication</code> are defined.
126 * All TypeDesignations added to the <code>Registration</code> need to refer to the same <code>typifiedName</code> and must be
127 * published in the same <code>publication</code>.
130 * @param typifiedName
132 public RegistrationDTO(Registration reg
, TaxonName typifiedName
, Reference publication
) {
134 citation
= publication
;
135 // create a TypeDesignationSetManager with only a reference to the typifiedName for validation
136 typeDesignationManager
= new TypeDesignationSetManager(typifiedName
);
143 private boolean hasTypifications(Registration reg
) {
144 return reg
.getTypeDesignations() != null && reg
.getTypeDesignations().size() > 0;
151 private boolean hasName(Registration reg
) {
152 return reg
.getName() != null;
157 * Provides access to the Registration entity this DTO has been build from.
158 * This method is purposely not a getter to hide the original Registration
159 * from generic processes which are exposing, binding bean properties.
163 public Registration
registration() {
169 * @return the summary
171 public String
getSummary() {
176 * @return the summary
178 public List
<TaggedText
> getSummaryTaggedText() {
179 return summaryTaggedText
;
182 public String
getSubmitterUserName(){
183 return submitterUserName
;
187 * @return the registrationType
189 public RegistrationType
getRegistrationType() {
190 return registrationType
;
196 public RegistrationStatus
getStatus() {
197 return reg
.getStatus();
201 * @return the identifier
203 public String
getIdentifier() {
204 return reg
.getIdentifier();
208 public UUID
getUuid() {
209 return reg
.getUuid();
213 * @return the specificIdentifier
215 public String
getSpecificIdentifier() {
216 return reg
.getSpecificIdentifier();
220 * @return the registrationDate
222 public DateTime
getRegistrationDate() {
223 return reg
.getRegistrationDate();
227 * @return the registrationDate
229 public VerbatimTimePeriod
getDatePublished() {
230 return citation
== null ?
null : citation
.getDatePublished();
234 * @return the created
236 public DateTime
getCreated() {
237 return reg
.getCreated();
240 public Reference
getCitation() {
244 public void setCitation(Reference citation
) throws Exception
{
245 if(this.citation
== null){
246 this.citation
= citation
;
248 throw new Exception("Can not set the citation on a non emtpy RegistrationDTO");
253 public UUID
getCitationUuid() {
254 return citation
== null ?
null : citation
.getUuid();
257 public EntityReference
getTypifiedNameRef() {
258 return typeDesignationManager
!= null ? typeDesignationManager
.getTypifiedNameRef() : null;
261 public TaxonName
getTypifiedName() {
262 return typeDesignationManager
!= null ? typeDesignationManager
.getTypifiedName() : null;
265 public EntityReference
getNameRef() {
269 public LinkedHashMap
<TypedEntityReference
, TypeDesignationWorkingSet
> getOrderdTypeDesignationWorkingSets() {
270 return typeDesignationManager
!= null ? typeDesignationManager
.getOrderdTypeDesignationWorkingSets() : null;
274 * @param baseEntityReference
276 public TypeDesignationWorkingSet
getTypeDesignationWorkingSet(TypedEntityReference baseEntityReference
) {
277 return typeDesignationManager
!= null ? typeDesignationManager
.getOrderdTypeDesignationWorkingSets().get(baseEntityReference
) : null;
282 * @param baseEntityReference
284 public Set
<TypeDesignationBase
> getTypeDesignationsInWorkingSet(TypedEntityReference baseEntityReference
) {
285 Set
<TypeDesignationBase
> typeDesignations
= new HashSet
<>();
286 TypeDesignationWorkingSet workingSet
= getTypeDesignationWorkingSet(baseEntityReference
);
287 for(EntityReference ref
: workingSet
.getTypeDesignations()){
288 typeDesignations
.add(findTypeDesignation(ref
));
290 return typeDesignations
;
293 public NameTypeDesignation
getNameTypeDesignation(TypedEntityReference baseEntityReference
) {
294 Set
<TypeDesignationBase
> typeDesignations
= getTypeDesignationsInWorkingSet(baseEntityReference
);
295 if(typeDesignations
.size() == 1){
296 TypeDesignationBase item
= typeDesignations
.iterator().next();
297 return (NameTypeDesignation
)item
;
299 if(typeDesignations
.size() == 0){
302 if(typeDesignations
.size() > 1){
303 throw new RuntimeException("Workingsets of NameTypeDesignations must contain exactly one item.");
312 private TypeDesignationBase
findTypeDesignation(EntityReference ref
) {
313 return typeDesignationManager
!= null ? typeDesignationManager
.findTypeDesignation(ref
) : null;
316 public Collection
<TypeDesignationBase
> typeDesignations() {
317 return typeDesignationManager
!= null ? typeDesignationManager
.getTypeDesignations() : null;
321 * @return the citationString
323 public String
getNomenclaturalCitationString() {
324 if(citation
== null){
327 if(INomenclaturalReference
.class.isAssignableFrom(citation
.getClass())){
328 return ((INomenclaturalReference
)citation
).getNomenclaturalCitation(citationDetail
);
330 logger
.error("The citation is not a NomenclaturalReference");
331 return citation
.generateTitle();
336 * @return the citationString
338 public String
getBibliographicCitationString() {
339 if(citation
== null){
342 if(StringUtils
.isNotEmpty(citationDetail
)){
343 // TODO see https://dev.e-taxonomy.eu/redmine/issues/6623
344 return citation
.generateTitle().replaceAll("\\.$", "") + (StringUtils
.isNotEmpty(citationDetail
) ?
": " + citationDetail
: "");
346 return citation
.generateTitle();
353 public boolean isBlocked() {
354 return reg
.getBlockedBy() != null && !reg
.getBlockedBy().isEmpty();
358 * @return the blockedBy
360 public Set
<TypedEntityReference
<Registration
>> getBlockedBy() {
362 if(blockedBy
== null){
363 blockedBy
= new HashSet
<>();
364 if(reg
.getBlockedBy() != null){
365 for(Registration blockReg
: reg
.getBlockedBy()){
366 blockedBy
.add(new TypedEntityReference
<Registration
>(Registration
.class, blockReg
.getUuid(), blockReg
.getIdentifier()));
376 public List
<String
> getValidationProblems() {
377 return validationProblems
;
383 public boolean isPersisted() {
384 return reg
.isPersited();