hide term vocabulary constructors and create factory methods instead
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TermVocabulary.java
index d0abcc8d5c64eb0367ec8801f85f3b6c0033cae0..de46f8749b5b8b6102fcdc8e1687f82e0cee707c 100644 (file)
@@ -37,6 +37,10 @@ import org.apache.log4j.Logger;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
 import org.hibernate.annotations.Type;
+import org.hibernate.envers.Audited;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
 
 
 /**
@@ -53,29 +57,19 @@ import org.hibernate.annotations.Type;
 })
 @XmlRootElement(name = "TermVocabulary")
 @Entity
-//@Audited
+@Indexed(index = "eu.etaxonomy.cdm.model.common.TermVocabulary")
+@Audited
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
 public class TermVocabulary<T extends DefinedTermBase> extends TermBase implements Iterable<T> {
        private static final long serialVersionUID = 1925052321596648672L;
        @SuppressWarnings("unused")
        private static final Logger logger = Logger.getLogger(TermVocabulary.class);
 
-       
-       public T findTermByUuid(UUID uuid){
-               for(T t : terms) {
-                       if(t.getUuid().equals(uuid)) {
-                               return t;
-                       }
-               }
-               return null;
-       }
-       
-       
-
        //The vocabulary source (e.g. ontology) defining the terms to be loaded when a database is created for the first time.  
        // Software can go and grap these terms incl labels and description. 
        // UUID needed? Further vocs can be setup through our own ontology.
        @XmlElement(name = "TermSourceURI")
+       @Field(index=org.hibernate.search.annotations.Index.UN_TOKENIZED)
        private String termSourceUri;
        
 
@@ -84,39 +78,56 @@ public class TermVocabulary<T extends DefinedTermBase> extends TermBase implemen
        @XmlElement(name = "Term")
     @XmlIDREF
     @XmlSchemaType(name = "IDREF")
+    @OneToMany(mappedBy="vocabulary", fetch=FetchType.LAZY, targetEntity = DefinedTermBase.class)
+       @Type(type="DefinedTermBase")
+       @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
+       @IndexedEmbedded(depth = 2)
        protected Set<T> terms = getNewTermSet();
+       
+// ********************************* FACTORY METHODS *****************************************/
 
-       public TermVocabulary(String term, String label, String labelAbbrev, String termSourceUri) {
+       public static TermVocabulary NewInstance(String description, String label, String abbrev, String termSourceUri){
+               return new TermVocabulary(description, label, abbrev, termSourceUri);
+       }
+       
+// ************************* CONSTRUCTOR *************************************************     
+
+       protected TermVocabulary() {
+       }
+       
+       protected TermVocabulary(String term, String label, String labelAbbrev, String termSourceUri) {
                super(term, label, labelAbbrev);
                setTermSourceUri(termSourceUri);
        }
        
-       public TermVocabulary() {
-               // TODO Auto-generated constructor stub
-       }
+
+// ******************* METHODS *************************************************/      
        
+       public T findTermByUuid(UUID uuid){
+               for(T t : terms) {
+                       if(t.getUuid().equals(uuid)) {
+                               return t;
+                       }
+               }
+               return null;
+       }
+
        @Transient
        Set<T> getNewTermSet() {
                return new HashSet<T>();
        }
 
-       @OneToMany(mappedBy="vocabulary", fetch=FetchType.LAZY, targetEntity = DefinedTermBase.class)
-       @Type(type="DefinedTermBase")
-       @Cascade({CascadeType.SAVE_UPDATE})
        public Set<T> getTerms() {
                return terms;
        }
-       protected void setTerms(Set<T> terms) {
-               this.terms = terms;
-       }
        
        public void addTerm(T term) {
                term.setVocabulary(this);
                this.terms.add(term);
        }
        public void removeTerm(T term) {
-               term.setVocabulary(null);
                this.terms.remove(term);
+               term.setVocabulary(null);
        }
 
        public String getTermSourceUri() {
@@ -127,24 +138,6 @@ public class TermVocabulary<T extends DefinedTermBase> extends TermBase implemen
        }
        
        
-//     // inner iterator class for the iterable interface
-//     private class TermIterator<T> implements Iterator<T> {
-//                // FIXME: using a list here is probably not safe. Sth passed by value, an array, would be better
-//                // but arrays cause generics problems: http://forum.java.sun.com/thread.jspa?threadID=651276&messageID=3832182
-//                // hack for now ;(
-//                private Set<T> array;
-//                private int i= 0;
-//                // ctor
-//                public TermIterator(Set<T> array) {
-//                   // check for null being passed in etc.
-//                   this.array= array;
-//                }
-//                // interface implementation
-//                public boolean hasNext() { return i < array.size(); }
-//                public T next() { return array.get(i++); }
-//                public void remove() { throw new UnsupportedOperationException(); }
-//     }
-
        public Iterator<T> iterator() {
                return terms.iterator();  // OLD: new TermIterator<T>(this.terms);
        }
@@ -160,7 +153,6 @@ public class TermVocabulary<T extends DefinedTermBase> extends TermBase implemen
         * @param language
         * @return
         */
-       @Transient
        public SortedSet<T> getTermsOrderedByLabels(Language language){
                TermLanguageComparator<T> comp = new TermLanguageComparator<T>();
                comp.setCompareLanguage(language);
@@ -175,8 +167,9 @@ public class TermVocabulary<T extends DefinedTermBase> extends TermBase implemen
         * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
         */
        public TermVocabulary<T> readCsvLine(List<String> csvLine) {
-               return readCsvLine(csvLine, Language.ENGLISH());
+               return readCsvLine(csvLine, Language.CSV_LANGUAGE());
        }
+       
        public TermVocabulary<T> readCsvLine(List<String> csvLine, Language lang) {
                this.setUuid(UUID.fromString(csvLine.get(0)));
                this.setUri(csvLine.get(1));