cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / ref / TypedEntityReference.java
index 3c2b30650ba9837e987f977825657d6eabc4aefd..478ffa52d2d082f5dd5ee681afccb9395ef30947 100644 (file)
@@ -12,31 +12,77 @@ import java.util.UUID;
 
 import org.apache.commons.lang3.builder.HashCodeBuilder;
 
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+
 /**
  * @author a.kohlbecker
  * @since Jun 12, 2017
- *
  */
-public class TypedEntityReference<T> extends EntityReference {
+public class TypedEntityReference<T extends CdmBase> extends EntityReference {
 
     private static final long serialVersionUID = -4619590272174606288L;
 
     private Class<T> type;
 
     /**
-     * @param uuid
-     * @param label
+     * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
      */
+    @Deprecated
     public TypedEntityReference(Class<T> type, UUID uuid, String label) {
         super(uuid, label);
         this.type = type;
     }
 
+    /**
+     * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO: to be made protected once no longer used publicly)
+     */
+    @Deprecated
+    public TypedEntityReference(T entity) {
+        super();
+        this.type = (Class<T>) entity.getClass();
+        this.uuid = entity.getUuid();
+    }
+
+    /**
+     * @deprecated use factory method instead, should only be used by in DTO sub-class constructors (TODO; to be made protected once no longer used publicly)
+     */
+    @Deprecated
     public TypedEntityReference(Class<T> type, UUID uuid) {
         super(uuid, null);
         this.type = type;
     }
 
+    public static  <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity, boolean withLabel) {
+        if(entity == null) {
+            return null;
+        }
+        entity = HibernateProxyHelper.deproxy(entity);
+        if(withLabel && IdentifiableEntity.class.isAssignableFrom(entity.getClass())) {
+            return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid(), ((IdentifiableEntity)entity).getTitleCache());
+        } else {
+            return new TypedEntityReference<>((Class<T>)entity.getClass(), entity.getUuid());
+        }
+    }
+
+    /**
+     * Casts the <code>TypedEntityReference</code> to the <code>subType</code> if possible.
+     *
+     * @throws ClassCastException
+     *  If the {@link #type} is not a super type of <code>subType</code>.
+     */
+    public <S extends CdmBase> TypedEntityReference<S> castTo(Class<S> subType){
+        if(!type.isAssignableFrom(subType)) {
+            throw new ClassCastException("Cannot cast " + type.getName() + " to " + subType.getName());
+        }
+        return new TypedEntityReference<>(subType, getUuid());
+    }
+
+    public static  <T extends CdmBase> TypedEntityReference<T> fromEntity(T entity) {
+        return TypedEntityReference.fromEntity(entity, true);
+    }
+
     public Class<T> getType() {
         return type;
     }
@@ -44,9 +90,6 @@ public class TypedEntityReference<T> extends EntityReference {
         this.type = type;
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @Override
     public int hashCode() {
         return new HashCodeBuilder(17, 31)
@@ -55,9 +98,6 @@ public class TypedEntityReference<T> extends EntityReference {
                 .hashCode();
     }
 
-    /**
-     * {@inheritDoc}
-     */
     @SuppressWarnings("rawtypes")
     @Override
     public boolean equals(Object obj) {
@@ -73,7 +113,5 @@ public class TypedEntityReference<T> extends EntityReference {
     @Override
     public String toString(){
         return type.getSimpleName() + "#" + uuid;
-
     }
-
-}
+}
\ No newline at end of file