Project

General

Profile

Download (15.4 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.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;
36

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

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

    
46
    private TaxonName name;
47

    
48
    private TaxonName persistedValidatedName;
49

    
50
    private TaxonName persistedOrthographicVariant;
51

    
52
    /**
53
     * @param entity
54
     */
55
    public TaxonNameDTO(TaxonName entity) {
56
        super(entity);
57
        name = entity;
58
    }
59

    
60
    public String getAcronym() {
61
        return name.getAcronym();
62
    }
63

    
64
    public Set<Annotation> getAnnotations() {
65
        return name.getAnnotations();
66
    }
67

    
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){
72
            if(a == null){
73
                continue;
74
            }
75
            if(!currentAnnotations.contains(a)){
76
                name.addAnnotation(a);
77
            }
78
            annotationsSeen.add(a);
79
        }
80
        for(Annotation a : currentAnnotations){
81
            if(!annotationsSeen.contains(a)){
82
                name.removeAnnotation(a);
83
            }
84
        }
85
    }
86

    
87
    public String getAppendedPhrase() {
88
        return name.getAppendedPhrase();
89
    }
90

    
91
    public String getAuthorshipCache() {
92

    
93
        return name.getAuthorshipCache();
94
    }
95

    
96
    public TeamOrPersonBase<?> getBasionymAuthorship() {
97
        return name.getBasionymAuthorship();
98
    }
99

    
100
    public Set<TaxonName> getBasionyms() {
101
        Set<TaxonName> basionyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM());
102
        return basionyms;
103
    }
104

    
105
    public Set<TaxonName> getReplacedSynonyms() {
106
        Set<TaxonName> replacedSynonyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM());
107
        return replacedSynonyms;
108
    }
109

    
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();
117
            }
118
        }
119
        return nameRelDto;
120
    }
121

    
122
    public void setValidationFor(NameRelationshipDTO nameRelDto) {
123
        setUniqeNameRelationDTO(nameRelDto, NameRelationshipType.VALIDATED_BY_NAME(), Direction.relatedTo, persistedValidatedName);
124
    }
125

    
126

    
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();
134
            }
135
        }
136
        return nameRelDto;
137
    }
138

    
139
    public void setOrthographicVariant(NameRelationshipDTO nameRelDto) {
140
        setUniqeNameRelationDTO(nameRelDto, NameRelationshipType.ORTHOGRAPHIC_VARIANT(), Direction.relatedTo, persistedOrthographicVariant);
141
    }
142

    
143
    /**
144
     * @param nameRelDto
145
     * @param nameRelationshipType
146
     * @param direction
147
     * @param persistedRelatedName
148
     */
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
153
            nameRelDto = null;
154
        }
155

    
156
        NameRelationship relationship = uniqueNameRelationship(nameRelationshipType, direction);
157

    
158
        if(nameRelDto != null){
159
            // add or update ...
160
            boolean currentNameIsTarget = false;
161
            if(relationship != null && persistedRelatedName != null){
162
                if(direction == Direction.relatedTo){
163
                    relationship.getFromName().equals(persistedRelatedName);
164
                } else {
165
                    relationship.getToName().equals(persistedRelatedName);
166
                }
167
            }
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());
174
            } else {
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());
180
                } else {
181
                    name.addRelationshipToName(nameRelDto.getOtherName(), nameRelationshipType,
182
                            nameRelDto.getCitation(), nameRelDto.getCitationMicroReference(), nameRelDto.getRuleConsidered(), nameRelDto.getCodeEdition());
183
                }
184
                if(persistedRelatedName != null){
185
                    name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
186
                }
187
            }
188
        } else {
189
            // remove ...
190
            if(persistedRelatedName != null && relationship != null){
191
                name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
192
            }
193
        }
194
    }
195

    
196
    public void setBasionyms(Set<TaxonName> basionyms) {
197
        setRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM(), basionyms);
198
    }
199

    
200
    public void setReplacedSynonyms(Set<TaxonName> replacedSynonyms) {
201
        setRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM(), replacedSynonyms);
202
    }
