cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / term / DefinedTermBase.java
index 26806d3dcebee5540f6da2808700429f29cdb69d..bba872d05efe054e54c28e6890c951ca96198824 100644 (file)
@@ -10,7 +10,6 @@
 package eu.etaxonomy.cdm.model.term;
 
 import java.lang.reflect.Constructor;
-import java.net.URI;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -37,7 +36,8 @@ import javax.xml.bind.annotation.XmlSeeAlso;
 import javax.xml.bind.annotation.XmlTransient;
 import javax.xml.bind.annotation.XmlType;
 
-import org.apache.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.hibernate.annotations.Cascade;
 import org.hibernate.annotations.CascadeType;
 import org.hibernate.envers.Audited;
@@ -47,10 +47,13 @@ import org.hibernate.search.annotations.ClassBridge;
 
 import au.com.bytecode.opencsv.CSVWriter;
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.common.URI;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.hibernate.search.DefinedTermBaseClassBridge;
 import eu.etaxonomy.cdm.model.ICdmUuidCacher;
 import eu.etaxonomy.cdm.model.common.AnnotationType;
 import eu.etaxonomy.cdm.model.common.ExtensionType;
+import eu.etaxonomy.cdm.model.common.ExternallyManaged;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.description.Feature;
@@ -80,6 +83,7 @@ import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
     "idInVocabulary",
     "symbol",
     "symbol2",
