Cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / VersionableEntity.java
index eca5b95bc862465aeea3031f158e099702513944..bf8dbf1db85a1e58e4f905677400f45cbc64b633 100644 (file)
@@ -1,47 +1,54 @@
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
 
 package eu.etaxonomy.cdm.model.common;
 
-import java.util.Calendar;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
 
 import javax.persistence.Basic;
 import javax.persistence.FetchType;
 import javax.persistence.ManyToOne;
 import javax.persistence.MappedSuperclass;
-import javax.persistence.Temporal;
-import javax.persistence.TemporalType;
-import javax.persistence.Transient;
-import javax.persistence.Version;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.bind.annotation.XmlIDREF;
+import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
 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.Analyze;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.FieldBridge;
+import org.joda.time.DateTime;
 
-import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
+import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
+import eu.etaxonomy.cdm.strategy.match.Match;
+import eu.etaxonomy.cdm.strategy.match.MatchMode;
 
 /**
  * The class keeps track of versions via a full linked list to different version objects, or a simple updated/updatedBy property in the same object.
- * 
+ *
  * Full versioning allows concrete subclasses to keep track of previous or later versions of an object.
- * A different version is another (persistent) java object, but with the same UUID. 
+ * A different version is another (persistent) java object, but with the same UUID.
  * The version history is established as a linked list of the version objects in time.
  * If versioning via the linked list is used, updated/updatedBy is the same as created/createdBy (better NULL?).
- * 
+ *
  * Versioning can be turned off and in this case this class provides updated/updatedBy to keep track of the latest change event.
- * 
+ *
  * @author m.doering
  * @created 08-Nov-2007 13:07:01
  *
@@ -49,79 +56,73 @@ import eu.etaxonomy.cdm.model.agent.Person;
  */
 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "VersionableEntity", propOrder = {
-//    "updated",
+    "updated",
     "updatedBy"
 })
-@XmlRootElement(name = "VersionableEntity")
+@XmlJavaTypeAdapter(value=DateTimeAdapter.class,type=DateTime.class)
 @MappedSuperclass
-public abstract class VersionableEntity extends CdmBase {
+@Audited
+public abstract class VersionableEntity extends CdmBase implements IVersionableEntity{
        private static final long serialVersionUID = 1409299200302758513L;
        @SuppressWarnings("unused")
        private static final Logger logger = Logger.getLogger(VersionableEntity.class);
-       
-       //time of last update for this object
-       // There is a problem with "updated" during deserialization because of the @Version annotation.
-       @XmlTransient
+
+       @XmlElement(name ="Updated", type = String.class)
+       @XmlJavaTypeAdapter(DateTimeAdapter.class)
        //@XmlElement(name ="Updated")
-       private Calendar updated;
-       
-       @XmlElement(name = "UpdatedBy")
-       private Person updatedBy;
+       //@XmlElement(name ="Updated")
+       @Type(type="dateTimeUserType")
+       @Basic(fetch = FetchType.LAZY)
+       @Match(MatchMode.IGNORE)
+       @Field(analyze = Analyze.NO)
+       @FieldBridge(impl = DateTimeBridge.class)
+       private DateTime updated;
 
+       @XmlElement(name = "UpdatedBy")
+       @XmlIDREF
+       @XmlSchemaType(name = "IDREF")
        @ManyToOne(fetch=FetchType.LAZY)
-       @Cascade({CascadeType.SAVE_UPDATE})
-       public Person getUpdatedBy(){
+       @Match(MatchMode.IGNORE)
+       private User updatedBy;
+
+       @Override
+    public User getUpdatedBy(){
                return this.updatedBy;
        }
 
        /**
-        * 
+        *
         * @param updatedBy    updatedBy
         */
-       public void setUpdatedBy(Person updatedBy){
+       @Override
+    public void setUpdatedBy(User updatedBy){
                this.updatedBy = updatedBy;
        }
 
        /**
-        * 
+        *
         * @return
         */
-       @Temporal(TemporalType.TIMESTAMP)
-       @Basic(fetch = FetchType.LAZY)
-       public Calendar getUpdated(){
+       @Override
+    public DateTime getUpdated(){
                return this.updated;
        }
 
        /**
-        * 
+        *
         * @param updated    updated
         */
-       public void setUpdated(Calendar updated){
+       @Override
+    public void setUpdated(DateTime updated){
                this.updated = updated;
        }
 
-       /**
-        * based on created
-        */
-       @Transient
-       public Calendar getValidFrom(){
-               return null;
-       }
-
-       /**
-        * based on updated
-        */
-       @Transient
-       public Calendar getValidTo(){
-               return null;
-       }
-       
        /**
         * Is true if UUID and created timestamp are the same for the passed Object and this one.
         * @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
-        * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities} 
+        * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
         * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
-        * for more information about equals and hashcode. 
+        * for more information about equals and hashcode.
         */
        @Override
        public boolean equals(Object obj) {
@@ -135,31 +136,24 @@ public abstract class VersionableEntity extends CdmBase {
                        return false;
                }
                ICdmBase cdmObj = (ICdmBase)obj;
-               boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
-               boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
+               boolean uuidEqual;
+               UUID objUuid = cdmObj.getUuid();
+               if (objUuid == null){
+                       throw new NullPointerException("CdmBase is missing UUID");
+               }
+               uuidEqual = objUuid.equals(this.getUuid());
+               //TODO is this still needed?
+               boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
                if (! uuidEqual || !createdEqual){
                                return false;
                }
                return true;
        }
 
-       
-       /** Overrides {@link eu.etaxonomy.cdm.model.common.CdmBase#hashCode()}
-        *  See {@link http://www.hibernate.org/109.html}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html} 
-        * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html}
-        * for more information about equals and hashcode. 
-        */
-        @Override
-       public int hashCode() {
-                  int hashCode = 7;
-                  hashCode = 29 * hashCode + this.getUuid().hashCode();
-                  //hashCode = 29 * hashCode + this.getCreated().hashCode();
-                  return hashCode;
-       }
-        
+
 //********************** CLONE *****************************************/
-       
-       /** 
+
+       /**
         * Clones this versionable entity.
         * Set fields for nextVersion, previousVersion, updated, updatedBy and createdBy are set to <tt>null</tt>
         * The id is set to 0.
@@ -170,11 +164,27 @@ public abstract class VersionableEntity extends CdmBase {
        @Override
        public Object clone() throws CloneNotSupportedException{
                VersionableEntity result = (VersionableEntity)super.clone();
-               
+
                result.setUpdated(null);
                result.setUpdatedBy(null);
-               
+
                //no changes to: -
                return result;
        }
+
+
+    /**
+     * Convenience method to clone a LanguageString map
+     * @param oldMap
+     * @return
+     * @throws CloneNotSupportedException
+     */
+    protected Map<Language,LanguageString> cloneLanguageString(Map<Language,LanguageString> oldMap) throws CloneNotSupportedException{
+        Map<Language,LanguageString> result = new HashMap<>();
+        for (Language language : oldMap.keySet()){
+            LanguageString newLanguageString = (LanguageString)oldMap.get(language).clone();
+            result.put(language, newLanguageString);
+        }
+        return result;
+    }
 }