Project

General

Profile

« Previous | Next » 

Revision 58e6cb8f

Added by Andreas Müller about 3 years ago

ref #9541 new referencing objects implementation in TaxEditor

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/DescriptionHelper.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.model;
10 10

  
11
import java.lang.reflect.InvocationTargetException;
12
import java.lang.reflect.Method;
13 11
import java.util.Arrays;
14 12
import java.util.List;
15
import java.util.Set;
16 13

  
17 14
import org.apache.commons.lang3.StringUtils;
18
import org.hibernate.LazyInitializationException;
19 15

  
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21
import eu.etaxonomy.cdm.common.CdmUtils;
16
import eu.etaxonomy.cdm.format.DescriptionElementFormatter;
22 17
import eu.etaxonomy.cdm.format.common.ExtendedTimePeriodFormatter;
23 18
import eu.etaxonomy.cdm.format.description.CategoricalDataFormatter;
24 19
import eu.etaxonomy.cdm.format.description.QuantitativeDataFormatter;
25
import eu.etaxonomy.cdm.model.agent.AgentBase;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27 20
import eu.etaxonomy.cdm.model.common.ExtendedTimePeriod;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29 21
import eu.etaxonomy.cdm.model.common.Language;
30 22
import eu.etaxonomy.cdm.model.common.LanguageString;
31
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
32
import eu.etaxonomy.cdm.model.common.Marker;
33
import eu.etaxonomy.cdm.model.common.MarkerType;
34
import eu.etaxonomy.cdm.model.common.RelationshipBase;
35
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
36
import eu.etaxonomy.cdm.model.common.TimePeriod;
37 23
import eu.etaxonomy.cdm.model.description.CategoricalData;
38 24
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
39 25
import eu.etaxonomy.cdm.model.description.DescriptionBase;
......
42 28
import eu.etaxonomy.cdm.model.description.Distribution;
43 29
import eu.etaxonomy.cdm.model.description.Feature;
44 30
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
45
import eu.etaxonomy.cdm.model.description.KeyStatement;
46 31
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
47 32
import eu.etaxonomy.cdm.model.description.QuantitativeData;
48
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
49
import eu.etaxonomy.cdm.model.description.TaxonDescription;
50 33
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
51
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
52 34
import eu.etaxonomy.cdm.model.description.TemporalData;
53 35
import eu.etaxonomy.cdm.model.description.TextData;
54 36
import eu.etaxonomy.cdm.model.location.NamedArea;
55 37
import eu.etaxonomy.cdm.model.media.Media;
56
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
57
import eu.etaxonomy.cdm.model.name.HybridRelationship;
58
import eu.etaxonomy.cdm.model.name.NameRelationship;
59
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
60
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
61
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
62
import eu.etaxonomy.cdm.model.name.TaxonName;
63
import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
64
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
65
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
66
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
67
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
68 38
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
69
import eu.etaxonomy.cdm.model.permission.Group;
70
import eu.etaxonomy.cdm.model.permission.User;
71
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
72
import eu.etaxonomy.cdm.model.taxon.Classification;
73 39
import eu.etaxonomy.cdm.model.taxon.Taxon;
74
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
75
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
76
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
77
import eu.etaxonomy.cdm.model.term.Representation;
78 40
import eu.etaxonomy.taxeditor.store.CdmStore;
79 41

  
80 42
/**
......
95 57
	 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
96 58
	 * @return a {@link java.lang.String} object.
97 59
	 */