+    "externallyManaged",
 })
 @XmlRootElement(name = "DefinedTermBase")
 @XmlSeeAlso({
@@ -109,7 +113,7 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
             implements IDefinedTerm<T>, Comparable<T> {
 
     private static final long serialVersionUID = 2931811562248571531L;
-    private static final Logger logger = Logger.getLogger(DefinedTermBase.class);
+    private static final Logger logger = LogManager.getLogger();
 
 //     @XmlElement(name = "KindOf")
 //    @XmlIDREF
@@ -118,6 +122,7 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
     @ManyToOne(fetch = FetchType.LAZY, targetEntity = DefinedTermBase.class)
     @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
     private T kindOf;
+
     /**
      * FIXME - Hibernate returns this as a collection of CGLibProxy$$DefinedTermBase objects
      * which can't be cast to instances of T - can we explicitly initialize these terms using
@@ -190,20 +195,26 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
     //empty string is explicitly allowed and should be distinguished from NULL!
     private String symbol2;
 
+    private ExternallyManaged externallyManaged;
+
 //***************************** CONSTRUCTOR *******************************************/
 
-    //for javassit only
+    //for hibernate use only, *packet* private required by bytebuddy
+    //2022-06-17: currently still needed protected as TaxEditor.TaxonRelationshipTypeInverseContainer inherits from DefinedTermBase
     @Deprecated
-    protected DefinedTermBase(){};
+    protected DefinedTermBase(){}
 
     protected DefinedTermBase(TermType type) {
         super(type);
     }
+
+    public DefinedTermBase(TermType type, String description, String label, String labelAbbrev, Language lang) {
+        super(type, description, label, labelAbbrev, lang);
+    }
     public DefinedTermBase(TermType type, String description, String label, String labelAbbrev) {
-        super(type, description, label, labelAbbrev);
+        super(type, description, label, labelAbbrev, null);
     }
 
-
 //********************** GETTER /SETTER *************************************
 
       @Override
@@ -213,13 +224,11 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
 
       @Override
       public void setIdInVocabulary(String idInVocabulary) {
-
           this.idInVocabulary = CdmUtils.isBlank(idInVocabulary)? null : idInVocabulary;
       }
 
       @Override
       public T getKindOf(){
-
           if (this instanceof HibernateProxy) {
               HibernateProxy proxy = (HibernateProxy) this;
               LazyInitializer li = proxy.getHibernateLazyInitializer();
@@ -233,23 +242,18 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
           this.kindOf = kindOf;
       }
 
-
       @Override
       public Set<T> getGeneralizationOf(){
           return this.generalizationOf;
       }
-
       protected void setGeneralizationOf(Set<T> value) {
           this.generalizationOf = value;
       }
-
       public void addGeneralizationOf(T generalization) {
           checkTermType(generalization);
           generalization.setKindOf(this);
           this.generalizationOf.add(generalization);
       }
-
-
       public void removeGeneralization(T generalization) {
           if(generalizationOf.contains(generalization)){
               generalization.setKindOf(null);
@@ -326,55 +330,40 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
           this.media.remove(media);
       }
 
-      /**
-       * @return
-       */
       public TermVocabulary<T> getVocabulary() {
           return this.vocabulary;
       }
 
       //for bedirectional use only, use vocabulary.addTerm instead
-      /**
-       * @param newVocabulary
-       */
       protected void setVocabulary(TermVocabulary<T> newVocabulary) {
           this.vocabulary = newVocabulary;
     }
 
-
     public String getSymbol() {
         return symbol;
     }
     public void setSymbol(String symbol) {
         this.symbol = symbol;
     }
-    /**
-     * @return the symbol2
-     */
+
     public String getSymbol2() {
         return symbol2;
     }
-
-    /**
-     * @param symbol2 the symbol2 to set
-     */
     public void setSymbol2(String symbol2) {
         this.symbol2 = symbol2;
     }
 
 //******************************* METHODS ******************************************************/
 
-
-
     @Override
       public boolean isKindOf(T ancestor) {
           if (kindOf == null || ancestor == null){
-            return false;
-        }else if (kindOf.equals(ancestor)){
-            return true;
-        }else{
-            return kindOf.isKindOf(ancestor);
-        }
+              return false;
+          }else if (kindOf.equals(ancestor)){
+              return true;
+          }else{
+              return kindOf.isKindOf(ancestor);
+          }
       }
 
       @Override
@@ -389,13 +378,10 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
         return result;
       }
 
-
-
     public abstract void resetTerms();
 
     protected abstract void setDefaultTerms(TermVocabulary<T> termVocabulary);
 
-
     @Override
     public T readCsvLine(Class<T> termClass, List<String> csvLine, TermType termType, Map<UUID,DefinedTermBase> terms, boolean abbrevAsId) {
         try {
@@ -440,18 +426,12 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
                  partOf.addIncludes(newInstance);
              }
          }
-
     }
 
-    /**
-     * Get the
-     * @return
-     */
     protected int partOfCsvLineIndex() {
         return -1;
     }
 
-
     private  <T extends DefinedTermBase> T getInstance(Class<? extends DefinedTermBase> termClass, TermType termType) {
         try {
             Constructor<T> c = ((Class<T>)termClass).getDeclaredConstructor();
@@ -500,34 +480,33 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
      * @see java.lang.Object#clone()
      */
     @Override
-    public Object clone() {
-        DefinedTermBase result;
+    public DefinedTermBase<T> clone() {
         try {
-            result = (DefinedTermBase) super.clone();
-        }catch (CloneNotSupportedException e) {
-            logger.warn("Object does not implement cloneable");
-            e.printStackTrace();
-            return null;
-        }
+            DefinedTermBase<T> result = (DefinedTermBase<T>) super.clone();
 
-        result.generalizationOf = new HashSet<DefinedTermBase>();
-        for (DefinedTermBase generalizationOf : this.generalizationOf){
-            result.generalizationOf.add(generalizationOf.clone());
-        }
+            result.generalizationOf = new HashSet<>();
+            for (DefinedTermBase<T> generalizationOf : this.generalizationOf){
+                result.generalizationOf.add((T)generalizationOf.clone());
+            }
 
-        result.includes = new HashSet<DefinedTermBase>();
+            result.includes = new HashSet<>();
 
-        for (DefinedTermBase include: this.includes){
-            result.includes.add(include.clone());
-        }
+            for (DefinedTermBase<?> include: this.includes){
+                result.includes.add((T)include.clone());
+            }
 
-        result.media = new HashSet<Media>();
+            result.media = new HashSet<>();
 
-        for (Media media: this.media){
-            result.addMedia(media);
-        }
+            for (Media media: this.media){
+                result.addMedia(media);
+            }
 
-        return result;
+            return result;
+        }catch (CloneNotSupportedException e) {
+            logger.warn("Object does not implement cloneable");
+            e.printStackTrace();
+            return null;
+        }
     }
 
     // Currently the CDM Caching mechanism is only used for caching terms
@@ -554,11 +533,12 @@ public abstract class DefinedTermBase<T extends DefinedTermBase>
 
        public static <T extends DefinedTermBase> T getTermByClassAndUUID(Class<T> clazz, UUID uuid) {
            if(cacher != null) {
-               Object obj = getCacher().load(uuid);
+               Object obj = HibernateProxyHelper.deproxy(getCacher().load(uuid));
+
                if(obj != null && obj.getClass().equals(clazz)) {
                    return (T)obj;
                }
            }
            return null;
        }
-}
+}
\ No newline at end of file