203

    
204
    /**
205
     * @return
206
     */
207
    protected NameRelationship uniqueNameRelationship(NameRelationshipType relationShipType, Direction direction) {
208

    
209
        Set<NameRelationship> relations;
210

    
211
        if(direction == Direction.relatedTo){
212
            relations = name.getRelationsToThisName();
213
        } else {
214
            relations = name.getRelationsFromThisName();
215
        }
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) {
223
            return null;
224
        }
225
        return nameRelations.iterator().next();
226
    }
227

    
228
    /**
229
     * @param basionyms
230
     * @param relType
231
     * @param direction
232
     */
233
    protected void setRelatedNames(Direction direction, NameRelationshipType relType, Set<TaxonName> relatedNames) {
234
        Set<TaxonName> currentRelatedNames = new HashSet<>();
235
        Set<TaxonName> namesSeen = new HashSet<>();
236

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

    
260
    public TeamOrPersonBase<?> getCombinationAuthorship() {
261
        return name.getCombinationAuthorship();
262
    }
263

    
264
    public List<Credit> getCredits() {
265
        return name.getCredits();
266
    }
267

    
268
    public String getCultivarName() {
269
        return name.getCultivarName();
270
    }
271

    
272
    public TeamOrPersonBase<?> getExBasionymAuthorship() {
273
        return name.getExBasionymAuthorship();
274
    }
275

    
276
    public TeamOrPersonBase<?> getExCombinationAuthorship() {
277
        return name.getExCombinationAuthorship();
278
    }
279

    
280
    public Set<Extension> getExtensions() {
281
        return name.getExtensions();
282
    }
283

    
284
    public String getFullTitleCache() {
285
        return name.getFullTitleCache();
286
    }
287

    
288
    public String getGenusOrUninomial() {
289
        return name.getGenusOrUninomial();
290
    }
291

    
292
    public HomotypicalGroup getHomotypicalGroup() {
293
        return name.getHomotypicalGroup();
294
    }
295

    
296
    public List<Identifier> getIdentifiers() {
297
        return name.getIdentifiers();
298
    }
299

    
300
    public String getInfraGenericEpithet() {
301
        return name.getInfraGenericEpithet();
302
    }
303

    
304
    public String getInfraSpecificEpithet() {
305
        return name.getInfraSpecificEpithet();
306
    }
307

    
308
    public String getSpecificEpithet() {
309
        return name.getSpecificEpithet();
310
    }
311

    
312
    public String getNameCache() {
313
        return name.getNameCache();
314
    }
315

    
316
    public String getNomenclaturalMicroReference() {
317
        return name.getNomenclaturalMicroReference();
318
    }
319

    
320
    public INomenclaturalReference getNomenclaturalReference() {
321
        return name.getNomenclaturalReference();
322
    }
323

    
324
    public Rank getRank() {
325
        return name.getRank();
326
    }
327

    
328
    public Set<NomenclaturalStatus> getStatus() {
329
        return name.getStatus();
330
    }
331

    
332
    public void setStatus(Set<NomenclaturalStatus> status) throws SetterAdapterException {
333
        name.setStatus(status);
334
    }
335

    
336
    public boolean isProtectedAuthorshipCache() {
337
        return name.isProtectedAuthorshipCache();
338
    }
339

    
340
    public boolean isProtectedFullTitleCache() {
341
        return name.isProtectedFullTitleCache();
342
    }
343

    
344
    public boolean isProtectedNameCache() {
345
        return name.isProtectedNameCache();
346
    }
347

    
348
    public boolean isProtectedTitleCache() {
349
        return name.isProtectedTitleCache();
350
    }
351

    
352
    public void setAcronym(String acronym) {
353
        name.setAcronym(acronym);
354
    }
355

    
356
    public void setAppendedPhrase(String appendedPhrase) {
357
        name.setAppendedPhrase(appendedPhrase);
358
    }
359

    
360
    public void setBasionymAuthorship(TeamOrPersonBase<?> basionymAuthorship) {
361
        name.setBasionymAuthorship(basionymAuthorship);
362
    }
363

    
364
    public void setBinomHybrid(boolean binomHybrid) {
365
        name.setBinomHybrid(binomHybrid);
366
    }
367

    
368
    public void setBreed(String breed) {
369
        name.setBreed(breed);
370
    }
371

    
372
    public void setCombinationAuthorship(TeamOrPersonBase<?> combinationAuthorship) {
373
        name.setCombinationAuthorship(combinationAuthorship);
374
    }
375

    
376
    public void setCultivarName(String cultivarName) {
377
        name.setCultivarName(cultivarName);
378
    }
379

    
380
    public void setExBasionymAuthorship(TeamOrPersonBase<?> exBasionymAuthorship) {
381
        name.setExBasionymAuthorship(exBasionymAuthorship);
382
    }
383

    
384
    public void setExCombinationAuthorship(TeamOrPersonBase<?> exCombinationAuthorship) {
385
        name.setExCombinationAuthorship(exCombinationAuthorship);
386
    }
387

    
388
    public void setFullTitleCache(String fullTitleCache) {
389
        name.setFullTitleCache(fullTitleCache);
390
    }
391

    
392
    public void setGenusOrUninomial(String genusOrUninomial) {
393
        name.setGenusOrUninomial(genusOrUninomial);
394
    }
395

    
396
    public void setHybridFormula(boolean hybridFormula) {
397
        name.setHybridFormula(hybridFormula);
398
    }
399

    
400
    public void setInfraGenericEpithet(String infraGenericEpithet) {
401
        name.setInfraGenericEpithet(infraGenericEpithet);
402
    }
403

    
404
    public void setInfraSpecificEpithet(String infraSpecificEpithet) {
405
        name.setInfraSpecificEpithet(infraSpecificEpithet);
406
    }
407

    
408
    public void setMonomHybrid(boolean monomHybrid) {
409
        name.setMonomHybrid(monomHybrid);
410
    }
411

    
412
    public void setNameApprobation(String nameApprobation) {
413
        name.setNameApprobation(nameApprobation);
414
    }
415

    
416
    public void setNameCache(String nameCache) {
417
        name.setNameCache(nameCache);
418
    }
419

    
420
    public void setNameType(NomenclaturalCode nameType) {
421
        name.setNameType(nameType);
422
    }
423

    
424
    public void setNomenclaturalMicroReference(String nomenclaturalMicroReference) {
425
        name.setNomenclaturalMicroReference(nomenclaturalMicroReference);
426
    }
427

    
428
    public void setNomenclaturalReference(INomenclaturalReference nomenclaturalReference) {
429
        name.setNomenclaturalReference(nomenclaturalReference);
430
    }
431

    
432
    public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache) {
433
        name.setProtectedAuthorshipCache(protectedAuthorshipCache);
434
    }
