Project

General

Profile

« Previous | Next » 

Revision a799db81

Added by Andreas Müller almost 8 years ago

Improve DescriptionHelper / Referencing objects label provider #5835

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/DescriptionHelper.java
10 10

  
11 11
package eu.etaxonomy.taxeditor.model;
12 12

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

  
19
import org.apache.commons.lang.StringUtils;
16 20
import org.hibernate.LazyInitializationException;
17 21

  
18 22
import eu.etaxonomy.cdm.api.service.DefaultCategoricalDescriptionBuilder;
19 23
import eu.etaxonomy.cdm.api.service.DefaultQuantitativeDescriptionBuilder;
20 24
import eu.etaxonomy.cdm.api.service.DescriptionBuilder;
21 25
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
23
import eu.etaxonomy.cdm.model.common.ISourceable;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.Group;
24 28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 29
import eu.etaxonomy.cdm.model.common.Language;
26 30
import eu.etaxonomy.cdm.model.common.LanguageString;
......
28 32
import eu.etaxonomy.cdm.model.common.Marker;
29 33
import eu.etaxonomy.cdm.model.common.MarkerType;
30 34
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
35
import eu.etaxonomy.cdm.model.common.RelationshipBase;
36
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
37
import eu.etaxonomy.cdm.model.common.Representation;
38
import eu.etaxonomy.cdm.model.common.User;
31 39
import eu.etaxonomy.cdm.model.description.CategoricalData;
32 40
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
33 41
import eu.etaxonomy.cdm.model.description.DescriptionBase;
......
38 46
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
39 47
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
40 48
import eu.etaxonomy.cdm.model.description.QuantitativeData;
49
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
50
import eu.etaxonomy.cdm.model.description.TaxonDescription;
41 51
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
52
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
42 53
import eu.etaxonomy.cdm.model.description.TextData;
43 54
import eu.etaxonomy.cdm.model.location.NamedArea;
44 55
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.SpecimenTypeDesignation;
61
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
62
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
63
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
45 64
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
65
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
46 66
import eu.etaxonomy.cdm.model.taxon.Taxon;
67
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
47 68
import eu.etaxonomy.taxeditor.store.CdmStore;
48 69

  
49 70
/**
......
52 73
 * @author p.ciardelli
53 74
 * @author n.hoffmann
54 75
 * @created 02.04.2009
55
 * @version 1.0
56 76
 */
57 77
public class DescriptionHelper {
58 78

  
......
66 86
	 */
67 87
	public static String getCache(DescriptionElementBase element,
68 88
			Language language) {
89
		
90
		String mainElementLabel= null;
91
		DescriptionBase<?> descr = element.getInDescription();
92
		if (descr != null){
93
			if (descr.isInstanceOf(TaxonDescription.class)){
94
				Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
95
				if (taxon != null){
96
					mainElementLabel = taxon.getTitleCache();
97
				}
98
			}else if (descr.isInstanceOf(SpecimenDescription.class)){
99
				SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
100
				if (specimen != null){
101
					mainElementLabel = specimen.getTitleCache();
102
				}
103
			}else if (descr.isInstanceOf(TaxonNameDescription.class)){
104
				TaxonNameBase<?, ?> name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
105
				if (name != null){
106
					mainElementLabel = name.getTitleCache();
107
				}
108
			}
109
		}
110
		
69 111
		String cache = null;
70 112
		if (element instanceof TextData) {
71 113
			cache = ((TextData) element).getText(language);
......
97 139
				}
98 140
			}
99 141
		}
100
		return cache == null ? "" : cache;
142
		String result = cache == null ? "" : cache;
143
		if (StringUtils.isNotBlank(mainElementLabel)){
144
			result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
145
		}
146
		return result;
101 147
	}
