some changes to the update script
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / InstitutionalMembership.java
index fe64d175c944245035ee71cff434765d6291de0f..6fccb6c9d5a8ca08159cb4b002ac0e7438f45de0 100644 (file)
 package eu.etaxonomy.cdm.model.agent;
 
 
-import eu.etaxonomy.cdm.model.common.TimePeriod;
-import eu.etaxonomy.cdm.model.common.VersionableEntity;
-import org.apache.log4j.Logger;
-import org.hibernate.annotations.Cascade;
-import org.hibernate.annotations.CascadeType;
-
-import javax.persistence.*;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToOne;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -25,6 +21,14 @@ import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;
 
+import org.apache.log4j.Logger;
+import org.hibernate.annotations.Cascade;
+import org.hibernate.annotations.CascadeType;
+import org.hibernate.envers.Audited;
+
+import eu.etaxonomy.cdm.model.common.TimePeriod;
+import eu.etaxonomy.cdm.model.common.VersionableEntity;
+
 /**
  * This class allows to hold one {@link Institution institution} to which a {@link Person person}
  * is affiliated. It includes {@link eu.etaxonomy.cdm.model.common.TimePeriod time period} of membership and role of
@@ -46,15 +50,14 @@ import javax.xml.bind.annotation.XmlType;
 })
 @XmlRootElement(name = "InstitutionalMembership")
 @Entity
-//@Audited
-public class InstitutionalMembership extends VersionableEntity {
+@Audited
+public class InstitutionalMembership extends VersionableEntity implements Cloneable{
        private static final long serialVersionUID = -800814712134999042L;
        public static final Logger logger = Logger.getLogger(InstitutionalMembership.class);
        
        /*Time period a person belonged to the institution*/
     @XmlElement(name = "Period")
-    //@XmlJavaTypeAdapter(IntervalAdapter.class)
-       private TimePeriod period;
+       private TimePeriod period = TimePeriod.NewInstance();
        
        //Department of the institution this person was working in
     @XmlElement(name = "Department")
@@ -68,11 +71,15 @@ public class InstitutionalMembership extends VersionableEntity {
     @XmlElement(name = "Institution", required = true)
     @XmlIDREF
     @XmlSchemaType(name = "IDREF")
+       @ManyToOne(fetch = FetchType.LAZY)
+       @Cascade(CascadeType.SAVE_UPDATE)
        private Institution institute;
        
     @XmlElement(name = "Person", required = true)
     @XmlIDREF
     @XmlSchemaType(name = "IDREF")
+    @ManyToOne(fetch = FetchType.LAZY)
+    @Cascade(CascadeType.SAVE_UPDATE)
        private Person person;
        
        public static InstitutionalMembership NewInstance() {
@@ -80,13 +87,14 @@ public class InstitutionalMembership extends VersionableEntity {
                return mship;
        }
 
-       public InstitutionalMembership() {
+       protected InstitutionalMembership() {
                super();
        }
 
        /** 
         * Class constructor using an {@link Institution institution}, a {@link Person person}, a {@link common.TimePeriod time period},
         * a department name string and a role string.
+        * Adds this membership to the persons memberships.
         *
         * @param  institute   the institution in which the person is a member
         * @param  person      the person who is a member of the institution
@@ -107,6 +115,7 @@ public class InstitutionalMembership extends VersionableEntity {
                this.role = role;
                this.institute = institute;
                this.person = person;
+               person.addInstitutionalMembership(this);
        }
        
        /** 
@@ -115,30 +124,26 @@ public class InstitutionalMembership extends VersionableEntity {
         * @see  Person#institutionalMemberships
         * @see  Person#addInstitutionalMembership(Institution, TimePeriod, String, String)
         */
-       @ManyToOne(fetch = FetchType.LAZY)
-       @Cascade({CascadeType.SAVE_UPDATE})
        public Person getPerson() {
                return person;
        }
-       /** 
+       
+       /**
         * Assigns a new {@link Person person} (replacing the actual one) to <i>this</i> institutional membership.
         * This method also updates both sets of institutions
-        * the two persons (the new one and the substituted one) belong to. 
+        * the two persons (the new one and the substituted one) belong to.
         *
         * @param  newPerson  the new person to be included in <i>this</i> institutional membership
         * @see               #getPerson()
         * @see               Person#removeInstitutionalMembership(InstitutionalMembership)
         */
-       protected void setPerson(Person newPerson) {
-               this.person = newPerson;
+       protected void setPerson(Person person) {
+               this.person = person;
        }
 
-       
        /** 
         * Returns the {@link Institution institution} corresponding to <i>this</i> institutional membership.
         */
-       @Cascade({CascadeType.SAVE_UPDATE})
-       @ManyToOne(fetch = FetchType.LAZY)
        public Institution getInstitute(){
                return this.institute;
        }
@@ -200,5 +205,26 @@ public class InstitutionalMembership extends VersionableEntity {
        public void setRole(String role){
                this.role = role;
        }
-
+       
+//*********************** CLONE ********************************************************/
+       
+       /** 
+        * Clones <i>this</i> InstitutionalMembership. This is a shortcut that enables to create
+        * a new instance that differs only slightly from <i>this</i> InstitutionalMembership.
+        *  
+        * @see eu.etaxonomy.cdm.model.common.VersionableEntity
+        * @see java.lang.Object#clone()
+        */
+       @Override
+       public Object clone() {
+               try{
+                       InstitutionalMembership result = (InstitutionalMembership) super.clone();
+                       //no changes to department, institute, period, person, role
+                       return result;
+               }catch (CloneNotSupportedException e){
+                       logger.warn("Object does not implement cloneable");
+                       e.printStackTrace();
+                       return null;
+               }
+       }
 }
\ No newline at end of file