Project

General

Profile

Download (16 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.vaadin.model.name;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.stream.Collectors;
16

    
17
import org.joda.time.DateTime;
18

    
19
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
20
import eu.etaxonomy.cdm.model.common.Annotation;
21
import eu.etaxonomy.cdm.model.common.Credit;
22
import eu.etaxonomy.cdm.model.common.Extension;
23
import eu.etaxonomy.cdm.model.common.Identifier;
24
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
25
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
26
import eu.etaxonomy.cdm.model.name.NameRelationship;
27
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
28
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
29
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
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.model.reference.Reference;
36
import eu.etaxonomy.cdm.vaadin.model.CdmEntityAdapterDTO;
37

    
38
/**
39
 * @author a.kohlbecker
40
 * @since Apr 23, 2018
41
 *
42
 */
43
public class TaxonNameDTO extends CdmEntityAdapterDTO<TaxonName> {
44

    
45
    private static final long serialVersionUID = -8018109905949198530L;
46

    
47
    private TaxonName name;
48

    
49
    private TaxonName persistedValidatedName;
50

    
51
    private TaxonName persistedOrthographicVariant;
52

    
53
    private Set<NomenclaturalStatusDTO> nomenclaturalStatusDTOs = new HashSet<>();
54

    
55
    /**
56
     * @param entity
57
     */
58
    public TaxonNameDTO(TaxonName entity) {
59
        super(entity);
60
        name = entity;
61
        for(NomenclaturalStatus status : name.getStatus()) {
62
            nomenclaturalStatusDTOs.add(NomenclaturalStatusDTO.from(status));
63
        }
64
    }
65

    
66
    public String getAcronym() {
67
        return name.getAcronym();
68
    }
69

    
70
    public Set<Annotation> getAnnotations() {
71
        return name.getAnnotations();
72
    }
73

    
74
    public void setAnnotations(Set<Annotation> annotations) {
75
        List<Annotation> currentAnnotations = new ArrayList<>(name.getAnnotations());
76
        List<Annotation> annotationsSeen = new ArrayList<>();
77
        for(Annotation a : annotations){
78
            if(a == null){
79
                continue;
80
            }
81
            if(!currentAnnotations.contains(a)){
82
                name.addAnnotation(a);
83
            }
84
            annotationsSeen.add(a);
85
        }
86
        for(Annotation a : currentAnnotations){
87
            if(!annotationsSeen.contains(a)){
88
                name.removeAnnotation(a);
89
            }
90
        }
91
    }
92

    
93
    public String getAppendedPhrase() {
94
        return name.getAppendedPhrase();
95
    }
96

    
97
    public String getAuthorshipCache() {
98

    
99
        return name.getAuthorshipCache();
100
    }
101

    
102
    public TeamOrPersonBase<?> getBasionymAuthorship() {
103
        return name.getBasionymAuthorship();
104
    }
105

    
106
    public Set<TaxonName> getBasionyms() {
107
        Set<TaxonName> basionyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM());
108
        return basionyms;
109
    }
110

    
111
    public Set<TaxonName> getReplacedSynonyms() {
112
        Set<TaxonName> replacedSynonyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM());
113
        return replacedSynonyms;
114
    }
115

    
116
    public NameRelationshipDTO getValidationFor() {
117
        NameRelationshipDTO nameRelDto  = null;
118
        NameRelationship validatingRelationship = uniqueNameRelationship(NameRelationshipType.VALIDATED_BY_NAME(), Direction.relatedTo);
119
        if(validatingRelationship != null){
120
            nameRelDto = new NameRelationshipDTO(Direction.relatedTo, validatingRelationship);
121
            if(persistedValidatedName == null){
122
               persistedValidatedName = nameRelDto.getOtherName();
123
            }
124
        }
125
        return nameRelDto;
126
    }
127

    
128
    public void setValidationFor(NameRelationshipDTO nameRelDto) {
129
        setUniqeNameRelationDTO(nameRelDto, NameRelationshipType.VALIDATED_BY_NAME(), Direction.relatedTo, persistedValidatedName);
130
    }
