merge cdmlib-3.4 branch into trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / agent / Institution.java
index e2e6dcef4785bf7219ce9790e84a0ef4b5b57f55..3d72ac602f0105c1701dcdf8ebcaf3369b9044b4 100644 (file)
@@ -1,8 +1,8 @@
 /**
 * 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.agent;
 
 
-import org.apache.log4j.Logger;
-import org.hibernate.annotations.Cascade;
-import org.hibernate.annotations.CascadeType;
+import java.util.HashSet;
+import java.util.Set;
 
-import java.util.*;
-import javax.persistence.*;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.ManyToMany;
+import javax.persistence.ManyToOne;
+import javax.validation.constraints.Size;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -25,6 +27,18 @@ 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 org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.springframework.beans.factory.annotation.Configurable;
+
+import eu.etaxonomy.cdm.model.common.DefinedTerm;
+import eu.etaxonomy.cdm.strategy.cache.agent.InstitutionDefaultCacheStrategy;
+import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
+
 /**
  * This class represents public or private institutions.
  * It includes name, contact details and institution type.
@@ -34,7 +48,7 @@ import javax.xml.bind.annotation.XmlType;
  * <li> Institution according to the TCS
  * <li> Organisation (Institution) according to the ABCD schema
  * </ul>
- * 
+ *
  * @author m.doering
  * @version 1.0
  * @created 08-Nov-2007 13:06:29
@@ -44,36 +58,44 @@ import javax.xml.bind.annotation.XmlType;
        "code",
        "name",
        "types",
-       "isPartOf",
-       "contact"
+       "isPartOf"
 })
 @XmlRootElement(name = "Institution")
 @Entity
-//@Audited
-public class Institution extends Agent {
+@Indexed(index = "eu.etaxonomy.cdm.model.agent.AgentBase")
+@Audited
+@Configurable
+public class Institution extends AgentBase<IIdentifiableEntityCacheStrategy<Institution>> {
        private static final long serialVersionUID = -951321271656955808L;
        public static final Logger logger = Logger.getLogger(Institution.class);
-       
+
     @XmlElement(name = "Code")
+    @Field
+    //TODO Val #3379
+//    @NullOrNotEmpty
+    @Size(max = 255)
        private String code;
-       
+
     @XmlElement(name = "Name")
+    @Field
+//TODO Val #3379
+//    @NullOrNotEmpty
+    @Size(max = 255)
        private String name;
-       
-    @XmlElementWrapper(name = "Types")
+
+    @XmlElementWrapper(name = "Types", nillable = true)
     @XmlElement(name = "Type")
-    //@XmlIDREF
-    //@XmlSchemaType(name = "IDREF")
-       private Set<InstitutionType> types = new HashSet<InstitutionType>();
-       
+    @XmlIDREF
+    @XmlSchemaType(name = "IDREF")
+    @ManyToMany(fetch = FetchType.LAZY)
+       private Set<DefinedTerm> types;  //InstitutionTypes
+
     @XmlElement(name = "IsPartOf")
     @XmlIDREF
     @XmlSchemaType(name = "IDREF")
+    @ManyToOne(fetch = FetchType.LAZY)
+    @Cascade(CascadeType.SAVE_UPDATE)
        private Institution isPartOf;
-       
-    // TODO: Move to Agent
-    @XmlElement(name = "Contact")
-       private Contact contact;
 
        /**
         * Creates a new empty institution instance.
@@ -81,93 +103,68 @@ public class Institution extends Agent {
        public static Institution NewInstance(){
                return new Institution();
        }
-       
-       
-       /** 
+
+
+       /**
         * Class constructor.
         */
        public Institution() {
                super();
+               this.cacheStrategy = new InstitutionDefaultCacheStrategy();
        }
 
