Project

General

Profile

Download (13 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.HashSet;
12
import java.util.List;
13
import java.util.Set;
14
import java.util.stream.Collectors;
15

    
16
import org.joda.time.DateTime;
17

    
18
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
19
import eu.etaxonomy.cdm.model.common.Annotation;
20
import eu.etaxonomy.cdm.model.common.Credit;
21
import eu.etaxonomy.cdm.model.common.Extension;
22
import eu.etaxonomy.cdm.model.common.Identifier;
23
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
24
import eu.etaxonomy.cdm.model.common.User;
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.NomenclaturalStatus;
30
import eu.etaxonomy.cdm.model.name.Rank;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
33
import eu.etaxonomy.cdm.vaadin.model.CdmEntityDecoraterDTO;
34

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

    
42
    private static final long serialVersionUID = -8018109905949198530L;
43

    
44
    private TaxonName name;
45

    
46
    private Set<TaxonName> persistedBasionyms;
47

    
48
    private Set<TaxonName> persistedReplacedSynonyms;
49

    
50
    private TaxonName persistedValidatedName;
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 String getAppendedPhrase() {
69
        return name.getAppendedPhrase();
70
    }
71

    
72
    public String getAuthorshipCache() {
73

    
74
        return name.getAuthorshipCache();
75
    }
76

    
77
    public TeamOrPersonBase<?> getBasionymAuthorship() {
78
        return name.getBasionymAuthorship();
79
    }
80

    
81
    public Set<TaxonName> getBasionyms() {
82
        Set<TaxonName> basionyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM());
83
        if(persistedBasionyms == null){
84
            // remember the persisted state before starting to operate on the DTO
85
            persistedBasionyms = basionyms;
86
        }
87
        return basionyms;
88
    }
89

    
90
    public Set<TaxonName> getReplacedSynonyms() {
91
        Set<TaxonName> replacedSynonyms = name.getRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM());
92
        if(persistedReplacedSynonyms == null){
93
            // remember the persisted state before starting to operate on the DTO
94
            persistedReplacedSynonyms = replacedSynonyms;
95
        }
96
        return replacedSynonyms;
97
    }
98

    
99
    public NameRelationshipDTO getValidationFor() {
100
        NameRelationshipDTO nameRelDto  = null;
101
        NameRelationship validatingRelationship = validatingRelationship();
102
        if(validatingRelationship != null){
103
            nameRelDto = new NameRelationshipDTO(Direction.relatedTo, validatingRelationship);
104
            if(persistedValidatedName == null){
105
               persistedValidatedName = nameRelDto.getOtherName();
106
            }
107
        }
108
        return nameRelDto;
109
    }
110

    
111
    /**
112
     * @return
113
     */
114
    protected NameRelationship validatingRelationship() {
115
        Set<NameRelationship> toRelations = name.getRelationsToThisName();
116
        Set<NameRelationship> validatedNameRelations = toRelations.stream().filter(
117
                    nr -> nr.getType().equals(NameRelationshipType.VALIDATED_BY_NAME())
118
                ).collect(Collectors.toSet());
119
        if(validatedNameRelations.size() > 1){
120
            // TODO use non RuntimeException
121
            throw new RuntimeException("More than one validated name found.");
122
        } else if(validatedNameRelations.size() == 0) {
123
            return null;
124
        }
125
        return validatedNameRelations.iterator().next();
126
    }
127

    
128
    public void setValidationFor(NameRelationshipDTO nameRelDto) {
129

    
130
        if(nameRelDto != null && nameRelDto.getOtherName() == null){
131
            // treat as if there is no validation
132
            nameRelDto = null;
133
        }
134

    
135
        NameRelationship validatingRelationship = validatingRelationship();
136

    
137
        if(nameRelDto != null){
138
            // add or update ...
139
            if(validatingRelationship != null && persistedValidatedName != null && validatingRelationship.getFromName().equals(persistedValidatedName)){
140
                // validated name has not changed, so we can update the relation
141
                validatingRelationship.setCitation(nameRelDto.getCitation());
142
                validatingRelationship.setCitationMicroReference(nameRelDto.getCitationMicroReference());
143
                validatingRelationship.setRuleConsidered(nameRelDto.getRuleConsidered());
144
            } else {
145
                // need to remove the old relationship and to create a new one.
146
                // the actual removal will take place ....
147
                name.addRelationshipFromName(nameRelDto.getOtherName(), NameRelationshipType.VALIDATED_BY_NAME(),
148
                        nameRelDto.getCitation(), nameRelDto.getCitationMicroReference(), nameRelDto.getRuleConsidered());
149
                if(persistedValidatedName != null){
150
                    name.removeRelationWithTaxonName(persistedValidatedName, Direction.relatedTo, NameRelationshipType.VALIDATED_BY_NAME());
151
                }
152
            }
153
        } else {
154
            // remove ...
155
            if(persistedValidatedName != null && validatingRelationship != null){
156
                name.removeRelationWithTaxonName(persistedValidatedName, Direction.relatedTo, NameRelationshipType.VALIDATED_BY_NAME());
157
            }
158
        }
159
    }
