2 * Copyright (C) 2018 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
.vaadin
.model
.name
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashSet
;
13 import java
.util
.List
;
15 import java
.util
.stream
.Collectors
;
17 import org
.joda
.time
.DateTime
;
19 import eu
.etaxonomy
.cdm
.model
.EntityCollectionSetterAdapter
.SetterAdapterException
;
20 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Identifier
;
25 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
.Direction
;
26 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
29 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
30 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
31 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
33 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
35 import eu
.etaxonomy
.cdm
.vaadin
.model
.CdmEntityAdapterDTO
;
38 * @author a.kohlbecker
42 public class TaxonNameDTO
extends CdmEntityAdapterDTO
<TaxonName
> {
44 private static final long serialVersionUID
= -8018109905949198530L;
46 private TaxonName name
;
48 private TaxonName persistedValidatedName
;
50 private TaxonName persistedOrthographicVariant
;
55 public TaxonNameDTO(TaxonName entity
) {
60 public String
getAcronym() {
61 return name
.getAcronym();
64 public Set
<Annotation
> getAnnotations() {
65 return name
.getAnnotations();
68 public void setAnnotations(Set
<Annotation
> annotations
) {
69 List
<Annotation
> currentAnnotations
= new ArrayList
<>(name
.getAnnotations());
70 List
<Annotation
> annotationsSeen
= new ArrayList
<>();
71 for(Annotation a
: annotations
){
75 if(!currentAnnotations
.contains(a
)){
76 name
.addAnnotation(a
);
78 annotationsSeen
.add(a
);
80 for(Annotation a
: currentAnnotations
){
81 if(!annotationsSeen
.contains(a
)){
82 name
.removeAnnotation(a
);
87 public String
getAppendedPhrase() {
88 return name
.getAppendedPhrase();
91 public String
getAuthorshipCache() {
93 return name
.getAuthorshipCache();
96 public TeamOrPersonBase
<?
> getBasionymAuthorship() {
97 return name
.getBasionymAuthorship();
100 public Set
<TaxonName
> getBasionyms() {
101 Set
<TaxonName
> basionyms
= name
.getRelatedNames(Direction
.relatedTo
, NameRelationshipType
.BASIONYM());
105 public Set
<TaxonName
> getReplacedSynonyms() {
106 Set
<TaxonName
> replacedSynonyms
= name
.getRelatedNames(Direction
.relatedTo
, NameRelationshipType
.REPLACED_SYNONYM());
107 return replacedSynonyms
;
110 public NameRelationshipDTO
getValidationFor() {
111 NameRelationshipDTO nameRelDto
= null;
112 NameRelationship validatingRelationship
= uniqueNameRelationship(NameRelationshipType
.VALIDATED_BY_NAME(), Direction
.relatedTo
);
113 if(validatingRelationship
!= null){
114 nameRelDto
= new NameRelationshipDTO(Direction
.relatedTo
, validatingRelationship
);
115 if(persistedValidatedName
== null){
116 persistedValidatedName
= nameRelDto
.getOtherName();
122 public void setValidationFor(NameRelationshipDTO nameRelDto
) {
123 setUniqeNameRelationDTO(nameRelDto
, NameRelationshipType
.VALIDATED_BY_NAME(), Direction
.relatedTo
, persistedValidatedName
);
127 public NameRelationshipDTO
getOrthographicVariant() {
128 NameRelationshipDTO nameRelDto
= null;
129 NameRelationship nameRelationship
= uniqueNameRelationship(NameRelationshipType
.ORTHOGRAPHIC_VARIANT(), Direction
.relatedTo
);
130 if(nameRelationship
!= null){
131 nameRelDto
= new NameRelationshipDTO(Direction
.relatedTo
, nameRelationship
);
132 if(persistedOrthographicVariant
== null){
133 persistedOrthographicVariant
= nameRelDto
.getOtherName();
139 public void setOrthographicVariant(NameRelationshipDTO nameRelDto
) {
140 setUniqeNameRelationDTO(nameRelDto
, NameRelationshipType
.ORTHOGRAPHIC_VARIANT(), Direction
.relatedTo
, persistedOrthographicVariant
);
145 * @param nameRelationshipType
147 * @param persistedRelatedName
149 public void setUniqeNameRelationDTO(NameRelationshipDTO nameRelDto
, NameRelationshipType nameRelationshipType
,
150 Direction direction
, TaxonName persistedRelatedName
) {
151 if(nameRelDto
!= null && nameRelDto
.getOtherName() == null){
152 // treat as if there is no related name
156 NameRelationship relationship
= uniqueNameRelationship(nameRelationshipType
, direction
);
158 if(nameRelDto
!= null){
160 boolean currentNameIsTarget
= false;
161 if(relationship
!= null && persistedRelatedName
!= null){
162 if(direction
== Direction
.relatedTo
){
163 currentNameIsTarget
= relationship
.getFromName().equals(persistedRelatedName
);
165 currentNameIsTarget
= relationship
.getToName().equals(persistedRelatedName
);
168 if(relationship
!= null && currentNameIsTarget
){
169 // related name has not changed, so we can update the relation
170 relationship
.setCitation(nameRelDto
.getCitation());
171 relationship
.setCitationMicroReference(nameRelDto
.getCitationMicroReference());
172 relationship
.setRuleConsidered(nameRelDto
.getRuleConsidered());
173 relationship
.setCodeEdition(nameRelDto
.getCodeEdition());
175 // need to remove the old relationship and to create a new one.
176 // the actual removal will take place ....
177 if(direction
== Direction
.relatedTo
){
178 name
.addRelationshipFromName(nameRelDto
.getOtherName(), nameRelationshipType
,
179 nameRelDto
.getCitation(), nameRelDto
.getCitationMicroReference(), nameRelDto
.getRuleConsidered(), nameRelDto
.getCodeEdition());
181 name
.addRelationshipToName(nameRelDto
.getOtherName(), nameRelationshipType
,
182 nameRelDto
.getCitation(), nameRelDto
.getCitationMicroReference(), nameRelDto
.getRuleConsidered(), nameRelDto
.getCodeEdition());
184 if(persistedRelatedName
!= null){
185 name
.removeRelationWithTaxonName(persistedRelatedName
, direction
, nameRelationshipType
);
190 if(persistedRelatedName
!= null && relationship
!= null){
191 name
.removeRelationWithTaxonName(persistedRelatedName
, direction
, nameRelationshipType
);
196 public void setBasionyms(Set
<TaxonName
> basionyms
) {
197 setRelatedNames(Direction
.relatedTo
, NameRelationshipType
.BASIONYM(), basionyms
);
200 public void setReplacedSynonyms(Set
<TaxonName
> replacedSynonyms
) {
201 setRelatedNames(Direction
.relatedTo
, NameRelationshipType
.REPLACED_SYNONYM(), replacedSynonyms
);
207 protected NameRelationship
uniqueNameRelationship(NameRelationshipType relationShipType
, Direction direction
) {
209 Set
<NameRelationship
> relations
;
211 if(direction
== Direction
.relatedTo
){
212 relations
= name
.getRelationsToThisName();
214 relations
= name
.getRelationsFromThisName();
216 Set
<NameRelationship
> nameRelations
= relations
.stream().filter(
217 nr
-> nr
.getType().equals(relationShipType
)
218 ).collect(Collectors
.toSet());
219 if(nameRelations
.size() > 1){
220 // TODO use non RuntimeException
221 throw new RuntimeException("More than one relationship of type " + relationShipType
.getLabel() + " found.");
222 } else if(nameRelations
.size() == 0) {
225 return nameRelations
.iterator().next();
233 protected void setRelatedNames(Direction direction
, NameRelationshipType relType
, Set
<TaxonName
> relatedNames
) {
234 Set
<TaxonName
> currentRelatedNames
= new HashSet
<>();
235 Set
<TaxonName
> namesSeen
= new HashSet
<>();
237 for(TaxonName tn
: name
.getRelatedNames(direction
, relType
)){
238 currentRelatedNames
.add(tn
);
240 for(TaxonName tn
: relatedNames
){
244 if(!currentRelatedNames
.contains(tn
)){
245 if(direction
.equals(Direction
.relatedTo
)){
246 tn
.addRelationshipToName(name
, relType
, null, null);
248 tn
.addRelationshipFromName(name
, relType
, null, null);
253 for(TaxonName tn
: currentRelatedNames
){
254 if(!namesSeen
.contains(tn
)){
255 name
.removeRelationWithTaxonName(tn
, direction
, relType
);
260 public TeamOrPersonBase
<?
> getCombinationAuthorship() {
261 return name
.getCombinationAuthorship();
264 public List
<Credit
> getCredits() {
265 return name
.getCredits();
268 public String
getCultivarName() {
269 return name
.getCultivarName();
272 public TeamOrPersonBase
<?
> getExBasionymAuthorship() {
273 return name
.getExBasionymAuthorship();
276 public TeamOrPersonBase
<?
> getExCombinationAuthorship() {
277 return name
.getExCombinationAuthorship();
280 public Set
<Extension
> getExtensions() {
281 return name
.getExtensions();
284 public String
getFullTitleCache() {
285 return name
.getFullTitleCache();
288 public String
getGenusOrUninomial() {
289 return name
.getGenusOrUninomial();
292 public HomotypicalGroup
getHomotypicalGroup() {
293 return name
.getHomotypicalGroup();
296 public List
<Identifier
> getIdentifiers() {
297 return name
.getIdentifiers();
300 public String
getInfraGenericEpithet() {
301 return name
.getInfraGenericEpithet();
304 public String
getInfraSpecificEpithet() {
305 return name
.getInfraSpecificEpithet();
308 public String
getSpecificEpithet() {
309 return name
.getSpecificEpithet();
312 public String
getNameCache() {
313 return name
.getNameCache();
316 public String
getNomenclaturalMicroReference() {
317 return name
.getNomenclaturalMicroReference();
320 public INomenclaturalReference
getNomenclaturalReference() {
321 return name
.getNomenclaturalReference();
324 public Rank
getRank() {
325 return name
.getRank();
328 public Set
<NomenclaturalStatus
> getStatus() {
329 return name
.getStatus();
332 public void setStatus(Set
<NomenclaturalStatus
> status
) throws SetterAdapterException
{
333 name
.setStatus(status
);
336 public boolean isProtectedAuthorshipCache() {
337 return name
.isProtectedAuthorshipCache();
340 public boolean isProtectedFullTitleCache() {
341 return name
.isProtectedFullTitleCache();
344 public boolean isProtectedNameCache() {
345 return name
.isProtectedNameCache();
348 public boolean isProtectedTitleCache() {
349 return name
.isProtectedTitleCache();
352 public void setAcronym(String acronym
) {
353 name
.setAcronym(acronym
);
356 public void setAppendedPhrase(String appendedPhrase
) {
357 name
.setAppendedPhrase(appendedPhrase
);
360 public void setBasionymAuthorship(TeamOrPersonBase
<?
> basionymAuthorship
) {
361 name
.setBasionymAuthorship(basionymAuthorship
);
364 public void setBinomHybrid(boolean binomHybrid
) {
365 name
.setBinomHybrid(binomHybrid
);
368 public void setBreed(String breed
) {
369 name
.setBreed(breed
);
372 public void setCombinationAuthorship(TeamOrPersonBase
<?
> combinationAuthorship
) {
373 name
.setCombinationAuthorship(combinationAuthorship
);
376 public void setCultivarName(String cultivarName
) {
377 name
.setCultivarName(cultivarName
);
380 public void setExBasionymAuthorship(TeamOrPersonBase
<?
> exBasionymAuthorship
) {
381 name
.setExBasionymAuthorship(exBasionymAuthorship
);
384 public void setExCombinationAuthorship(TeamOrPersonBase
<?
> exCombinationAuthorship
) {
385 name
.setExCombinationAuthorship(exCombinationAuthorship
);
388 public void setFullTitleCache(String fullTitleCache
) {
389 name
.setFullTitleCache(fullTitleCache
);
392 public void setGenusOrUninomial(String genusOrUninomial
) {
393 name
.setGenusOrUninomial(genusOrUninomial
);
396 public void setHybridFormula(boolean hybridFormula
) {
397 name
.setHybridFormula(hybridFormula
);
400 public void setInfraGenericEpithet(String infraGenericEpithet
) {
401 name
.setInfraGenericEpithet(infraGenericEpithet
);
404 public void setInfraSpecificEpithet(String infraSpecificEpithet
) {
405 name
.setInfraSpecificEpithet(infraSpecificEpithet
);
408 public void setMonomHybrid(boolean monomHybrid
) {
409 name
.setMonomHybrid(monomHybrid
);
412 public void setNameApprobation(String nameApprobation
) {
413 name
.setNameApprobation(nameApprobation
);
416 public void setNameCache(String nameCache
) {
417 name
.setNameCache(nameCache
);
420 public void setNameType(NomenclaturalCode nameType
) {
421 name
.setNameType(nameType
);
424 public void setNomenclaturalMicroReference(String nomenclaturalMicroReference
) {
425 name
.setNomenclaturalMicroReference(nomenclaturalMicroReference
);
428 public void setNomenclaturalReference(INomenclaturalReference nomenclaturalReference
) {
429 name
.setNomenclaturalReference(nomenclaturalReference
);
432 public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache
) {
433 name
.setProtectedAuthorshipCache(protectedAuthorshipCache
);
436 public void setProtectedFullTitleCache(boolean protectedFullTitleCache
) {
437 name
.setProtectedFullTitleCache(protectedFullTitleCache
);
440 public void setProtectedNameCache(boolean protectedNameCache
) {
441 name
.setProtectedNameCache(protectedNameCache
);
444 public void setProtectedTitleCache(boolean protectedTitleCache
) {
445 name
.setProtectedTitleCache(protectedTitleCache
);
448 public void setRank(Rank rank
) {
452 public void setSpecificEpithet(String specificEpithet
) {
453 name
.setSpecificEpithet(specificEpithet
);
456 public void setTitleCache(String titleCache
) {
457 name
.setTitleCache(titleCache
);
460 public void setTrinomHybrid(boolean trinomHybrid
) {
461 name
.setTrinomHybrid(trinomHybrid
);
464 public void setUpdated(DateTime updated
) {
465 name
.setUpdated(updated
);
468 public void setUpdatedBy(User updatedBy
) {
469 name
.setUpdatedBy(updatedBy
);