-       /** 
-        * Returns the {@link Contact contact} corresponding to <i>this</i> institution.
-        * It includes telecommunication data
-        * and electronic as well as multiple postal addresses.
-        */
-       // TODO: Move to Agent, mark as @OneToOne
-       @ManyToOne
-       @Cascade({CascadeType.SAVE_UPDATE})
-       public Contact getContact(){
-               return this.contact;
-       }
-       /** 
-        * @see  #getContact()
-        */
-       public void setContact(Contact contact){
-               this.contact = contact;
-       }
-
-       /** 
-        * Returns the set of institution {@link InstitutionType types} (categories)
+       /**
+        * Returns the set of institution types (categories)
         * used to describe or circumscribe <i>this</i> institution's activities.
         * Institution types are items of a controlled {@link eu.etaxonomy.cdm.model.common.TermVocabulary vocabulary}.
         *
         * @return      the set of institution types
-        * @see     InstitutionType
         */
-       @ManyToMany(fetch = FetchType.LAZY)
-       public Set<InstitutionType> getTypes(){
+       public Set<DefinedTerm> getTypes(){
+               if(types == null) {
+                       this.types = new HashSet<DefinedTerm>();
+               }
                return this.types;
        }
-       
-       /** 
+
+       /**
         * Adds a new institutional type (from the corresponding {@link eu.etaxonomy.cdm.model.common.TermVocabulary vocabulary})
         * to the set of institution types of <i>this</i> institution.
         *
         * @param  t  any type of institution
         * @see           #getTypes()
-        * @see           InstitutionType
         */
-       public void addType(InstitutionType t){
-               this.types.add(t);
+       public void addType(DefinedTerm type){
+               getTypes().add(type);
        }
-       
-       /** 
+
+       /**
         * Removes one element from the set of institution types for <i>this</i> institution.
         *
         * @param  t  the institution type which should be deleted
         * @see       #getTypes()
         */
-       public void removeType(InstitutionType t){
-               this.types.remove(t);
-       }
-       /** 
-        * @see     #getTypes()
-        */
-       protected void setTypes(Set<InstitutionType> types){
-               this.types = types;
+       public void removeType(DefinedTerm type){
+               getTypes().remove(type);
        }
 
-
-       /** 
+       /**
         * Returns the parent institution of this institution.
         * This is for instance the case when this institution is a herbarium
         * belonging to a parent institution such as a museum.
         */
-       @ManyToOne
-       @Cascade({CascadeType.SAVE_UPDATE})
        public Institution getIsPartOf(){
                return this.isPartOf;
        }
-       /** 
+
+       /**
         * Assigns a parent institution to which this institution belongs.
         *
         * @param  isPartOf  the parent institution
         * @see    #getIsPartOf()
         */
-       public void setIsPartOf(Institution isPartOf){
-               this.isPartOf = isPartOf;
+       public void setIsPartOf(Institution parentInstitution){
+               this.isPartOf = parentInstitution;
        }
 
        /**
@@ -177,41 +174,47 @@ public class Institution extends Agent {
        public String getCode(){
                return this.code;
        }
-       /** 
+       /**
         * @see    #getCode()
         */
        public void setCode(String code){
                this.code = code;
        }
 
-       
-       /** 
+
+       /**
         * Returns the full name, as distinct from a code, an acronym or initials,
         * by which this institution is generally known.
         */
        public String getName(){
                return this.name;
        }
-       /** 
+       /**
         * @see    #getName()
         */
        public void setName(String name){
                this.name = name;
        }
 
+//*********************** CLONE ********************************************************/
+
        /**
-        * Generates the identification string for this institution.
-        * The string is based on its name and code as well as on the name and code of
-        * its parent institution, if existing.
-        * This method overrides {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#generateTitle() generateTitle}.
-        * The result might be kept as {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#setTitleCache(String) titleCache} if the
-        * flag {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity#protectedTitleCache protectedTitleCache} is not set.
-        * 
-        * @return  the identification string
+        * Clones <i>this</i> Institution. This is a shortcut that enables to create
+        * a new instance that differs only slightly from <i>this</i> Institution.
+        *
+        * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity
+        * @see java.lang.Object#clone()
         */
        @Override
-       public String generateTitle(){
-               return "";
+       public Object clone() {
+               try{
+                       Institution result = (Institution) super.clone();
+                       //no changes to code, isPartOf, name, types
+                       return result;
+               }catch (CloneNotSupportedException e){
+                       logger.warn("Object does not implement cloneable");
+                       e.printStackTrace();
+                       return null;
+               }
        }
-
 }
\ No newline at end of file