160

    
161
    public void setBasionyms(Set<TaxonName> basionyms) {
162
        setRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM(), basionyms);
163
    }
164

    
165
    public void setReplacedSynonyms(Set<TaxonName> replacedSynonyms) {
166
        setRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM(), replacedSynonyms);
167
    }
168

    
169
    /**
170
     * @param basionyms
171
     * @param relType
172
     * @param direction
173
     */
174
    protected void setRelatedNames(Direction direction, NameRelationshipType relType, Set<TaxonName> relatedNames) {
175
        Set<TaxonName> currentRelatedNames = new HashSet<>();
176
        Set<TaxonName> namesSeen = new HashSet<>();
177

    
178
        for(TaxonName tn : name.getRelatedNames(direction, relType)){
179
            currentRelatedNames.add(tn);
180
        }
181
        for(TaxonName tn : relatedNames){
182
            if(tn == null){
183
                continue;
184
            }
185
            if(!currentRelatedNames.contains(tn)){
186
                if(direction.equals(Direction.relatedTo)){
187
                    tn.addRelationshipToName(name, relType, null);
188
                } else {
189
                    tn.addRelationshipFromName(name, relType, null);
190
                }
191
            }
192
            namesSeen.add(tn);
193
        }
194
        for(TaxonName tn : currentRelatedNames){
195
            if(!namesSeen.contains(tn)){
196
                name.removeRelationWithTaxonName(tn, direction, relType);
197
            }
198
        }
199
    }
200

    
201
    public Set<TaxonName> persistedBasionyms(){
202
        return persistedBasionyms;
203
    }
204

    
205
    public TeamOrPersonBase<?> getCombinationAuthorship() {
206
        return name.getCombinationAuthorship();
207
    }
208

    
209
    public List<Credit> getCredits() {
210
        return name.getCredits();
211
    }
212

    
213
    public String getCultivarName() {
214
        return name.getCultivarName();
215
    }
216

    
217
    public TeamOrPersonBase<?> getExBasionymAuthorship() {
218
        return name.getExBasionymAuthorship();
219
    }
220

    
221
    public TeamOrPersonBase<?> getExCombinationAuthorship() {
222
        return name.getExCombinationAuthorship();
223
    }
224

    
225
    public Set<Extension> getExtensions() {
226
        return name.getExtensions();
227
    }
228

    
229
    public String getFullTitleCache() {
230
        return name.getFullTitleCache();
231
    }
232

    
233
    public String getGenusOrUninomial() {
234
        return name.getGenusOrUninomial();
235
    }
236

    
237
    public HomotypicalGroup getHomotypicalGroup() {
238
        return name.getHomotypicalGroup();
239
    }
240

    
241
    public List<Identifier> getIdentifiers() {
242
        return name.getIdentifiers();
243
    }
244

    
245
    public String getInfraGenericEpithet() {
246
        return name.getInfraGenericEpithet();
247
    }
248

    
249
    public String getInfraSpecificEpithet() {
250
        return name.getInfraSpecificEpithet();
251
    }
252

    
253
    public String getSpecificEpithet() {
254
        return name.getSpecificEpithet();
255
    }
256

    
257
    public String getNameCache() {
258
        return name.getNameCache();
259
    }
260

    
261
    public String getNomenclaturalMicroReference() {
262
        return name.getNomenclaturalMicroReference();
263
    }
264

    
265
    public INomenclaturalReference getNomenclaturalReference() {
266
        return name.getNomenclaturalReference();
267
    }
268

    
269
    public Rank getRank() {
270
        return name.getRank();
271
    }
272

    
273
    public Set<NomenclaturalStatus> getStatus() {
274
        return name.getStatus();
275
    }
276

    
277
    public boolean isProtectedAuthorshipCache() {
278
        return name.isProtectedAuthorshipCache();
279
    }
280

    
281
    public boolean isProtectedFullTitleCache() {
282
        return name.isProtectedFullTitleCache();
283
    }
284

    
285
    public boolean isProtectedNameCache() {
286
        return name.isProtectedNameCache();
287
    }
