clone methods
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / Person.java
index b7f48c914e1d1af62037e030999fbadd3fff6b7e..26bfe50b26527eba9cef73b91a50e3186f068e44 100644 (file)
@@ -15,6 +15,8 @@ import java.util.Set;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.OneToMany;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -30,12 +32,15 @@ import org.hibernate.search.annotations.Field;
 import org.hibernate.search.annotations.Index;
 import org.hibernate.search.annotations.Indexed;
 import org.hibernate.search.annotations.IndexedEmbedded;
+import org.joda.time.Partial;
 import org.springframework.beans.factory.annotation.Configurable;
 
 import eu.etaxonomy.cdm.model.common.TimePeriod;
+import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.cdm.strategy.cache.agent.PersonDefaultCacheStrategy;
 import eu.etaxonomy.cdm.strategy.match.Match;
 import eu.etaxonomy.cdm.strategy.match.MatchMode;
+import eu.etaxonomy.cdm.validation.annotation.NullOrNotEmpty;
 
 /**
  * This class represents human beings, living or dead.<BR>
@@ -65,7 +70,7 @@ import eu.etaxonomy.cdm.strategy.match.MatchMode;
            "suffix",
            "lifespan",
            "institutionalMemberships"
-       })
+})
 @XmlRootElement(name = "Person")
 @Entity
 @Indexed(index = "eu.etaxonomy.cdm.model.agent.AgentBase")
@@ -77,30 +82,39 @@ public class Person extends TeamOrPersonBase<Person>{
 
     @XmlElement(name = "Prefix")
     @Field(index=Index.TOKENIZED)
+    @NullOrNotEmpty
+    @Size(max = 255)
        private String prefix;
     
     @XmlElement(name = "FirstName")
     @Field(index=Index.TOKENIZED)
+    @NullOrNotEmpty
+    @Size(max = 255)
        private String firstname;
        
     @XmlElement(name = "LastName")
     @Field(index=Index.TOKENIZED)
+    @NullOrNotEmpty
+    @Size(max = 255)
        private String lastname;
        
     @XmlElement(name = "Suffix")
     @Field(index=Index.TOKENIZED)
+    @NullOrNotEmpty
+    @Size(max = 255)
        private String suffix;
        
     @XmlElement(name = "Lifespan")
     @IndexedEmbedded
     @Match(value=MatchMode.EQUAL_OR_ONE_NULL)
+    @NotNull
        private TimePeriod lifespan = TimePeriod.NewInstance();
        
-    @XmlElementWrapper(name = "InstitutionalMemberships")
+    @XmlElementWrapper(name = "InstitutionalMemberships", nillable = true)
     @XmlElement(name = "InstitutionalMembership")
     @OneToMany(fetch=FetchType.LAZY, mappedBy = "person")
        @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
-       protected Set<InstitutionalMembership> institutionalMemberships = new HashSet<InstitutionalMembership>();
+       protected Set<InstitutionalMembership> institutionalMemberships;
 
        /** 
         * Creates a new empty instance for a person whose existence is all what is known.
@@ -122,7 +136,7 @@ public class Person extends TeamOrPersonBase<Person>{
         */
        public static Person NewTitledInstance(String titleCache){
                Person result = new Person();
-               result.setTitleCache(titleCache);
+               result.setTitleCache(titleCache, true);
                return result;
        }
        
@@ -153,9 +167,9 @@ public class Person extends TeamOrPersonBase<Person>{
        public Person(String firstname, String lastname, String nomenclaturalTitel) {
                this.setFirstname(firstname);
                this.setLastname(lastname);
-               System.err.println("before - Set nomenclatural Title");
+               logger.debug("before - Set nomenclatural Title");
                this.setNomenclaturalTitle(nomenclaturalTitel);
-               System.err.println("after - Set nomenclatural Title");
+               logger.debug("after - Set nomenclatural Title");
        }
        
        
@@ -165,11 +179,14 @@ public class Person extends TeamOrPersonBase<Person>{
         * @see     InstitutionalMembership
         */
        public Set<InstitutionalMembership> getInstitutionalMemberships(){
+               if(institutionalMemberships == null) {
+                       this.institutionalMemberships = new HashSet<InstitutionalMembership>();
+               }
                return this.institutionalMemberships;
        }
 
        protected void addInstitutionalMembership(InstitutionalMembership ims){
-               this.institutionalMemberships.add(ims);
+               getInstitutionalMemberships().add(ims);
                if (ims.getPerson() != this){
                        logger.warn("Institutional membership's person has to be changed for adding it to person: " + this);
                        ims.getPerson().removeInstitutionalMembership(ims);
@@ -206,7 +223,7 @@ public class Person extends TeamOrPersonBase<Person>{
        public void removeInstitutionalMembership(InstitutionalMembership ims){
                ims.setInstitute(null);
                ims.setPerson(null);
-               this.institutionalMemberships.remove(ims);
+               getInstitutionalMemberships().remove(ims);
        }
 
        /**
@@ -285,6 +302,9 @@ public class Person extends TeamOrPersonBase<Person>{
         * @see  eu.etaxonomy.cdm.model.common.TimePeriod
         */
        public TimePeriod getLifespan(){
+               if(lifespan == null) {
+                       this.lifespan = TimePeriod.NewInstance(new Partial(), new Partial());
+               }
                return this.lifespan;
        }
        /**
@@ -313,5 +333,29 @@ public class Person extends TeamOrPersonBase<Person>{
 //             } 
 //        return title;
 //     }
+       
+//*********************** CLONE ********************************************************/
+       
+       /** 
+        * Clones <i>this</i> Person. This is a shortcut that enables to create
+        * a new instance that differs only slightly from <i>this</i> Person.
+        *  
+        * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
+        * @see java.lang.Object#clone()
+        */
+       @Override
+       public Object clone() {
+               try{
+                       Person result = (Person)super.clone();
+                       //no changes to firstname, lastname, lifespan, prefix, suffix
+                       return result;
+               } catch (CloneNotSupportedException e){
+                       logger.warn("Object does not implement cloneable");
+                       e.printStackTrace();
+                       return null;
+               }
+               
+               
+       }
 
 }
\ No newline at end of file