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 relationship
.getFromName().equals(persistedRelatedName
);
165 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());
174 // need to remove the old relationship and to create a new one.
175 // the actual removal will take place ....
176 if(direction
== Direction
.relatedTo
){
177 name
.addRelationshipFromName(nameRelDto
.getOtherName(), nameRelationshipType
,
178 nameRelDto
.getCitation(), nameRelDto
.getCitationMicroReference(), nameRelDto
.getRuleConsidered());
180 name
.addRelationshipToName(nameRelDto
.getOtherName(), nameRelationshipType
,
181 nameRelDto
.getCitation(), nameRelDto
.getCitationMicroReference(), nameRelDto
.getRuleConsidered());
183 if(persistedRelatedName
!= null){
184 name
.removeRelationWithTaxonName(persistedRelatedName
, direction
, nameRelationshipType
);
189 if(persistedRelatedName
!= null && relationship
!= null){
190 name
.removeRelationWithTaxonName(persistedRelatedName
, direction
, nameRelationshipType
);
195 public void setBasionyms(Set
<TaxonName
> basionyms
) {
196 setRelatedNames(Direction
.relatedTo
, NameRelationshipType
.BASIONYM(), basionyms
);
199 public void setReplacedSynonyms(Set
<TaxonName
> replacedSynonyms
) {
200 setRelatedNames(Direction
.relatedTo
, NameRelationshipType
.REPLACED_SYNONYM(), replacedSynonyms
);
206 protected NameRelationship
uniqueNameRelationship(NameRelationshipType relationShipType
, Direction direction
) {
208 Set
<NameRelationship
> relations
;
210 if(direction
== Direction
.relatedTo
){
211 relations
= name
.getRelationsToThisName();
213 relations
= name
.getRelationsFromThisName();
215 Set
<NameRelationship
> nameRelations
= relations
.stream().filter(
216 nr
-> nr
.getType().equals(relationShipType
)
217 ).collect(Collectors
.toSet());
218 if(nameRelations
.size() > 1){
219 // TODO use non RuntimeException
220 throw new RuntimeException("More than one relationship of type " + relationShipType
.getLabel() + " found.");
221 } else if(nameRelations
.size() == 0) {
224 return nameRelations
.iterator().next();
232 protected void setRelatedNames(Direction direction
, NameRelationshipType relType
, Set
<TaxonName
> relatedNames
) {
233 Set
<TaxonName
> currentRelatedNames
= new HashSet
<>();
234 Set
<TaxonName
> namesSeen
= new HashSet
<>();
236 for(TaxonName tn
: name
.getRelatedNames(direction
, relType
)){
237 currentRelatedNames
.add(tn
);
239 for(TaxonName tn
: relatedNames
){
243 if(!currentRelatedNames
.contains(tn
)){
244 if(direction
.equals(Direction
.relatedTo
)){
245 tn
.addRelationshipToName(name
, relType
, null);
247 tn
.addRelationshipFromName(name
, relType
, null);
252 for(TaxonName tn
: currentRelatedNames
){
253 if(!namesSeen
.contains(tn
)){
254 name
.removeRelationWithTaxonName(tn
, direction
, relType
);
259 public TeamOrPersonBase
<?
> getCombinationAuthorship() {
260 return name
.getCombinationAuthorship();
263 public List
<Credit
> getCredits() {
264 return name
.getCredits();
267 public String
getCultivarName() {
268 return name
.getCultivarName();
271 public TeamOrPersonBase
<?
> getExBasionymAuthorship() {
272 return name
.getExBasionymAuthorship();
275 public TeamOrPersonBase
<?
> getExCombinationAuthorship() {
276 return name
.getExCombinationAuthorship();
279 public Set
<Extension
> getExtensions() {
280 return name
.getExtensions();
283 public String
getFullTitleCache() {
284 return name
.getFullTitleCache();
287 public String
getGenusOrUninomial() {
288 return name
.getGenusOrUninomial();
291 public HomotypicalGroup
getHomotypicalGroup() {
292 return name
.getHomotypicalGroup();
295 public List
<Identifier
> getIdentifiers() {
296 return name
.getIdentifiers();
299 public String
getInfraGenericEpithet() {
300 return name
.getInfraGenericEpithet();
303 public String
getInfraSpecificEpithet() {
304 return name
.getInfraSpecificEpithet();
307 public String
getSpecificEpithet() {
308 return name
.getSpecificEpithet();
311 public String
getNameCache() {
312 return name
.getNameCache();
315 public String
getNomenclaturalMicroReference() {
316 return name
.getNomenclaturalMicroReference();
319 public INomenclaturalReference
getNomenclaturalReference() {
320 return name
.getNomenclaturalReference();
323 public Rank
getRank() {
324 return name
.getRank();
327 public Set
<NomenclaturalStatus
> getStatus() {
328 return name
.getStatus();
331 public void setStatus(Set
<NomenclaturalStatus
> status
) throws SetterAdapterException
{
332 name
.setStatus(status
);
335 public boolean isProtectedAuthorshipCache() {
336 return name
.isProtectedAuthorshipCache();
339 public boolean isProtectedFullTitleCache() {
340 return name
.isProtectedFullTitleCache();
343 public boolean isProtectedNameCache() {
344 return name
.isProtectedNameCache();
347 public boolean isProtectedTitleCache() {
348 return name
.isProtectedTitleCache();
351 public void setAcronym(String acronym
) {
352 name
.setAcronym(acronym
);
355 public void setAppendedPhrase(String appendedPhrase
) {
356 name
.setAppendedPhrase(appendedPhrase
);
359 public void setBasionymAuthorship(TeamOrPersonBase
<?
> basionymAuthorship
) {
360 name
.setBasionymAuthorship(basionymAuthorship
);
363 public void setBinomHybrid(boolean binomHybrid
) {
364 name
.setBinomHybrid(binomHybrid
);
367 public void setBreed(String breed
) {
368 name
.setBreed(breed
);
371 public void setCombinationAuthorship(TeamOrPersonBase
<?
> combinationAuthorship
) {
372 name
.setCombinationAuthorship(combinationAuthorship
);
375 public void setCultivarName(String cultivarName
) {
376 name
.setCultivarName(cultivarName
);
379 public void setExBasionymAuthorship(TeamOrPersonBase
<?
> exBasionymAuthorship
) {
380 name
.setExBasionymAuthorship(exBasionymAuthorship
);
383 public void setExCombinationAuthorship(TeamOrPersonBase
<?
> exCombinationAuthorship
) {
384 name
.setExCombinationAuthorship(exCombinationAuthorship
);
387 public void setFullTitleCache(String fullTitleCache
) {
388 name
.setFullTitleCache(fullTitleCache
);
391 public void setGenusOrUninomial(String genusOrUninomial
) {
392 name
.setGenusOrUninomial(genusOrUninomial
);
395 public void setHybridFormula(boolean hybridFormula
) {
396 name
.setHybridFormula(hybridFormula
);
399 public void setInfraGenericEpithet(String infraGenericEpithet
) {
400 name
.setInfraGenericEpithet(infraGenericEpithet
);
403 public void setInfraSpecificEpithet(String infraSpecificEpithet
) {
404 name
.setInfraSpecificEpithet(infraSpecificEpithet
);
407 public void setMonomHybrid(boolean monomHybrid
) {
408 name
.setMonomHybrid(monomHybrid
);
411 public void setNameApprobation(String nameApprobation
) {
412 name
.setNameApprobation(nameApprobation
);
415 public void setNameCache(String nameCache
) {
416 name
.setNameCache(nameCache
);
419 public void setNameType(NomenclaturalCode nameType
) {
420 name
.setNameType(nameType
);
423 public void setNomenclaturalMicroReference(String nomenclaturalMicroReference
) {
424 name
.setNomenclaturalMicroReference(nomenclaturalMicroReference
);
427 public void setNomenclaturalReference(INomenclaturalReference nomenclaturalReference
) {
428 name
.setNomenclaturalReference(nomenclaturalReference
);
431 public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache
) {
432 name
.setProtectedAuthorshipCache(protectedAuthorshipCache
);
435 public void setProtectedFullTitleCache(boolean protectedFullTitleCache
) {
436 name
.setProtectedFullTitleCache(protectedFullTitleCache
);
439 public void setProtectedNameCache(boolean protectedNameCache
) {
440 name
.setProtectedNameCache(protectedNameCache
);
443 public void setProtectedTitleCache(boolean protectedTitleCache
) {
444 name
.setProtectedTitleCache(protectedTitleCache
);
447 public void setRank(Rank rank
) {
451 public void setSpecificEpithet(String specificEpithet
) {
452 name
.setSpecificEpithet(specificEpithet
);
455 public void setTitleCache(String titleCache
) {
456 name
.setTitleCache(titleCache
);
459 public void setTrinomHybrid(boolean trinomHybrid
) {
460 name
.setTrinomHybrid(trinomHybrid
);
463 public void setUpdated(DateTime updated
) {
464 name
.setUpdated(updated
);
467 public void setUpdatedBy(User updatedBy
) {
468 name
.setUpdatedBy(updatedBy
);