(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / DefinedTermBase.java
index 241dabd1b364d12a22cb7d9e84be1ad049455b33..ede6c35935790abed8693671096dea74b6728f36 100644 (file)
@@ -12,37 +12,74 @@ package eu.etaxonomy.cdm.model.common;
 import org.apache.log4j.Logger;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.hibernate.collection.AbstractPersistentCollection;
 
 import au.com.bytecode.opencsv.CSVWriter;
 import eu.etaxonomy.cdm.model.common.init.DefaultVocabularyStore;
-import eu.etaxonomy.cdm.model.common.init.TermLoader;
 import eu.etaxonomy.cdm.model.common.init.IVocabularyStore;
+import eu.etaxonomy.cdm.model.media.Media;
+import eu.etaxonomy.cdm.model.name.Rank;
+
+import java.lang.reflect.Field;
 import java.util.*;
 
 import javax.persistence.*;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.bind.annotation.XmlType;
 
 
 /**
- * workaround for enumerations, base type according to TDWG.  For linear ordering
+ * walkaround for enumerations, base type according to TDWG.  For linear ordering
  * use partOf relation and BreadthFirst. Default iterator order should therefore
  * be BreadthFirst (not DepthFirst)
  * @author m.doering
  * @version 1.0
  * @created 08-Nov-2007 13:06:19
  */
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "DefinedTermBase", propOrder = {
+    "kindOf",
+    "generalizationOf",
+    "partOf",
+    "includes",
+    "media",
+    "vocabulary"
+})
+@XmlRootElement(name = "DefinedTermBase")
 @Entity
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
-public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements IDefTerm{
+public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBase implements ILoadableTerm{
        static Logger logger = Logger.getLogger(DefinedTermBase.class);
        
        static protected IVocabularyStore vocabularyStore = new DefaultVocabularyStore();
 
+       public static void setVocabularyStore(IVocabularyStore vocabularyStore){
+               DefinedTermBase.vocabularyStore = vocabularyStore;
+       }
+       
+       @XmlElement(name = "KindOf")
        private DefinedTermBase kindOf;
+       
+       @XmlElement(name = "GeneralizationOf")
        private Set<DefinedTermBase> generalizationOf = new HashSet<DefinedTermBase>();
+       
+       @XmlElement(name = "PartOf")
        private DefinedTermBase partOf;
+       
+       @XmlElementWrapper(name = "Includes")
+       @XmlElement(name = "Include")
        private Set<DefinedTermBase> includes = new HashSet<DefinedTermBase>();
+       
+       @XmlElementWrapper(name = "Media")
+       @XmlElement(name = "Medium")
        private Set<Media> media = new HashSet<Media>();
+       
+       @XmlElement(name = "Vocabulary")
        protected TermVocabulary<T> vocabulary;
        
 
@@ -53,25 +90,29 @@ public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBas
        public DefinedTermBase() {
                super();
        }
-       public DefinedTermBase(String term, String label) {
-               super(term, label);
+       public DefinedTermBase(String term, String label, String labelAbbrev) {
+               super(term, label, labelAbbrev);
        }
 
 
        /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.model.common.IDefTerm#readCsvLine(java.util.List)
+        * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#readCsvLine(java.util.List)
         */
-       public void readCsvLine(List<String> csvLine) {
-               readCsvLine(csvLine, Language.ENGLISH());
+       public ILoadableTerm readCsvLine(List<String> csvLine) {
+               return readCsvLine(csvLine, Language.ENGLISH());
        }
-       public void readCsvLine(List<String> csvLine, Language lang) {
+       public ILoadableTerm readCsvLine(List<String> csvLine, Language lang) {
                this.setUuid(UUID.fromString(csvLine.get(0)));
                this.setUri(csvLine.get(1));
-               this.addRepresentation(new Representation(csvLine.get(3), csvLine.get(2).trim(), lang) );
+               String label = csvLine.get(2).trim();
+               String text = csvLine.get(3);
+               String abbreviatedLabel = null;
+               this.addRepresentation(Representation.NewInstance(text, label, abbreviatedLabel, lang) );
+               return this;
        }
 
        /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.model.common.IDefTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
+        * @see eu.etaxonomy.cdm.model.common.ILoadableTerm#writeCsvLine(au.com.bytecode.opencsv.CSVWriter)
         */
        public void writeCsvLine(CSVWriter writer) {
                String [] line = new String[4];
@@ -145,12 +186,11 @@ public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBas
                this.media.remove(media);
        }
 
-       
        /* (non-Javadoc)
         * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
         */
-       @ManyToOne(fetch=FetchType.EAGER)
-       @Cascade( { CascadeType.SAVE_UPDATE })
+       @Transient
+       @XmlTransient
        public TermVocabulary getVocabulary() {
                return this.vocabulary;
        }
@@ -170,4 +210,50 @@ public abstract class DefinedTermBase<T extends DefinedTermBase> extends TermBas
                this.vocabulary = newVocabulary;                
        }
        
+       
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.common.IDefTerm#getVocabulary()
+        */
+       @ManyToOne(fetch=FetchType.LAZY)
+       @Cascade( { CascadeType.SAVE_UPDATE })
+       protected TermVocabulary getPersistentVocabulary() {
+               return this.vocabulary;
+       }
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.cdm.model.common.IDefTerm#setVocabulary(eu.etaxonomy.cdm.model.common.TermVocabulary)
+        */
+       protected void setPersistentVocabulary(TermVocabulary newVocabulary) {
+               // Hibernate bidirectional cascade hack: 
+               // http://opensource.atlassian.com/projects/hibernate/browse/HHH-1054
+               if(this.vocabulary == newVocabulary){ return;}
+               if (this.vocabulary != null) { 
+                       this.vocabulary.terms.remove(this);
+               }
+               if (newVocabulary!= null) { 
+                       try {
+                               Field fieldInitializing = AbstractPersistentCollection.class.getDeclaredField("initializing");
+                               fieldInitializing.setAccessible(true);
+                               if (AbstractPersistentCollection.class.isAssignableFrom(newVocabulary.terms.getClass())){
+                                       boolean initValue = fieldInitializing.getBoolean(newVocabulary.terms);
+                                       if (initValue == false){
+                                               newVocabulary.terms.add(this);
+                                       }else{
+                                               //nothing
+                                       }
+                               }else{
+                                       newVocabulary.terms.add(this);
+                               }
+                       } catch (SecurityException e) {
+                               e.printStackTrace();
+                       } catch (IllegalArgumentException e) {
+                               e.printStackTrace();
+                       } catch (NoSuchFieldException e) {
+                               e.printStackTrace();
+                       } catch (IllegalAccessException e) {
+                               e.printStackTrace();
+                       }
+               }
+               this.vocabulary = newVocabulary;                
+       }
+       
 }
\ No newline at end of file