Project

General

Profile

Download (2.77 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.model.common;
2

    
3
import java.util.HashSet;
4
import java.util.Set;
5

    
6
import javax.persistence.FetchType;
7
import javax.persistence.MappedSuperclass;
8
import javax.persistence.OneToMany;
9
import javax.persistence.Transient;
10

    
11
import org.apache.log4j.Logger;
12
import org.hibernate.annotations.Cascade;
13
import org.hibernate.annotations.CascadeType;
14

    
15
@MappedSuperclass
16
public abstract class TermBase extends VersionableEntity {
17
	private static final Logger logger = Logger.getLogger(TermBase.class);
18
	
19
	private String uri;
20
	private Set<Representation> representations = new HashSet<Representation>();
21
	
22
	public TermBase() {
23
		super();
24
	}
25
	public TermBase(String term, String label) {
26
		super();
27
		this.addRepresentation(new Representation(term, label, Language.DEFAULT()) );
28
	}
29

    
30
	@OneToMany(fetch=FetchType.EAGER)
31
	@Cascade( { CascadeType.SAVE_UPDATE, CascadeType.DELETE })
32
	public Set<Representation> getRepresentations() {
33
		return this.representations;
34
	}
35

    
36
	public void setRepresentations(Set<Representation> representations) {
37
		this.representations = representations;
38
	}
39

    
40
	public void addRepresentation(Representation representation) {
41
		this.representations.add(representation);
42
	}
43

    
44
	public void removeRepresentation(Representation representation) {
45
		this.representations.remove(representation);
46
	}
47

    
48
	@Transient
49
	public Representation getRepresentation(Language lang) {
50
		for (Representation repr : representations){
51
			Language reprLanguage = repr.getLanguage();
52
			if (reprLanguage != null && reprLanguage.equals(lang)){
53
				return repr;
54
			}
55
		}
56
		return null;
57
	}
58

    
59
	public String getUri() {
60
		return this.uri;
61
	}
62

    
63
	public void setUri(String uri) {
64
		this.uri = uri;
65
	}
66

    
67
	@Transient
68
	public String getLabel() {
69
		if(getLabel(Language.DEFAULT())!=null){
70
			Representation repr = getRepresentation(Language.DEFAULT());
71
			return (repr == null)? null :repr.getLabel();
72
		}else{
73
			for (Representation r : representations){
74
				return r.getLabel();
75
			}			
76
		}
77
		return super.getUuid().toString();
78
	}
79

    
80
	@Transient
81
	public String getLabel(Language lang) {
82
		Representation repr = this.getRepresentation(lang);
83
		return (repr == null) ? null : repr.getLabel();
84
	}
85

    
86
	@Transient
87
	public String getDescription() {
88
		return this.getDescription(Language.DEFAULT());
89
	}
90

    
91
	@Transient
92
	public String getDescription(Language lang) {
93
		Representation repr = this.getRepresentation(lang);
94
		return (repr == null) ? null :repr.getDescription();
95
	}
96

    
97
	@Override
98
	public boolean equals(Object obj) {
99
		if (TermBase.class.isAssignableFrom(obj.getClass())){
100
			TermBase dtb = (TermBase)obj;
101
			if (dtb.getUuid().equals(this.getUuid())){
102
				return true;
103
			}
104
		}
105
		return false;
106
	}
107

    
108
	@Override
109
	public String toString() {
110
		//FIXME make toString save as explained in CdmBase.toString
111
		return super.toString()+" "+this.getLabel();
112
	}
113

    
114
}
(32-32/39)