(no commit message)
[cdmlib.git] / cdmlibrary / src / main / java / eu / etaxonomy / cdm / model / common / TermVocabulary.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.model.common;
11
12
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16
17 import java.util.*;
18 import javax.persistence.*;
19
20 /**
21 * A single enumeration must only contain DefinedTerm instances of one kind
22 * (=class)
23 * @author m.doering
24 * @version 1.0
25 * @created 08-Nov-2007 13:06:23
26 */
27 @Entity
28 public class TermVocabulary extends TermBase {
29 static Logger logger = Logger.getLogger(TermVocabulary.class);
30 //The order of the enumeration list is a linear order that can be used for statistical purposes. Measurement scale =
31 //ordinal
32 private boolean isOrdinal;
33 protected List<DefinedTermBase> terms = new ArrayList();
34 //The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.
35 // Software can go and grap these terms incl labels and description.
36 // UUID needed? Further vocs can be setup through our own ontology.
37 private String termSourceUri;
38 private Class termClass;
39
40
41 public TermVocabulary() {
42 super();
43 }
44 public TermVocabulary(String term, String label, String termSourceUri) {
45 super(term, label);
46 setTermSourceUri(termSourceUri);
47 }
48
49
50 public boolean isOrdinal(){
51 return this.isOrdinal;
52 }
53 public void setOrdinal(boolean isOrdinal){
54 this.isOrdinal = isOrdinal;
55 }
56
57
58 @OneToMany(mappedBy="vocabulary")
59 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
60 public List<DefinedTermBase> getTerms() {
61 return terms;
62 }
63 protected void setTerms(List<DefinedTermBase> terms) {
64 this.terms = terms;
65 }
66 public void addTerm(DefinedTermBase term) throws WrongTermTypeException {
67 if (terms.size()<1){
68 // no term yet in the list. First term defines the vocabulary kind
69 termClass=term.getClass();
70 }else if (term.getClass()!=termClass){
71 // check if new term in this vocabulary matches the previous ones
72 throw new WrongTermTypeException(term.getClass().getCanonicalName());
73 }
74 term.setVocabulary(this);
75 }
76 public void removeTerm(DefinedTermBase term) {
77 term.setVocabulary(null);
78 }
79
80
81 public List<DefinedTermBase> getPrecedingTerms(OrderedTermBase otb) {
82 // FIXME: need to return only OrderedTermBase lists
83 return terms.subList(0, terms.indexOf(otb));
84 }
85 public List<DefinedTermBase> getSucceedingTerms(OrderedTermBase otb) {
86 // FIXME: need to return only OrderedTermBase lists
87 return terms.subList(terms.indexOf(otb), terms.size());
88 }
89 public OrderedTermBase getPreviousTerm(OrderedTermBase otb) {
90 int idx = terms.indexOf(otb)-1;
91 return (OrderedTermBase)terms.get(idx);
92 }
93 public OrderedTermBase getNextTerm(OrderedTermBase otb) {
94 int idx = terms.indexOf(otb)+1;
95 return (OrderedTermBase)terms.get(idx);
96 }
97
98
99 public String getTermSourceUri() {
100 return termSourceUri;
101 }
102 public void setTermSourceUri(String vocabularyUri) {
103 this.termSourceUri = vocabularyUri;
104 }
105
106
107 protected Class getTermClass() {
108 return termClass;
109 }
110 protected void setTermClass(Class termClass) {
111 this.termClass = termClass;
112 }
113 }