102 148

  
103 149
	/**
......
124 170
		if (element instanceof TextData) {
125 171
			((TextData) element).putText(language, value);
126 172
			return;
127
		}
128
		if (element instanceof CommonTaxonName) {
173
		}else if (element instanceof CommonTaxonName) {
129 174
			((CommonTaxonName) element).setName(value);
130 175
			return;
131
		}
132
		if (element instanceof TaxonInteraction) {
176
		}else if (element instanceof TaxonInteraction) {
133 177

  
134
		}
135
		if(element instanceof Distribution){
178
		}else if(element instanceof Distribution){
136 179
			MessagingUtils.warn(DescriptionHelper.class, "trying to set cache on distribution, don't know what to do at the moment.");
137 180
			return;
181
		}else{
182
			MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
138 183
		}
139
		MessagingUtils.warn(DescriptionHelper.class, "No matching subclass found for DescriptionElementBase object, 'cache' not set.");
140 184
	}
141 185

  
142 186
	/**
......
151 195
		setCache(element, value, CdmStore.getDefaultLanguage());
152 196
	}
153 197

  
154
	/* (non-Javadoc)
155
	 * @see eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.IReferencingObjectsService#getObjectDescription(java.lang.Object)
156
	 */
198

  
157 199
	/**
158 200
	 * <p>getObjectDescription</p>
159 201
	 *
......
169 211
				MessagingUtils.error(DescriptionHelper.class, result, e);
170 212
				return "TODO: " + result;
171 213
			}
172
		}
173
		if (element instanceof OriginalSourceBase) {
214
		}else if (element instanceof OriginalSourceBase) {
174 215
			try{
175
				OriginalSourceBase originalSource = (OriginalSourceBase) element;
216
				OriginalSourceBase<?> originalSource = (OriginalSourceBase<?>) element;
176 217
//				ISourceable sourcedObject = originalSource.getSourcedObj();
177 218
				//due to #5743 the bidirectionality for sourced object had to be removed 
178 219
				String sourceObjectTitle = "sourced object data not available (#5743)";
......
190 231
				MessagingUtils.error(DescriptionHelper.class, result, e);
191 232
				return "TODO: " + result;
192 233
			}
193
		}
194
		if (element instanceof LanguageStringBase) {
234
		}else if (element instanceof LanguageStringBase) {
195 235
			return ((LanguageStringBase) element).getText();
196
		}
197
		if (element instanceof DescriptionElementBase) {
236
		}else if (element instanceof DescriptionElementBase) {
198 237
			return getCache((DescriptionElementBase) element);
199
		}
200
		if (element instanceof Marker) {
238
		}else if (element instanceof RelationshipBase<?, ?, ?>) {
239
			return getCache((RelationshipBase<?, ?, ?>) element);
240
		}else if (element instanceof TypeDesignationBase<?>) {
241
			return getCache((TypeDesignationBase<?>) element);
242
		}else if (element instanceof HomotypicalGroup) {
243
			return getCache((HomotypicalGroup) element);
244
		}else if (element instanceof Marker) {
201 245
			Marker marker = (Marker) element;
202 246
			MarkerType type = marker.getMarkerType();
203 247
			return (type == null ? "- no marker type -" : marker.getMarkerType().getLabel()) + " (" + marker.getFlag() + ")";
248
		}else if (element instanceof User) {
249
			User user = (User) element;
250
			return user.getUsername();
251
		}else if (element instanceof Group) {
252
			Group group = (Group) element;
253
			return group.getName();
254
		}else{
255
			// TODO write return texts for HomotypicalGroup, etc.
256
			return element.toString();
257
		}
258
	}
259

  
260

  
261
	private static String getCache(TypeDesignationBase<?> designation) {
262
		designation = CdmBase.deproxy(designation);
263
		TypeDesignationStatusBase<?> status = designation.getTypeStatus();
264
		Set<TaxonNameBase> from;
265
		IdentifiableEntity<?> to;
266
		from = designation.getTypifiedNames();
267
		if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
268
			to = ((SpecimenTypeDesignation)designation).getTypeSpecimen();
269
		}else if (designation.isInstanceOf(NameTypeDesignation.class)){
270
			to = ((NameTypeDesignation)designation).getTypeName();	
271
		}else{
272
			throw new RuntimeException("Type Designation class not supported: " +  designation.getClass().getName());
204 273
		}
205
		// TODO write return texts for NameRelationship, HomotypicalGroup, SpecimenTypeDesignation, etc.
206
		return element.toString();
274
		String typeLabel = null;
275
		if (status != null){
276
			Representation typeRepr = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
277
			if (typeRepr != null){
278
				typeLabel = typeRepr.getAbbreviatedLabel();
279
			}
280
			if (StringUtils.isBlank(typeLabel) && typeRepr != null){
281
				typeLabel = typeRepr.getLabel();
282
			}
283
			if (StringUtils.isBlank(typeLabel) ){
284
				typeLabel = status.getSymbol();
285
			}
286
			if (StringUtils.isBlank(typeLabel)){
287
				typeLabel = status.getTitleCache();
288
			}
289
		}
290
		if (StringUtils.isBlank(typeLabel)){
291
			typeLabel = status.getTitleCache();
292
			typeLabel = "->";
293
		}
294
		String fromString = "";
295
		for (TaxonNameBase<?,?> name : from){
296
			CdmUtils.concat(",", fromString, name.getTitleCache());
297
		}
298
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : fromString, 
299
				typeLabel, to == null? null : to.getTitleCache()});
300
		return result;
301
	}
302

  
303
	private static String getCache(RelationshipBase<?, ?, ?> rel) {
304
		rel = CdmBase.deproxy(rel);
305
		RelationshipTermBase<?> type = rel.getType();
306
		IdentifiableEntity<?> from;
307
		IdentifiableEntity<?> to;
308
		if (rel.isInstanceOf(SynonymRelationship.class)){
309
			from = ((SynonymRelationship)rel).getSynonym();
310
			to = ((SynonymRelationship)rel).getAcceptedTaxon();
311
		}else if (rel.isInstanceOf(NameRelationship.class)){
312
			from = ((NameRelationship)rel).getFromName();
313
			to = ((NameRelationship)rel).getToName();
314
		}else if (rel.isInstanceOf(HybridRelationship.class)){
315
			from = ((HybridRelationship)rel).getParentName();
316
			to = ((HybridRelationship)rel).getHybridName();
317
		}else if (rel.isInstanceOf(TaxonRelationship.class)){
318
			from = ((TaxonRelationship)rel).getFromTaxon();
319
			to = ((TaxonRelationship)rel).getToTaxon();
320
		}else{
321
			try {
322
				Method fromMethod = rel.getClass().getMethod("getRelatedFrom");
323
				Method toMethod = rel.getClass().getMethod("getRelatedFrom");
324
				fromMethod.setAccessible(true);
325
				toMethod.setAccessible(true);
326
				from = (IdentifiableEntity<?>)fromMethod.invoke(rel);
327
				to = (IdentifiableEntity<?>)toMethod.invoke(rel);
328
			} catch (NoSuchMethodException e) {
329
				throw new RuntimeException(e);
330
			} catch (SecurityException e) {
331
				throw new RuntimeException(e);
332
			} catch (IllegalAccessException e) {
333
				throw new RuntimeException(e);
334
			} catch (IllegalArgumentException e) {
335
				throw new RuntimeException(e);
336
			} catch (InvocationTargetException e) {
337
				throw new RuntimeException(e);
338
			}
339
			
340
		}
341
		String typeLabel = null;
342
		if (type != null){
343
			Representation typeRepr = type.getPreferredRepresentation(CdmStore.getDefaultLanguage());
344
			if (typeRepr != null){
345
				typeLabel = typeRepr.getAbbreviatedLabel();
346
			}
347
			if (StringUtils.isBlank(typeLabel) && typeRepr != null){
348
				typeLabel = typeRepr.getLabel();
349
			}
350
			if (StringUtils.isBlank(typeLabel) ){
351
				typeLabel = type.getSymbol();
352
			}
353
			if (StringUtils.isBlank(typeLabel)){
354
				typeLabel = type.getTitleCache();
355
			}
356
		}
357
		if (StringUtils.isBlank(typeLabel)){
358
			typeLabel = type.getTitleCache();
359
			typeLabel = "->";
360
		}
361
		String result = CdmUtils.concat(" ", new String[]{from == null ? null : from.getTitleCache(), 
362
				typeLabel, to == null? null : to.getTitleCache()});
363
		return result;
364
	}
365
	
366

  
367
	private static String getCache(HomotypicalGroup hg) {
368
		String result = "";
369
		for (TaxonNameBase<?,?> tnb : hg.getTypifiedNames()){
370
			result = CdmUtils.concat(", ", result, tnb.getTitleCache());
371
		}
372
		if (StringUtils.isBlank(result)){
373
			result = "No typified names";
374
		}
375
		return result;
207 376
	}
208 377

  
209 378
	/**

Also available in: Unified diff