435

    
436
    public void setProtectedFullTitleCache(boolean protectedFullTitleCache) {
437
        name.setProtectedFullTitleCache(protectedFullTitleCache);
438
    }
439

    
440
    public void setProtectedNameCache(boolean protectedNameCache) {
441
        name.setProtectedNameCache(protectedNameCache);
442
    }
443

    
444
    public void setProtectedTitleCache(boolean protectedTitleCache) {
445
        name.setProtectedTitleCache(protectedTitleCache);
446
    }
447

    
448
    public void setRank(Rank rank) {
449
        name.setRank(rank);
450
    }
451

    
452
    public void setSpecificEpithet(String specificEpithet) {
453
        name.setSpecificEpithet(specificEpithet);
454
    }
455

    
456
    public void setTitleCache(String titleCache) {
457
        name.setTitleCache(titleCache);
458
    }
459

    
460
    public void setTrinomHybrid(boolean trinomHybrid) {
461
        name.setTrinomHybrid(trinomHybrid);
462
    }
463

    
464
    public void setUpdated(DateTime updated) {
465
        name.setUpdated(updated);
466
    }
467

    
468
    public void setUpdatedBy(User updatedBy) {
469
        name.setUpdatedBy(updatedBy);
470
    }
471

    
472
}
(2-2/2)