131

    
132

    
133
    public NameRelationshipDTO getOrthographicVariant() {
134
        NameRelationshipDTO nameRelDto  = null;
135
        NameRelationship nameRelationship = uniqueNameRelationship(NameRelationshipType.ORTHOGRAPHIC_VARIANT(), Direction.relatedTo);
136
        if(nameRelationship != null){
137
            nameRelDto = new NameRelationshipDTO(Direction.relatedTo, nameRelationship);
138
            if(persistedOrthographicVariant == null){
139
               persistedOrthographicVariant = nameRelDto.getOtherName();
140
            }
141
        }
142
        return nameRelDto;
143
    }
144

    
145
    public void setOrthographicVariant(NameRelationshipDTO nameRelDto) {
146
        setUniqeNameRelationDTO(nameRelDto, NameRelationshipType.ORTHOGRAPHIC_VARIANT(), Direction.relatedTo, persistedOrthographicVariant);
147
    }
148

    
149
    /**
150
     * @param nameRelDto
151
     * @param nameRelationshipType
152
     * @param direction
153
     * @param persistedRelatedName
154
     */
155
    public void setUniqeNameRelationDTO(NameRelationshipDTO nameRelDto, NameRelationshipType nameRelationshipType,
156
            Direction direction, TaxonName persistedRelatedName) {
157
        if(nameRelDto != null && nameRelDto.getOtherName() == null){
158
            // treat as if there is no related name
159
            nameRelDto = null;
160
        }
161

    
162
        NameRelationship relationship = uniqueNameRelationship(nameRelationshipType, direction);
163

    
164
        if(nameRelDto != null){
165
            // add or update ...
166
            boolean currentNameIsTarget = false;
167
            if(relationship != null && persistedRelatedName != null){
168
                if(direction == Direction.relatedTo){
169
                    currentNameIsTarget = relationship.getFromName().equals(persistedRelatedName);
170
                } else {
171
                    currentNameIsTarget = relationship.getToName().equals(persistedRelatedName);
172
                }
173
            }
174
            if(relationship != null && currentNameIsTarget){
175
                // related name has not changed, so we can update the relation
176
                relationship.setCitation(nameRelDto.getCitation());
177
                relationship.setCitationMicroReference(nameRelDto.getCitationMicroReference());
178
                relationship.setRuleConsidered(nameRelDto.getRuleConsidered());
179
                relationship.setCodeEdition(nameRelDto.getCodeEdition());
180
            } else {
181
                // need to remove the old relationship and to create a new one.
182
                // the actual removal will take place ....
183
                if(direction == Direction.relatedTo){
184
                    name.addRelationshipFromName(nameRelDto.getOtherName(), nameRelationshipType,
185
                            nameRelDto.getCitation(), nameRelDto.getCitationMicroReference(), nameRelDto.getRuleConsidered(), nameRelDto.getCodeEdition());
186
                } else {
187
                    name.addRelationshipToName(nameRelDto.getOtherName(), nameRelationshipType,
188
                            nameRelDto.getCitation(), nameRelDto.getCitationMicroReference(), nameRelDto.getRuleConsidered(), nameRelDto.getCodeEdition());
189
                }
190
                if(persistedRelatedName != null){
191
                    name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
192
                }
193
            }
194
        } else {
195
            // remove ...
196
            if(persistedRelatedName != null && relationship != null){
197
                name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
198
            }
199
        }
200
    }
201

    
202
    public void setBasionyms(Set<TaxonName> basionyms) {
203
        setRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM(), basionyms);
204
    }
205

    
206
    public void setReplacedSynonyms(Set<TaxonName> replacedSynonyms) {
207
        setRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM(), replacedSynonyms);
208
    }
209

    
210
    /**
211
     * @return
212
     */
213
    protected NameRelationship uniqueNameRelationship(NameRelationshipType relationShipType, Direction direction) {
214

    
215
        Set<NameRelationship> relations;
216

    
217
        if(direction == Direction.relatedTo){
218
            relations = name.getRelationsToThisName();
219
        } else {
220
            relations = name.getRelationsFromThisName();
221
        }
222
        Set<NameRelationship> nameRelations = relations.stream().filter(
223
                    nr -> nr.getType().equals(relationShipType)
224
                ).collect(Collectors.toSet());
225
        if(nameRelations.size() > 1){
226
            // TODO use non RuntimeException
227
            throw new RuntimeException("More than one relationship of type " + relationShipType.getLabel() + " found.");
228
        } else if(nameRelations.size() == 0) {
229
            return null;
230
        }
231
        return nameRelations.iterator().next();
232
    }
233

    
234
    /**
235
     * @param basionyms
236
     * @param relType
237
     * @param direction
238
     */
