Project

General

Profile

Download (2.48 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.service;
2

    
3
import java.util.List;
4

    
5
import eu.etaxonomy.cdm.model.common.Language;
6
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
7
import eu.etaxonomy.cdm.model.description.Feature;
8
import eu.etaxonomy.cdm.model.description.TextData;
9
import eu.etaxonomy.cdm.model.term.Representation;
10

    
11
/**
12
 * Abstract class that defines the basic element for constructing natural language descriptions.
13
 *
14
 * @author m.venin
15
 *
16
 */
17
public abstract class DescriptionBuilder<T extends DescriptionElementBase> {
18

    
19
	protected String separator = ",";// the basic separator, used for example to separate states when building a description
20
	// of a CategoricalData
21
	private int option = 0; // option used to return either the text, the label or the abbreviation of a Representation.
22
	// By default a builder returns the label
23

    
24

    
25
	/**
26
	 * Sets the builder to return the abbreviation contained in the Representation element of an object
27
	 */
28
	public void returnAbbreviatedLabels() {
29
		option=1;
30
	}
31

    
32
	/**
33
	 * Sets the builder to return the text contained in the Representation element of an object
34
	 */
35
	public void returnTexts() {
36
		option=2;
37
	}
38

    
39
	/**
40
	 * Sets the builder to return the label contained in the Representation element of an object
41
	 */
42
	public void returnLabels() {
43
		option=0;
44
	}
45

    
46
	public void setSeparator(String newSeparator) {
47
		separator = newSeparator;
48
	}
49

    
50
	public String getSeparator() {
51
		return separator;
52
	}
53

    
54
	/**
55
	 * Returns the TextData element with the description of the according DescriptionElement
56
	 *
57
	 * @param descriptionElement
58
	 * @param languages
59
	 * @return
60
	 */
61
	public abstract TextData build(T descriptionElement, List<Language> languages);
62

    
63
	/**
64
	 * Returns either the text, label or abbreviation of a Representation
65
	 * @param representation
66
	 * @return
67
	 */
68
	protected String getRightText(Representation representation){
69
		String result;
70
		if (option==1){
71
			result = representation.getAbbreviatedLabel();
72
			if (result != null) {
73
                return result;
74
            }
75
		}
76
		else if (option==2){
77
			result = representation.getText();
78
			if (result != null) {
79
                return result;
80
            }
81
		}
82
		return representation.getLabel();
83
	}
84

    
85

    
86
	/**
87
	 * Returns a TextData with the name of the feature.
88
	 *
89
	 * @param feature
90
	 * @param languages
91
	 * @return
92
	 */
93
	public TextData buildTextDataFeature(Feature feature, List<Language> languages){
94
		return TextData.NewInstance(getRightText(feature.getPreferredRepresentation(languages)),languages.get(0),null);
95
	}
96

    
97
}
(15-15/103)