adapt Vaadin to model changes v5.8.1
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / model / name / TaxonNameDTO.java
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 } else {
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());
179 } else {
180 name.addRelationshipToName(nameRelDto.getOtherName(), nameRelationshipType,
181 nameRelDto.getCitation(), nameRelDto.getCitationMicroReference(), nameRelDto.getRuleConsidered());
182 }
183 if(persistedRelatedName != null){
184 name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
185 }
186 }
187 } else {
188 // remove ...
189 if(persistedRelatedName != null && relationship != null){
190 name.removeRelationWithTaxonName(persistedRelatedName, direction, nameRelationshipType);
191 }
192 }
193 }
194
195 public void setBasionyms(Set<TaxonName> basionyms) {
196 setRelatedNames(Direction.relatedTo, NameRelationshipType.BASIONYM(), basionyms);
197 }
198
199 public void setReplacedSynonyms(Set<TaxonName> replacedSynonyms) {
200 setRelatedNames(Direction.relatedTo, NameRelationshipType.REPLACED_SYNONYM(), replacedSynonyms);
201 }
202
203 /**
204 * @return
205 */
206 protected NameRelationship uniqueNameRelationship(NameRelationshipType relationShipType, Direction direction) {
207
208 Set<NameRelationship> relations;
209
210 if(direction == Direction.relatedTo){
211 relations = name.getRelationsToThisName();
212 } else {
213 relations = name.getRelationsFromThisName();
214 }
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) {
222 return null;
223 }
224 return nameRelations.iterator().next();
225 }
226
227 /**
228 * @param basionyms
229 * @param relType
230 * @param direction
231 */
232 protected void setRelatedNames(Direction direction, NameRelationshipType relType, Set<TaxonName> relatedNames) {
233 Set<TaxonName> currentRelatedNames = new HashSet<>();
234 Set<TaxonName> namesSeen = new HashSet<>();
235
236 for(TaxonName tn : name.getRelatedNames(direction, relType)){
237 currentRelatedNames.add(tn);
238 }
239 for(TaxonName tn : relatedNames){
240 if(tn == null){
241 continue;
242 }
243 if(!currentRelatedNames.contains(tn)){
244 if(direction.equals(Direction.relatedTo)){
245 tn.addRelationshipToName(name, relType, null);
246 } else {
247 tn.addRelationshipFromName(name, relType, null);
248 }
249 }
250 namesSeen.add(tn);
251 }
252 for(TaxonName tn : currentRelatedNames){
253 if(!namesSeen.contains(tn)){
254 name.removeRelationWithTaxonName(tn, direction, relType);
255 }
256 }
257 }
258
259 public TeamOrPersonBase<?> getCombinationAuthorship() {
260 return name.getCombinationAuthorship();
261 }
262
263 public List<Credit> getCredits() {
264 return name.getCredits();
265 }
266
267 public String getCultivarName() {
268 return name.getCultivarName();
269 }
270
271 public TeamOrPersonBase<?> getExBasionymAuthorship() {
272 return name.getExBasionymAuthorship();
273 }
274
275 public TeamOrPersonBase<?> getExCombinationAuthorship() {
276 return name.getExCombinationAuthorship();
277 }
278
279 public Set<Extension> getExtensions() {
280 return name.getExtensions();
281 }
282
283 public String getFullTitleCache() {
284 return name.getFullTitleCache();
285 }
286
287 public String getGenusOrUninomial() {
288 return name.getGenusOrUninomial();
289 }
290
291 public HomotypicalGroup getHomotypicalGroup() {
292 return name.getHomotypicalGroup();
293 }
294
295 public List<Identifier> getIdentifiers() {
296 return name.getIdentifiers();
297 }
298
299 public String getInfraGenericEpithet() {
300 return name.getInfraGenericEpithet();
301 }
302
303 public String getInfraSpecificEpithet() {
304 return name.getInfraSpecificEpithet();
305 }
306
307 public String getSpecificEpithet() {
308 return name.getSpecificEpithet();
309 }
310
311 public String getNameCache() {
312 return name.getNameCache();
313 }
314
315 public String getNomenclaturalMicroReference() {
316 return name.getNomenclaturalMicroReference();
317 }
318
319 public INomenclaturalReference getNomenclaturalReference() {
320 return name.getNomenclaturalReference();
321 }
322
323 public Rank getRank() {
324 return name.getRank();
325 }
326
327 public Set<NomenclaturalStatus> getStatus() {
328 return name.getStatus();
329 }
330
331 public void setStatus(Set<NomenclaturalStatus> status) throws SetterAdapterException {
332 name.setStatus(status);
333 }
334
335 public boolean isProtectedAuthorshipCache() {
336 return name.isProtectedAuthorshipCache();
337 }
338
339 public boolean isProtectedFullTitleCache() {
340 return name.isProtectedFullTitleCache();
341 }
342
343 public boolean isProtectedNameCache() {
344 return name.isProtectedNameCache();
345 }
346
347 public boolean isProtectedTitleCache() {
348 return name.isProtectedTitleCache();
349 }
350
351 public void setAcronym(String acronym) {
352 name.setAcronym(acronym);
353 }
354
355 public void setAppendedPhrase(String appendedPhrase) {
356 name.setAppendedPhrase(appendedPhrase);
357 }
358
359 public void setBasionymAuthorship(TeamOrPersonBase<?> basionymAuthorship) {
360 name.setBasionymAuthorship(basionymAuthorship);
361 }
362
363 public void setBinomHybrid(boolean binomHybrid) {
364 name.setBinomHybrid(binomHybrid);
365 }
366
367 public void setBreed(String breed) {
368 name.setBreed(breed);
369 }
370
371 public void setCombinationAuthorship(TeamOrPersonBase<?> combinationAuthorship) {
372 name.setCombinationAuthorship(combinationAuthorship);
373 }
374
375 public void setCultivarName(String cultivarName) {
376 name.setCultivarName(cultivarName);
377 }
378
379 public void setExBasionymAuthorship(TeamOrPersonBase<?> exBasionymAuthorship) {
380 name.setExBasionymAuthorship(exBasionymAuthorship);
381 }
382
383 public void setExCombinationAuthorship(TeamOrPersonBase<?> exCombinationAuthorship) {
384 name.setExCombinationAuthorship(exCombinationAuthorship);
385 }
386
387 public void setFullTitleCache(String fullTitleCache) {
388 name.setFullTitleCache(fullTitleCache);
389 }
390
391 public void setGenusOrUninomial(String genusOrUninomial) {
392 name.setGenusOrUninomial(genusOrUninomial);
393 }
394
395 public void setHybridFormula(boolean hybridFormula) {
396 name.setHybridFormula(hybridFormula);
397 }
398
399 public void setInfraGenericEpithet(String infraGenericEpithet) {
400 name.setInfraGenericEpithet(infraGenericEpithet);
401 }
402
403 public void setInfraSpecificEpithet(String infraSpecificEpithet) {
404 name.setInfraSpecificEpithet(infraSpecificEpithet);
405 }
406
407 public void setMonomHybrid(boolean monomHybrid) {
408 name.setMonomHybrid(monomHybrid);
409 }
410
411 public void setNameApprobation(String nameApprobation) {
412 name.setNameApprobation(nameApprobation);
413 }
414
415 public void setNameCache(String nameCache) {
416 name.setNameCache(nameCache);
417 }
418
419 public void setNameType(NomenclaturalCode nameType) {
420 name.setNameType(nameType);
421 }
422
423 public void setNomenclaturalMicroReference(String nomenclaturalMicroReference) {
424 name.setNomenclaturalMicroReference(nomenclaturalMicroReference);
425 }
426
427 public void setNomenclaturalReference(INomenclaturalReference nomenclaturalReference) {
428 name.setNomenclaturalReference(nomenclaturalReference);
429 }
430
431 public void setProtectedAuthorshipCache(boolean protectedAuthorshipCache) {
432 name.setProtectedAuthorshipCache(protectedAuthorshipCache);
433 }
434
435 public void setProtectedFullTitleCache(boolean protectedFullTitleCache) {
436 name.setProtectedFullTitleCache(protectedFullTitleCache);
437 }
438
439 public void setProtectedNameCache(boolean protectedNameCache) {
440 name.setProtectedNameCache(protectedNameCache);
441 }
442
443 public void setProtectedTitleCache(boolean protectedTitleCache) {
444 name.setProtectedTitleCache(protectedTitleCache);
445 }
446
447 public void setRank(Rank rank) {
448 name.setRank(rank);
449 }
450
451 public void setSpecificEpithet(String specificEpithet) {
452 name.setSpecificEpithet(specificEpithet);
453 }
454
455 public void setTitleCache(String titleCache) {
456 name.setTitleCache(titleCache);
457 }
458
459 public void setTrinomHybrid(boolean trinomHybrid) {
460 name.setTrinomHybrid(trinomHybrid);
461 }
462
463 public void setUpdated(DateTime updated) {
464 name.setUpdated(updated);
465 }
466
467 public void setUpdatedBy(User updatedBy) {
468 name.setUpdatedBy(updatedBy);
469 }
470
471 }