2 * Copyright (C) 2021 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
.format
;
11 import java
.lang
.reflect
.InvocationTargetException
;
12 import java
.lang
.reflect
.Method
;
15 import org
.apache
.commons
.lang3
.StringUtils
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.format
.occurrences
.DistanceStringFormatter
;
19 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
25 import eu
.etaxonomy
.cdm
.model
.common
.LanguageStringBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
27 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
28 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
31 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
32 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
35 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
36 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
37 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
38 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
39 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
40 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonInteraction
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
43 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
44 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
45 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
46 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
47 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
48 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
49 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalSource
;
50 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatus
;
51 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
52 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
53 import eu
.etaxonomy
.cdm
.model
.name
.TextualTypeDesignation
;
54 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
55 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
56 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
57 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
58 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
59 import eu
.etaxonomy
.cdm
.model
.permission
.Group
;
60 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
61 import eu
.etaxonomy
.cdm
.model
.reference
.NamedSource
;
62 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceBase
;
63 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
64 import eu
.etaxonomy
.cdm
.model
.taxon
.SecundumSource
;
65 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
66 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
67 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
68 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
69 import eu
.etaxonomy
.cdm
.model
.term
.Representation
;
75 public class ReferencingObjectFormatter
{
78 public static String
format(CdmBase element
, Language defaultLanguage
) {
79 return format(element
, null, defaultLanguage
);
83 public static String
format(CdmBase element
, String target
, Language defaultLanguage
) {
85 String resultString
= null;
88 }else if (element
instanceof IdentifiableEntity
) {
89 resultString
= ((IdentifiableEntity
<?
>) element
).getTitleCache();
90 }else if (element
instanceof OriginalSourceBase
) {
91 OriginalSourceBase originalSource
= (OriginalSourceBase
) element
;
92 // ISourceable sourcedObject = originalSource.getSourcedObj();
93 //due to #5743 the bidirectionality for sourced object had to be removed
95 String sourceObjectTitle
= "sourced object data not available (#5743)";
97 //it is now possible for NomenclaturalSource as they link to the sourced name
98 if (originalSource
instanceof NomenclaturalSource
){
99 TaxonName sourcedName
= ((NomenclaturalSource
)originalSource
).getSourcedName();
100 sourceObjectTitle
= sourcedName
== null ?
"Source orphaned, not attached to a name" :
101 "for " + sourcedName
.getTitleCache();
102 }else if (originalSource
instanceof SecundumSource
){
103 TaxonBase
<?
> sourcedTaxon
= ((SecundumSource
)originalSource
).getSourcedTaxon();
104 sourceObjectTitle
= sourcedTaxon
== null ?
"Source orphaned, not attached to a taxon" :
105 "for " + sourcedTaxon
.getTitleCache();
106 }else if (originalSource
instanceof DescriptionElementSource
){
107 sourceObjectTitle
= getCache((DescriptionElementSource
)originalSource
, defaultLanguage
);
108 }else if (originalSource
instanceof IdentifiableSource
&& isNotBlank(target
) ){
109 sourceObjectTitle
= "for " + target
;
110 }else if (originalSource
instanceof NamedSource
&& isNotBlank(target
) ){
111 sourceObjectTitle
= "for " + target
;
113 resultString
= CdmUtils
.concat("; ", new String
[]{originalSource
.getIdNamespace(), originalSource
.getIdInSource(), sourceObjectTitle
});
114 }else if (element
instanceof LanguageStringBase
) {
115 resultString
= ((LanguageStringBase
) element
).getText();
116 }else if (element
instanceof DescriptionElementBase
) {
117 resultString
= getCache((DescriptionElementBase
) element
, defaultLanguage
);
118 }else if (element
instanceof RelationshipBase
<?
, ?
, ?
>) {
119 resultString
= getCache((RelationshipBase
<?
, ?
, ?
>) element
, defaultLanguage
);
120 }else if (element
instanceof TypeDesignationBase
<?
>) {
121 resultString
= getCache((TypeDesignationBase
<?
>) element
, defaultLanguage
);
122 }else if (element
instanceof HomotypicalGroup
) {
123 resultString
= getCache((HomotypicalGroup
) element
);
124 }else if (element
instanceof TaxonNode
) {
125 resultString
= getCache((TaxonNode
) element
);
126 }else if (element
instanceof DeterminationEvent
) {
127 resultString
= getCache((DeterminationEvent
) element
);
128 }else if (element
instanceof NomenclaturalStatus
) {
129 resultString
= getCache((NomenclaturalStatus
) element
);
130 }else if (element
instanceof GatheringEvent
){
131 resultString
= getCache((GatheringEvent
) element
);
132 }else if (element
instanceof Marker
) {
133 Marker marker
= (Marker
) element
;
134 MarkerType type
= marker
.getMarkerType();
135 resultString
= (type
== null ?
"- no marker type -" : marker
.getMarkerType().getLabel()) + " (" + marker
.getFlag() + ")";
136 }else if (element
instanceof User
) {
137 User user
= (User
) element
;
138 resultString
= user
.getUsername();
139 }else if (element
instanceof Group
) {
140 Group group
= (Group
) element
;
141 resultString
= group
.getName();
142 }else if (element
instanceof KeyStatement
) {
143 KeyStatement keyStatement
= (KeyStatement
) element
;
144 resultString
= getCache(keyStatement
);
146 // TODO write return texts for HomotypicalGroup, etc.
147 resultString
= element
.toString();
150 if (resultString
== null){
151 resultString
= element
.toString();
156 private static String
getCache(DescriptionElementSource source
, Language defaultLanguage
) {
157 DescriptionElementBase sourcedElement
= source
.getSourcedElement();
159 if (sourcedElement
== null){
160 return "Source orphaned, not attached to a fact";
162 String superLabel
= getDescribedObjectLabel(sourcedElement
.getInDescription());
163 String result
= CdmUtils
.concat(": ", DescriptionElementFormatter
.format(sourcedElement
, defaultLanguage
), superLabel
);
167 private static String
getDescribedObjectLabel(DescriptionBase
<?
> inDescription
) {
168 IDescribable
<?
> entity
= inDescription
.describedEntity();
170 return entity
.getTitleCache();
172 return inDescription
.getTitleCache();
176 private static String
getCache(RelationshipBase
<?
, ?
, ?
> rel
, Language defaultLanguage
) {
177 rel
= CdmBase
.deproxy(rel
);
178 RelationshipTermBase
<?
> type
= rel
.getType();
179 IdentifiableEntity
<?
> from
;
180 IdentifiableEntity
<?
> to
;
181 if (rel
.isInstanceOf(NameRelationship
.class)){
182 from
= ((NameRelationship
)rel
).getFromName();
183 to
= ((NameRelationship
)rel
).getToName();
184 }else if (rel
.isInstanceOf(HybridRelationship
.class)){
185 from
= ((HybridRelationship
)rel
).getParentName();
186 to
= ((HybridRelationship
)rel
).getHybridName();
187 }else if (rel
.isInstanceOf(TaxonRelationship
.class)){
188 from
= ((TaxonRelationship
)rel
).getFromTaxon();
189 to
= ((TaxonRelationship
)rel
).getToTaxon();
192 Method fromMethod
= rel
.getClass().getMethod("getRelatedFrom");
193 Method toMethod
= rel
.getClass().getMethod("getRelatedFrom");
194 fromMethod
.setAccessible(true);
195 toMethod
.setAccessible(true);
196 from
= (IdentifiableEntity
<?
>)fromMethod
.invoke(rel
);
197 to
= (IdentifiableEntity
<?
>)toMethod
.invoke(rel
);
198 } catch (NoSuchMethodException
| SecurityException
| IllegalAccessException
199 | IllegalArgumentException
| InvocationTargetException e
) {
200 throw new RuntimeException(e
);
203 String typeLabel
= null;
205 Representation typeRepr
= type
.getPreferredRepresentation(defaultLanguage
);
206 if (typeRepr
!= null){
207 typeLabel
= typeRepr
.getAbbreviatedLabel();
209 if (isBlank(typeLabel
) && typeRepr
!= null){
210 typeLabel
= typeRepr
.getLabel();
212 if (isBlank(typeLabel
) ){
213 typeLabel
= type
.getSymbol();
215 if (isBlank(typeLabel
)){
216 typeLabel
= type
.getTitleCache();
219 if (isBlank(typeLabel
)){
222 String result
= CdmUtils
.concat(" ", new String
[]{from
== null ?
null : from
.getTitleCache(),
223 typeLabel
, to
== null?
null : to
.getTitleCache()});
227 private static String
getCache(GatheringEvent gatheringEvent
){
228 String ALTITUDE_PREFIX
= "alt. ";
229 final String METER
= "m";
234 AgentBase
<?
> collector
= CdmBase
.deproxy(gatheringEvent
.getCollector());
235 String collectorStr
= collector
== null?
null : collector
.getTitleCache();
236 result
= CdmUtils
.concat(", ", result
, collectorStr
);
239 TimePeriod gatheringPeriod
= gatheringEvent
.getTimeperiod();
240 result
= CdmUtils
.concat(", ", result
, (gatheringPeriod
== null?
null : gatheringPeriod
.toString()));
243 String strCountry
= null;
244 NamedArea country
= gatheringEvent
.getCountry();
245 Representation repCountry
= country
== null ?
null : country
.getRepresentation(Language
.DEFAULT());
246 strCountry
= repCountry
== null ?
null: repCountry
.getLabel();
247 result
= CdmUtils
.concat(", ", result
, strCountry
);
250 LanguageString locality
= gatheringEvent
.getLocality();
251 if (locality
!= null) {
252 result
= CdmUtils
.concat(", ", result
, locality
.getText());
257 if (isNotBlank(gatheringEvent
.getAbsoluteElevationText())){
258 elevationStr
= gatheringEvent
.getAbsoluteElevationText();
260 String text
= gatheringEvent
.getAbsoluteElevationText();
261 Integer min
= gatheringEvent
.getAbsoluteElevation();
262 Integer max
= gatheringEvent
.getAbsoluteElevationMax();
263 elevationStr
= DistanceStringFormatter
.distanceString(min
, max
, text
, METER
);
265 if (isNotBlank(elevationStr
)){
266 result
= CdmUtils
.concat(", " , result
, ALTITUDE_PREFIX
);
267 result
+= elevationStr
;
271 if (gatheringEvent
.getExactLocation() != null){
272 String exactLocation
= gatheringEvent
.getExactLocation().toSexagesimalString(false, false);
273 result
= CdmUtils
.concat(", ", result
, exactLocation
);
279 private static String
getCache(DeterminationEvent detEvent
) {
281 String taxonStr
= null;
282 TaxonName taxonName
= detEvent
.getTaxonName();
283 TaxonBase
<?
> taxon
= detEvent
.getTaxon();
284 if (taxonName
!= null){
285 taxonStr
= taxonName
.getTitleCache();
287 if (isBlank(taxonStr
) && taxon
!= null){
288 taxonStr
= taxon
.getTitleCache();
290 if (isBlank(taxonStr
)){
291 taxonStr
= "no or unlabled taxon";
295 SpecimenOrObservationBase
<?
> unit
= detEvent
.getIdentifiedUnit();
298 unitStr
= unit
.getTitleCache();
299 if (isBlank(unitStr
)){
300 unitStr
= "Unlabled unit";
306 String result
= CdmUtils
.concat(" determined as ", unitStr
, taxonStr
);
311 private static String
getCache(TaxonNode taxonNode
) {
313 Classification classification
= taxonNode
.getClassification();
314 if (classification
!= null){
315 String classificationStr
= classification
.getName() == null ?
"" : classification
.getName().getText();
316 result
= CdmUtils
.concat("" , result
, classificationStr
);
317 if (isBlank(result
)){
318 result
= classification
.toString();
322 TaxonNode parentNode
= taxonNode
.getParent();
323 if (parentNode
== null){
324 parentStr
= "no parent";
326 Taxon parentTaxon
= parentNode
.getTaxon();
327 if (parentTaxon
== null){
328 parentStr
= "no parent taxon";
330 TaxonName parentName
= parentTaxon
.getName();
331 if (parentName
== null){
332 parentStr
= "child of " + parentTaxon
.getTitleCache();
334 parentStr
= "child of " + parentName
.getTitleCache();
338 result
= CdmUtils
.concat(": ", result
, parentStr
);
343 private static String
getCache(TypeDesignationBase
<?
> designation
, Language defaultLanguage
) {
344 designation
= CdmBase
.deproxy(designation
);
346 String fromString
= null;
347 Set
<TaxonName
> from
= designation
.getTypifiedNames();
349 for (TaxonName name
: from
){
350 fromString
= CdmUtils
.concat(",", fromString
, name
.getTitleCache());
354 IdentifiableEntity
<?
> to
= null;
356 if (designation
.isInstanceOf(SpecimenTypeDesignation
.class)){
357 to
= ((SpecimenTypeDesignation
)designation
).getTypeSpecimen();
358 }else if (designation
.isInstanceOf(NameTypeDesignation
.class)){
359 to
= ((NameTypeDesignation
)designation
).getTypeName();
360 }else if (designation
.isInstanceOf(TextualTypeDesignation
.class)){
361 toStr
= ((TextualTypeDesignation
)designation
).getPreferredText(defaultLanguage
);
363 throw new RuntimeException("Type Designation class not supported: " + designation
.getClass().getName());
365 toStr
= to
== null? toStr
: to
.getTitleCache();
367 String typeLabel
= null;
368 TypeDesignationStatusBase
<?
> status
= designation
.getTypeStatus();
370 Representation typeRepr
= status
.getPreferredRepresentation(defaultLanguage
);
371 if (typeRepr
!= null){
372 typeLabel
= typeRepr
.getAbbreviatedLabel();
374 if (isBlank(typeLabel
) && typeRepr
!= null){
375 typeLabel
= typeRepr
.getLabel();
377 if (isBlank(typeLabel
) ){
378 typeLabel
= status
.getSymbol();
380 if (isBlank(typeLabel
)){
381 typeLabel
= status
.getTitleCache();
384 if (isBlank(typeLabel
)){
388 String result
= CdmUtils
.concat(" ", new String
[]{fromString
, typeLabel
, toStr
});
392 private static String
getCache(HomotypicalGroup hg
) {
394 for (TaxonName tnb
: hg
.getTypifiedNames()){
395 result
= CdmUtils
.concat(", ", result
, tnb
.getTitleCache());
397 if (isBlank(result
)){
398 result
= "No typified names";
403 private static String
getCache(KeyStatement ks
) {
405 LanguageString ls
= ks
.getPreferredLanguageString(Language
.DEFAULT());
406 if (ls
!= null && CdmUtils
.isNotBlank(ls
.getText())){
407 result
= ls
.getText();
409 result
= ks
.toString();
414 private static String
getCache(NomenclaturalStatus nomStatus
) {
415 String result
= nomStatus
.getName().getTitleCache();
416 if (nomStatus
.getType()!= null){
417 Representation rep
= nomStatus
.getType().getPreferredRepresentation(Language
.DEFAULT());
419 result
= CdmUtils
.concat(": ", rep
.getAbbreviatedLabel(), result
);
426 private static boolean isNotBlank(String str
){
427 return StringUtils
.isNotBlank(str
);
430 private static boolean isBlank(String str
){
431 return StringUtils
.isBlank(str
);
434 //from taxeditor DescriptionHelper
435 private static String
getCache(DescriptionElementBase element
,
436 Language defaultLanguage
) {
438 String mainElementLabel
= null;
439 DescriptionBase
<?
> descr
= element
.getInDescription();
440 descr
= CdmBase
.deproxy(descr
);
443 if (descr
.isInstanceOf(TaxonDescription
.class)){
444 Taxon taxon
= CdmBase
.deproxy(descr
, TaxonDescription
.class).getTaxon();
446 mainElementLabel
= taxon
.getTitleCache();
448 }else if (descr
.isInstanceOf(SpecimenDescription
.class)){
449 SpecimenOrObservationBase
<?
> specimen
= CdmBase
.deproxy(descr
, SpecimenDescription
.class).getDescribedSpecimenOrObservation();
450 if (specimen
!= null){
451 mainElementLabel
= specimen
.getTitleCache();
453 }else if (descr
.isInstanceOf(TaxonNameDescription
.class)){
454 TaxonName name
= CdmBase
.deproxy(descr
, TaxonNameDescription
.class).getTaxonName();
456 mainElementLabel
= name
.getTitleCache();
462 if (element
instanceof TextData
) {
463 //cache = ((TextData) element).getText(language);
466 if (element
instanceof CommonTaxonName
) {
467 cache
= ((CommonTaxonName
) element
).getName();
469 if (element
instanceof TaxonInteraction
) {
470 Taxon taxon2
= ((TaxonInteraction
) element
).getTaxon2();
471 if(taxon2
!= null && taxon2
.getName() != null){
472 cache
= taxon2
.getName().getTitleCache();
474 cache
= "No taxon chosen";
477 if (element
instanceof Distribution
) {
478 Distribution distribution
= (Distribution
) element
;
480 NamedArea area
= distribution
.getArea();
482 cache
= area
.getLabel();
484 PresenceAbsenceTerm status
= distribution
.getStatus();
486 cache
+= ", no status";
488 cache
+= ", " + status
.getLabel();
492 String result
= cache
== null ?
"" : cache
;
493 if (isNotBlank(mainElementLabel
)){
494 result
= CdmUtils
.concat(" ", result
, "(" + mainElementLabel
+ ")");