98
	public static String getCache(DescriptionElementBase element,
99
			Language language) {
100

  
101
		String mainElementLabel= null;
102
		DescriptionBase<?> descr = element.getInDescription();
103
		descr = CdmBase.deproxy(descr, DescriptionBase.class);
104

  
105
		if (descr != null){
106
			if (descr.isInstanceOf(TaxonDescription.class)){
107
				Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
108
				if (taxon != null){
109
					mainElementLabel = taxon.getTitleCache();
110
				}
111
			}else if (descr.isInstanceOf(SpecimenDescription.class)){
112
				SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
113
				if (specimen != null){
114
					mainElementLabel = specimen.getTitleCache();
115
				}
116
			}else if (descr.isInstanceOf(TaxonNameDescription.class)){
117
				TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
118
				if (name != null){
119
					mainElementLabel = name.getTitleCache();
120
				}
121
			}
122
		}
123

  
124
		String cache = null;
125
		if (element instanceof TextData) {
126
			//cache = ((TextData) element).getText(language);
127
		    cache = "Text Data";
128
		}
129
		if (element instanceof CommonTaxonName) {
130
			cache = ((CommonTaxonName) element).getName();
131
		}
132
		if (element instanceof TaxonInteraction) {
133
			Taxon taxon2 = ((TaxonInteraction) element).getTaxon2();
134
			if(taxon2 != null && taxon2.getName() != null){
135
				cache = taxon2.getName().getTitleCache();
136
			}else{
137
				cache = "No taxon chosen";
138
			}
139
		}
140
		if (element instanceof Distribution) {
141
			Distribution distribution = (Distribution) element;
142

  
143
			NamedArea area = distribution.getArea();
144
			if(area != null){
145
				cache =  area.getLabel();
146

  
147
				PresenceAbsenceTerm status = distribution.getStatus();
148
				if (status == null){
149
					cache += ", no status";
150
				}else {
151
					cache += ", " + status.getLabel();
152
				}
153
			}
154
		}
155
		String result = cache == null ? "" : cache;
156
		if (isNotBlank(mainElementLabel)){
157
			result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
158
		}
159
		return result;
60
	public static String getCache(DescriptionElementBase element, Language language) {
61
	    return DescriptionElementFormatter.format(element, language);
160 62
	}
161 63

  
162 64
	/**
......
196 98
		}
197 99
	}
198 100

  
101
//	public static String getObjectDescription(Object o){
102
//	    if (o instanceof CdmBase){
103
//	        return ReferencingObjectFormatter.format((CdmBase)o, CdmStore.getDefaultLanguage());
104
//	    }else{
105
//	        return o.toString();
106
//	    }
107
//	}
108

  
199 109
	/**
200 110
	 * Set whatever the element's title cache equivalent is,
201 111
	 * depending on its class, using the default language.
......
208 118
		setCache(element, value, CdmStore.getDefaultLanguage());
209 119
	}
210 120

  
211
	public static String getObjectDescription(Object element) {
212
	    String resultString = null;
213
		if (element instanceof IdentifiableEntity) {
214
			try{
215
			    resultString = ((IdentifiableEntity<?>) element).getTitleCache();
216
			}catch(LazyInitializationException e){
217
				String result = "No Session to initialize title cache for IdentifiableEntity";
218
				MessagingUtils.error(DescriptionHelper.class, result, e);
219
				resultString = "TODO: " + result;
220
			}
221
		}else if (element instanceof OriginalSourceBase) {
222
			try{
223
				OriginalSourceBase originalSource = (OriginalSourceBase) element;
224
//				ISourceable sourcedObject = originalSource.getSourcedObj();
225
				//due to #5743 the bidirectionality for sourced object had to be removed
226

  
227
				String sourceObjectTitle = "sourced object data not available (#5743)";
228

  
229
				//it is now possible for NomenclaturalSource as they link to the sourced name
230
				if (originalSource instanceof NomenclaturalSource){
231
				    TaxonName sourcedName = ((NomenclaturalSource)originalSource).getSourcedName();
232
				    sourceObjectTitle = sourcedName == null ? "Source not attached to a name" :
233
				        "Nom. source for " + sourcedName.getTitleCache();
234
				}
235

  
236

  
237
				resultString = CdmUtils.concat("; ", new String[]{originalSource.getIdNamespace(), originalSource.getIdInSource(), sourceObjectTitle});
238
			}catch(LazyInitializationException e){
239
				String result = "Error initializing title cache for ISourceable of an OriginalSourceBase";
240
				MessagingUtils.error(DescriptionHelper.class, result, e);
241
				resultString = "TODO: " + result;
242
			}
243
		}else if (element instanceof LanguageStringBase) {
244
		    resultString = ((LanguageStringBase) element).getText();
245
		}else if (element instanceof DescriptionElementBase) {
246
		    resultString = getCache((DescriptionElementBase) element);
247
		}else if (element instanceof RelationshipBase<?, ?, ?>) {
248
		    resultString = getCache((RelationshipBase<?, ?, ?>) element);
249
		}else if (element instanceof TypeDesignationBase<?>) {
250
		    resultString = getCache((TypeDesignationBase<?>) element);
251
		}else if (element instanceof HomotypicalGroup) {
252
		    resultString = getCache((HomotypicalGroup) element);
253
		}else if (element instanceof TaxonNode) {
254
		    resultString = getCache((TaxonNode) element);
255
		}else if (element instanceof DeterminationEvent) {
256
		    resultString = getCache((DeterminationEvent) element);
257
		}else if (element instanceof GatheringEvent){
258
			resultString = getCache((GatheringEvent) element);
259
		}else if (element instanceof Marker) {
260
			Marker marker = (Marker) element;
261
			MarkerType type = marker.getMarkerType();
262
			resultString = (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
263
		}else if (element instanceof User) {
264
			User user = (User) element;
265
			resultString = user.getUsername();
266
		}else if (element instanceof Group) {
267
			Group group = (Group) element;
268
			resultString = group.getName();
269
		}else if (element instanceof KeyStatement) {
270
            KeyStatement keyStatement = (KeyStatement) element;
271
            resultString = getCache(keyStatement);
272
        }else{
273
			// TODO write return texts for HomotypicalGroup, etc.
274
            resultString = element.toString();
275
		}
276

  
277
		if (resultString == null){
278
		    resultString = element.toString();
279
		}
280
		return resultString;
281
	}
282

  
283
	private static String getCache(GatheringEvent gatheringEvent){
284
		String ALTITUDE_PREFIX = "alt. ";
285
		final String METER = "m";
286

  
287
		String result = "";
288

  
289
		//collector
290
		AgentBase<?> collector = CdmBase.deproxy(gatheringEvent.getCollector());
291
		String collectorStr = collector == null? null : collector.getTitleCache();
292
		result = CdmUtils.concat(", ", result, collectorStr);
293

  
294
		//gathering period
295
		TimePeriod gatheringPeriod = gatheringEvent.getTimeperiod();
296
		result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
297

  
298
		//country
299
		String strCountry = null;
300
		NamedArea country = gatheringEvent.getCountry();
301
		Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
302
		strCountry = repCountry == null ? null: repCountry.getLabel();
303
		result = CdmUtils.concat(", ", result, strCountry);
304

  
305
		//locality
306
		LanguageString locality = gatheringEvent.getLocality();
307
		if (locality != null) {
308
			result = CdmUtils.concat(", ", result, locality.getText());
309
		}
310

  
311
		//elevation
312
		String elevationStr;
313
		if (isNotBlank(gatheringEvent.getAbsoluteElevationText())){
314
			elevationStr = gatheringEvent.getAbsoluteElevationText();
315
		}else{
316
			String text = gatheringEvent.getAbsoluteElevationText();
317
			Integer min = gatheringEvent.getAbsoluteElevation();
318
			Integer max = gatheringEvent.getAbsoluteElevationMax();
319
			elevationStr = DerivedUnitFacade.distanceString(min, max, text, METER);
320
		}
321
		if (isNotBlank(elevationStr)){
322
			result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
323
			result += elevationStr;
324
		}
325

  
326
		//exact locality
327
		if (gatheringEvent.getExactLocation() != null){
328
			String exactLocation = gatheringEvent.getExactLocation().toSexagesimalString(false, false);
329
			result = CdmUtils.concat(", ", result, exactLocation);
330
		}
331

  
332
		return result;
333
	}
334

  
335
	private static String getCache(DeterminationEvent detEvent) {
336
		//taxon
337
		String taxonStr = null;
338
		TaxonName taxonName = detEvent.getTaxonName();
339
		TaxonBase<?> taxon = detEvent.getTaxon();
340
		if (taxonName != null){
341
			taxonStr = taxonName.getTitleCache();
342
		}
343
		if (isBlank(taxonStr) && taxon != null){
344
			taxonStr = taxon.getTitleCache();
345
		}
346
		if (isBlank(taxonStr)){
347
			taxonStr = "no or unlabled taxon";
348
		}
349

  
350
		//unit
351
		SpecimenOrObservationBase<?> unit = detEvent.getIdentifiedUnit();
352
		String unitStr;
353
		if (unit != null){
354
			unitStr = unit.getTitleCache();
355
			if (isBlank(unitStr)){
356
				unitStr = "Unlabled unit";
357
			}
358
		}else{
359
			unitStr = "no unit";
360
		}
361

  
362
		String result = CdmUtils.concat(" determined as ", unitStr, taxonStr);
363

  
364
		return result;
365
	}
366

  
367
	private static String getCache(TaxonNode taxonNode) {
368
		String result = "";
369
		Classification classification = taxonNode.getClassification();
370
		if (classification != null){
371
			String classificationStr = classification.getName() == null ? "" : classification.getName().getText();
372
			result = CdmUtils.concat("" , result, classificationStr);
373
			if (isBlank(result)){
374
				result = classification.toString();
375
			}
376
		}
377
		String parentStr;
378
		TaxonNode parentNode = taxonNode.getParent();
379
		if (parentNode == null){
380
			parentStr = "no parent";
381
		}else{
382
			Taxon parentTaxon = parentNode.getTaxon();
383
			if (parentTaxon == null){
384
				parentStr = "no parent taxon";
385
			}else{
386
				TaxonName parentName = parentTaxon.getName();
387
				if (parentName == null){
388
					parentStr = "child of " + parentTaxon.getTitleCache();
389
				}else{
390
					parentStr = "child of " + parentName.getTitleCache();
391
				}
392
			}
393
		}
394
		result = CdmUtils.concat(": ", result, parentStr);
395

  
396
		return result;
397
	}
398

  
399
	private static String getCache(TypeDesignationBase<?> designation) {
400
		designation = CdmBase.deproxy(designation);
401
		//from
402
		String fromString = null;
403
		Set<TaxonName> from = designation.getTypifiedNames();
404
		if(from != null){
405
		    for (TaxonName name : from){
406
		        fromString = CdmUtils.concat(",", fromString, name.getTitleCache());
407
		    }
408
		}
409
		//to
410
		IdentifiableEntity<?> to = null;
411
		String toStr = "";
412
		if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
413
			to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
414
		}else if (designation.isInstanceOf(NameTypeDesignation.class)){
415
			to = ((NameTypeDesignation)designation).getTypeName();
416
		}else if (designation.isInstanceOf(TextualTypeDesignation.class)){
417
		    toStr = ((TextualTypeDesignation)designation).getPreferredText(CdmStore.getDefaultLanguage());
418
		}else{
419
			throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
420
		}
421
		toStr = to == null? toStr : to.getTitleCache();
422
		//status
423
		String typeLabel = null;
424
		TypeDesignationStatusBase<?> status = designation.getTypeStatus();
425
		if (status != null){
426
			Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
427
			if (typeRepr != null){
428
				typeLabel = typeRepr.getAbbreviatedLabel();
429
			}
430
			if (isBlank(typeLabel) && typeRepr != null){
431
				typeLabel = typeRepr.getLabel();
432
			}
433
			if (isBlank(typeLabel) ){
434
				typeLabel = status.getSymbol();
435
			}
436
			if (isBlank(typeLabel)){
437
				typeLabel = status.getTitleCache();
438
			}
439
		}
440
		if (isBlank(typeLabel)){
441
			typeLabel = "->";
442
		}
443
		//concat
444
		String result = CdmUtils.concat(" ", new String[]{fromString, typeLabel, toStr});
445
		return result;
446
	}
447

  
448
	private static String getCache(RelationshipBase<?, ?, ?> rel) {
449
		rel = CdmBase.deproxy(rel);
450
		RelationshipTermBase<?> type = rel.getType();
451
		IdentifiableEntity<?> from;
452
		IdentifiableEntity<?> to;
453
		if (rel.isInstanceOf(NameRelationship.class)){
454
			from = ((NameRelationship)rel).getFromName();
455
			to = ((NameRelationship)rel).getToName();
456
		}else if (rel.isInstanceOf(HybridRelationship.class)){
457
			from = ((HybridRelationship)rel).getParentName();
458
			to = ((HybridRelationship)rel).getHybridName();
459
		}else if (rel.isInstanceOf(TaxonRelationship.class)){
460
			from = ((TaxonRelationship)rel).getFromTaxon();
461
			to = ((TaxonRelationship)rel).getToTaxon();
462
		}else{
463
			try {
464
				Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
465
				Method toMethod = rel.getClass().getMethod("getRelatedFrom");
466
				fromMethod.setAccessible(true);
467
				toMethod.setAccessible(true);
468
				from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
469
				to = (IdentifiableEntity<?>)toMethod.invoke(rel);
470
			} catch (NoSuchMethodException | SecurityException | IllegalAccessException
471
				| IllegalArgumentException | InvocationTargetException e) {
472
				throw new RuntimeException(e);
473
			}
474
		}
475
		String typeLabel = null;
476
		if (type != null){
477
			Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
478
			if (typeRepr != null){
479
				typeLabel = typeRepr.getAbbreviatedLabel();
480
			}
481
			if (isBlank(typeLabel) && typeRepr != null){
482
				typeLabel = typeRepr.getLabel();
483
			}
484
			if (isBlank(typeLabel) ){
485
				typeLabel = type.getSymbol();
486
			}
487
			if (isBlank(typeLabel)){
488
				typeLabel = type.getTitleCache();
489
			}
490
		}
491
		if (isBlank(typeLabel)){
492
			typeLabel = "->";
493
		}
494
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(),
495
				typeLabel, to == null? null : to.getTitleCache()});
496
		return result;
497
	}
498

  
499

  
500
	private static String getCache(HomotypicalGroup hg) {
501
		String result = "";
502
		for (TaxonName tnb : hg.getTypifiedNames()){
503
			result = CdmUtils.concat(", ", result, tnb.getTitleCache());
504
		}
505
		if (isBlank(result)){
506
			result = "No typified names";
507
		}
508
		return result;
509
	}
510

  
511
	private static String getCache(KeyStatement ks) {
512
        String result = "";
513

  
514
        result = "KeyStatement";
515

  
516
        return result;
517
    }
518

  
519
	public static String getObjectClassname(Object element) {
520
		return element.getClass().getSimpleName();
521
	}
121
//	public static String getObjectClassname(Object element) {
122
//		if (element instanceof CdmBase){
123
//		    return ((CdmBase) element).getUserFriendlyTypeName();
124
//		}else{
125
//		    return element.getClass().getSimpleName();
126
//		}
127
//	}
522 128

  
523 129
	public static String getFeatureNodeContainerText(FeatureNodeContainer element) {
524 130
		String result = null;

Also available in: Unified diff