239
    protected void setRelatedNames(Direction direction, NameRelationshipType relType, Set<TaxonName> relatedNames) {
240
        Set<TaxonName> currentRelatedNames = new HashSet<>();
241
        Set<TaxonName> namesSeen = new HashSet<>();
242

    
243
        for(TaxonName tn : name.getRelatedNames(direction, relType)){
244
            currentRelatedNames.add(tn);
245
        }
246
        for(TaxonName tn : relatedNames){
247
            if(tn == null){
248
                continue;
249
            }
250
            if(!currentRelatedNames.contains(tn)){
251
                if(direction.equals(Direction.relatedTo)){
252
                    tn.addRelationshipToName(name, relType, null, null);
253
                } else {
254
                    tn.addRelationshipFromName(name, relType, null, null);
255
                }
256
            }
257
            namesSeen.add(tn);
258
        }
259
        for(TaxonName tn : currentRelatedNames){
260
            if(!namesSeen.contains(tn)){
261
                name.removeRelationWithTaxonName(tn, direction, relType);
262
            }
263
        }
264
    }
265

    
266
    public TeamOrPersonBase<?> getCombinationAuthorship() {
267
        return name.getCombinationAuthorship();
268
    }
269

    
270
    public List<Credit> getCredits() {
271
        return name.getCredits();
272
    }
273

    
274
    public String getCultivarName() {
275
        return name.getCultivarName();
276
    }
277

    
278
    public TeamOrPersonBase<?> getExBasionymAuthorship() {
279
        return name.getExBasionymAuthorship();
280
    }
281

    
282
    public TeamOrPersonBase<?> getExCombinationAuthorship() {
283
        return name.getExCombinationAuthorship();
284
    }
285

    
286
    public Set<Extension> getExtensions() {
287
        return name.getExtensions();
288
    }
289

    
290
    public String getFullTitleCache() {
291
        return name.getFullTitleCache();
292
    }
293

    
294
    public String getGenusOrUninomial() {
295
        return name.getGenusOrUninomial();
296
    }
297

    
298
    public HomotypicalGroup getHomotypicalGroup() {
299
        return name.getHomotypicalGroup();
300
    }
301

    
302
    public List<Identifier> getIdentifiers() {
303
        return name.getIdentifiers();
304
    }
305

    
306
    public String getInfraGenericEpithet() {
307
        return name.getInfraGenericEpithet();
308
    }
309

    
310
    public String getInfraSpecificEpithet() {
311
        return name.getInfraSpecificEpithet();
312
    }
313

    
314
    public String getSpecificEpithet() {
315
        return name.getSpecificEpithet();
316
    }
317

    
318
    public String getNameCache() {
319
        return name.getNameCache();
320
    }
321

    
322
    public String getNomenclaturalMicroReference() {
323
        return name.getNomenclaturalMicroReference();
324
    }
325

    
326
    public INomenclaturalReference getNomenclaturalReference() {
327
        return name.getNomenclaturalReference();
328
    }
329

    
330
    public Rank getRank() {
331
        return name.getRank();
332
    }
333

    
334
    public Set<NomenclaturalStatusDTO> getStatus() {
335
        return nomenclaturalStatusDTOs;
336
    }
337

    
338
    public void setStatus(Set<NomenclaturalStatusDTO> status) {
339
        nomenclaturalStatusDTOs = status;
340
    }
341

    
342
    public boolean isProtectedAuthorshipCache() {
343
        return name.isProtectedAuthorshipCache();
344
    }
345

    
346
    public boolean isProtectedFullTitleCache() {
347
        return name.isProtectedFullTitleCache();
348
    }
349

    
350
    public boolean isProtectedNameCache() {
351
        return name.isProtectedNameCache();
352
    }
353

    
354
    public boolean isProtectedTitleCache() {
355
        return name.isProtectedTitleCache();
356
    }
357

    
358
    public void setAcronym(String acronym) {
359
        name.setAcronym(acronym);
360
    }
361

    
362
    public void setAppendedPhrase(String appendedPhrase) {
363
        name.setAppendedPhrase(appendedPhrase);
364
    }
365

    
366
    public void setBasionymAuthorship(TeamOrPersonBase<?> basionymAuthorship) {
367
        name.setBasionymAuthorship(basionymAuthorship);
368
    }
