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.common.Representation;
|
7
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
8
|
import eu.etaxonomy.cdm.model.description.Feature;
|
9
|
import eu.etaxonomy.cdm.model.description.TextData;
|
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) return result;
|
73
|
}
|
74
|
else if (option==2){
|
75
|
result = representation.getText();
|
76
|
if (result != null) return result;
|
77
|
}
|
78
|
return representation.getLabel();
|
79
|
}
|
80
|
|
81
|
|
82
|
/**
|
83
|
* Returns a TextData with the name of the feature.
|
84
|
*
|
85
|
* @param feature
|
86
|
* @param languages
|
87
|
* @return
|
88
|
*/
|
89
|
public TextData buildTextDataFeature(Feature feature, List<Language> languages){
|
90
|
return TextData.NewInstance(getRightText(feature.getPreferredRepresentation(languages)),languages.get(0),null);
|
91
|
}
|
92
|
|
93
|
}
|