288

    
289
    public boolean isProtectedTitleCache() {
290
        return name.isProtectedTitleCache();
291
    }
292

    
293
    public void setAcronym(String acronym) {
294
        name.setAcronym(acronym);
295
    }
296

    
297
    public void setAppendedPhrase(String appendedPhrase) {
298
        name.setAppendedPhrase(appendedPhrase);
299
    }
300

    
301
    public void setBasionymAuthorship(TeamOrPersonBase<?> basionymAuthorship) {
302
        name.setBasionymAuthorship(basionymAuthorship);
303
    }
304

    
305
    public void setBinomHybrid(boolean binomHybrid) {
306
        name.setBinomHybrid(binomHybrid);
307
    }
308

    
309
    public void setBreed(String breed) {
310
        name.setBreed(breed);
311
    }
312

    
313
    public void setCombinationAuthorship(TeamOrPersonBase<?> combinationAuthorship) {
314
        name.setCombinationAuthorship(combinationAuthorship);
315
    }
316

    
317
    public void setCultivarName(String cultivarName) {
318
        name.setCultivarName(cultivarName);
319
    }
320

    
321
    public void setExBasionymAuthorship(TeamOrPersonBase<?> exBasionymAuthorship) {
322
        name.setExBasionymAuthorship(exBasionymAuthorship);
323
    }
324

    
325
    public void setExCombinationAuthorship(TeamOrPersonBase<?> exCombinationAuthorship) {
326
        name.setExCombinationAuthorship(exCombinationAuthorship);
327
    }
328

    
329
    public void setFullTitleCache(String fullTitleCache) {
330
        name.setFullTitleCache(fullTitleCache);
331
    }
332

    
333
    public void setGenusOrUninomial(String genusOrUninomial) {
334
        name.setGenusOrUninomial(genusOrUninomial);
335
    }
336

    
337
    public void setHybridFormula(boolean hybridFormula) {
338
        name.setHybridFormula(hybridFormula);
339
    }
340

    
341
    public void setInfraGenericEpithet(String infraGenericEpithet) {
342
        name.setInfraGenericEpithet(infraGenericEpithet);
343
    }
344

    
345
    public void setInfraSpecificEpithet(String infraSpecificEpithet) {
346
        name.setInfraSpecificEpithet(infraSpecificEpithet);
347
    }
348

    
349
    public void setMonomHybrid(boolean monomHybrid) {
350
        name.setMonomHybrid(monomHybrid);
351
    }
352

    
353
    public void setNameApprobation(String nameApprobation) {
354
        name.setNameApprobation(nameApprobation);
355
    }
356

    
357
    public void setNameCache(String nameCache) {
358
        name.setNameCache(nameCache);
359
    }
360

    
361
    public void setNameType(NomenclaturalCode nameType) {
362
        name.setNameType(nameType);
363
    }
364

    
365
    public void setNomenclaturalMicroReference(String nomenclaturalMicroReference) {
366
        name.setNomenclaturalMicroReference(nomenclaturalMicroReference);
367
    }
368

    
369
    public void setNomenclaturalReference(INomenclaturalReference nomenclaturalReference) {
370
        name.setNomenclaturalReference(nomenclaturalReference);
371
    }
372

    
373
    public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache) {
374
        name.setProtectedAuthorshipCache(protectedAuthorshipCache);
375
    }
376

    
377
    public void setProtectedFullTitleCache(boolean protectedFullTitleCache) {
378
        name.setProtectedFullTitleCache(protectedFullTitleCache);
379
    }
380

    
381
    public void setProtectedNameCache(boolean protectedNameCache) {
382
        name.setProtectedNameCache(protectedNameCache);
383
    }
384

    
385
    public void setProtectedTitleCache(boolean protectedTitleCache) {
386
        name.setProtectedTitleCache(protectedTitleCache);
387
    }
388

    
389
    public void setRank(Rank rank) {
390
        name.setRank(rank);
391
    }
392

    
393
    public void setSpecificEpithet(String specificEpithet) {
394
        name.setSpecificEpithet(specificEpithet);
395
    }
396

    
397
    public void setTitleCache(String titleCache) {
398
        name.setTitleCache(titleCache);
399
    }
400

    
401
    public void setTrinomHybrid(boolean trinomHybrid) {
402
        name.setTrinomHybrid(trinomHybrid);
403
    }
404

    
405
    public void setUpdated(DateTime updated) {
406
        name.setUpdated(updated);
407
    }
408

    
409
    public void setUpdatedBy(User updatedBy) {
410
        name.setUpdatedBy(updatedBy);
411
    }
412

    
413
}
(2-2/2)