369

    
370
    public void setBinomHybrid(boolean binomHybrid) {
371
        name.setBinomHybrid(binomHybrid);
372
    }
373

    
374
    public void setBreed(String breed) {
375
        name.setBreed(breed);
376
    }
377

    
378
    public void setCombinationAuthorship(TeamOrPersonBase<?> combinationAuthorship) {
379
        name.setCombinationAuthorship(combinationAuthorship);
380
    }
381

    
382
    public void setCultivarName(String cultivarName) {
383
        name.setCultivarName(cultivarName);
384
    }
385

    
386
    public void setExBasionymAuthorship(TeamOrPersonBase<?> exBasionymAuthorship) {
387
        name.setExBasionymAuthorship(exBasionymAuthorship);
388
    }
389

    
390
    public void setExCombinationAuthorship(TeamOrPersonBase<?> exCombinationAuthorship) {
391
        name.setExCombinationAuthorship(exCombinationAuthorship);
392
    }
393

    
394
    public void setFullTitleCache(String fullTitleCache) {
395
        name.setFullTitleCache(fullTitleCache);
396
    }
397

    
398
    public void setGenusOrUninomial(String genusOrUninomial) {
399
        name.setGenusOrUninomial(genusOrUninomial);
400
    }
401

    
402
    public void setHybridFormula(boolean hybridFormula) {
403
        name.setHybridFormula(hybridFormula);
404
    }
405

    
406
    public void setInfraGenericEpithet(String infraGenericEpithet) {
407
        name.setInfraGenericEpithet(infraGenericEpithet);
408
    }
409

    
410
    public void setInfraSpecificEpithet(String infraSpecificEpithet) {
411
        name.setInfraSpecificEpithet(infraSpecificEpithet);
412
    }
413

    
414
    public void setMonomHybrid(boolean monomHybrid) {
415
        name.setMonomHybrid(monomHybrid);
416
    }
417

    
418
    public void setNameApprobation(String nameApprobation) {
419
        name.setNameApprobation(nameApprobation);
420
    }
421

    
422
    public void setNameCache(String nameCache) {
423
        name.setNameCache(nameCache);
424
    }
425

    
426
    public void setNameType(NomenclaturalCode nameType) {
427
        name.setNameType(nameType);
428
    }
429

    
430
    public void setNomenclaturalMicroReference(String nomenclaturalMicroReference) {
431
        assureNomenclaturalSource().setCitationMicroReference(nomenclaturalMicroReference);
432
    }
433

    
434
    public void setNomenclaturalReference(Reference nomenclaturalReference) {
435
        assureNomenclaturalSource().setCitation(nomenclaturalReference);
436
    }
437

    
438
    protected NomenclaturalSource assureNomenclaturalSource() {
439
        NomenclaturalSource nomSource = name.getNomenclaturalSource();
440
        if(nomSource == null) {
441
            nomSource = NomenclaturalSource.NewNomenclaturalInstance(name);
442
        }
443
        return nomSource;
444
    }
445

    
446
    public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache) {
447
        name.setProtectedAuthorshipCache(protectedAuthorshipCache);
448
    }
449

    
450
    public void setProtectedFullTitleCache(boolean protectedFullTitleCache) {
451
        name.setProtectedFullTitleCache(protectedFullTitleCache);
452
    }
453

    
454
    public void setProtectedNameCache(boolean protectedNameCache) {
455
        name.setProtectedNameCache(protectedNameCache);
456
    }
457

    
458
    public void setProtectedTitleCache(boolean protectedTitleCache) {
459
        name.setProtectedTitleCache(protectedTitleCache);
460
    }
461

    
462
    public void setRank(Rank rank) {
463
        name.setRank(rank);
464
    }
465

    
466
    public void setSpecificEpithet(String specificEpithet) {
467
        name.setSpecificEpithet(specificEpithet);
468
    }
469

    
470
    public void setTitleCache(String titleCache) {
471
        name.setTitleCache(titleCache);
472
    }
473

    
474
    public void setTrinomHybrid(boolean trinomHybrid) {
475
        name.setTrinomHybrid(trinomHybrid);
476
    }
477

    
478
    public void setUpdated(DateTime updated) {
479
        name.setUpdated(updated);
480
    }
481

    
482
    public void setUpdatedBy(User updatedBy) {
483
        name.setUpdatedBy(updatedBy);
484
    }
485

    
486
}
(3-3/3)