upper case for JoinTable WorkingSet_DescriptionBase
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / description / Distribution.java
index a6b6c63b2a644e2e8f3be90c48301bcd19e227be..39c7161a8072324f1f5f1b3c06f055f3e4f20bbd 100644 (file)
@@ -9,8 +9,12 @@
 
 package eu.etaxonomy.cdm.model.description;
 
+import java.util.ArrayList;
+
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.ManyToOne;
+import javax.validation.constraints.NotNull;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -20,11 +24,12 @@ 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.Indexed;
 
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.validation.Level2;
 
 /**
  * This class represents elementary distribution data for a {@link Taxon taxon}.
@@ -49,7 +54,9 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 })
 @XmlRootElement(name = "Distribution")
 @Entity
-public class Distribution extends DescriptionElementBase {
+@Audited
+@Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
+public class Distribution extends DescriptionElementBase implements Cloneable {
        private static final long serialVersionUID = 8366462435651559730L;
        @SuppressWarnings("unused")
        private static final Logger logger = Logger.getLogger(Distribution.class);
@@ -57,11 +64,15 @@ public class Distribution extends DescriptionElementBase {
        @XmlElement(name = "NamedArea")
        @XmlIDREF
        @XmlSchemaType(name = "IDREF")
+       @ManyToOne(fetch = FetchType.LAZY)
+       @NotNull(groups = Level2.class)
        private NamedArea area;
        
        @XmlElement(name = "PresenceAbsenceStatus")
        @XmlIDREF
        @XmlSchemaType(name = "IDREF")
+       @ManyToOne(fetch = FetchType.LAZY)
+       @NotNull(groups = Level2.class)
        private PresenceAbsenceTermBase<?> status;
 
        
@@ -70,7 +81,7 @@ public class Distribution extends DescriptionElementBase {
         * The corresponding {@link Feature feature} is set to {@link Feature#DISTRIBUTION() DISTRIBUTION}.
         */
        protected Distribution(){
-               super(Feature.DISTRIBUTION());
+               super();
        }
        
        
@@ -82,6 +93,7 @@ public class Distribution extends DescriptionElementBase {
         */
        public static Distribution NewInstance(){
                Distribution result = new Distribution();
+               result.setFeature(Feature.DISTRIBUTION());
                return result;
        }
 
@@ -94,18 +106,15 @@ public class Distribution extends DescriptionElementBase {
         * @see                         #NewInstance()
         */
        public static Distribution NewInstance(NamedArea area, PresenceAbsenceTermBase<?> status){
-               Distribution result = new Distribution();
+               Distribution result = NewInstance();
                result.setArea(area);
                result.setStatus(status);
                return result;
        }
        
-
-               
-       
        /** 
-        * Deprecated because {@link Feature feature} should always be {@link Feature#DISTRIBUTION() DISTRIBUTION}
-        * for all distribution instances.
+        * @deprecated Deprecated because {@link Feature feature} should always be {@link Feature#DISTRIBUTION() DISTRIBUTION}
+        * for all distribution instances and therefore it should not be changed.
         */
        /* (non-Javadoc)
         * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#setFeature(eu.etaxonomy.cdm.model.description.Feature)
@@ -119,8 +128,6 @@ public class Distribution extends DescriptionElementBase {
        /** 
         * Returns the {@link NamedArea named area} <i>this</i> distribution applies to.
         */
-       @ManyToOne
-       @Cascade({CascadeType.SAVE_UPDATE})
        public NamedArea getArea(){
                return this.area;
        }
@@ -134,7 +141,6 @@ public class Distribution extends DescriptionElementBase {
        /** 
         * Returns the {@link PresenceAbsenceTermBase presence or absence term} for <i>this</i> distribution.
         */
-       @ManyToOne
        public PresenceAbsenceTermBase<?> getStatus(){
                return this.status;
        }
@@ -144,5 +150,74 @@ public class Distribution extends DescriptionElementBase {
        public void setStatus(PresenceAbsenceTermBase<?> status){
                this.status = status;
        }
+       
+       /**
+        * Special equal method for building an sorted distribution tree
+        * @param dist
+        * @return
+        */
+       public boolean equalsForTree(Distribution dist){
+               boolean result = false;
+               //same area level and area label
+               if (this.getArea().getLabel().compareTo(dist.getArea().getLabel()) == 0 &&
+                               this.getArea().getLevel().getLabel().compareTo(dist.getArea().getLevel().getLabel()) == 0){
+                       result = true;
+               }
+               
+               return result;
+       }
+       
+       /**
+        * Special function for building the sorted distribution tree. The function returns true 
+        * if the sources of the two different objects are different
+        * @param dist
+        * @return
+        */
+       public boolean isDifferentSources(Distribution dist){
+               boolean result = false;
+               if(this.getSources().equals(dist.getSources())){
+                       result = true;
+               }
+               return result;
+       }
+       
+
+//*********************************** CLONE *****************************************/
+
+       /** 
+        * Clones <i>this</i> distribution. This is a shortcut that enables to create
+        * a new instance that differs only slightly from <i>this</i> distribution by
+        * modifying only some of the attributes.
+        * 
+        * @see eu.etaxonomy.cdm.model.description.DescriptionElementBase#clone()
+        * @see java.lang.Object#clone()
+        */
+       @Override
+       public Object clone() {
+
+               try {
+                       Distribution result = (Distribution)super.clone();
+                       
+                       return result;
+                       //no changes to: area, status
+               } catch (CloneNotSupportedException e) {
+                       logger.warn("Object does not implement cloneable");
+                       e.printStackTrace();
+                       return null;
+               }
+       }       
+
+// ************************* to String ***************************************************/    
+       
+       /**
+        * Implementation of the toString() function
+        */
+       public String toString(){
+               String result = "null";
+               if (this.area != null){                 
+                       result = area.getLabel().toString();
+               }
+               return result;
+       }
 
 }
\ No newline at end of file