fix #5680: get method needs to be named like the attribute
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / description / TaxonInteraction.java
index 6ea8a4acb653b3be22cbaa5997e5f3a70466e25a..12b94ca5bdb8b5e266a47c84a3d7970c948b0d81 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.
 */
@@ -17,6 +17,7 @@ import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToOne;
+import javax.persistence.MapKeyJoinColumn;
 import javax.persistence.OneToMany;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -65,42 +66,42 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 @Entity
 @Audited
 @Indexed(index = "eu.etaxonomy.cdm.model.description.DescriptionElementBase")
-public class TaxonInteraction extends DescriptionElementBase implements IMultiLanguageTextHolder{
+public class TaxonInteraction extends DescriptionElementBase implements IMultiLanguageTextHolder, Cloneable{
        private static final long serialVersionUID = -5014025677925668627L;
-       @SuppressWarnings("unused")
        private static final Logger logger = Logger.getLogger(TaxonInteraction.class);
-       
+
        @XmlElement(name = "Description")
     @XmlJavaTypeAdapter(MultilanguageTextAdapter.class)
-    @OneToMany(fetch = FetchType.LAZY)
-    @JoinTable(name = "TaxonInteraction_LanguageString")
-    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE, CascadeType.DELETE_ORPHAN })
+    @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
+       @MapKeyJoinColumn(name="description_mapkey_id")
+    @JoinTable(name = "TaxonInteraction_LanguageString")  //to distinguish from other DescriptionElementBase_LanguageString
+    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE, CascadeType.DELETE})
     private Map<Language,LanguageString> description = new HashMap<Language,LanguageString>();
-       
+
        @XmlElement(name = "Taxon2")
        @XmlIDREF
        @XmlSchemaType(name = "IDREF")
        @ManyToOne(fetch = FetchType.LAZY)
-       @Cascade(CascadeType.SAVE_UPDATE)
+       @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
        private Taxon taxon2;
-       
-       /** 
+
+       /**
         * Class constructor: creates a new empty taxon interaction instance.
         */
        public TaxonInteraction() {
                super(null);
        }
 
-       /** 
+       /**
         * Creates a new empty taxon interaction instance.
         */
        public static TaxonInteraction NewInstance(){
                return new TaxonInteraction();
        }
-       
+
        /**
         * Creates a new empty taxon interaction instance and also sets the feature
-        * 
+        *
         * @param feature
         * @return
         */
@@ -111,9 +112,9 @@ public class TaxonInteraction extends DescriptionElementBase implements IMultiLa
                }
                return taxonInteraction;
        }
-       
-       
-       /** 
+
+
+       /**
         * Returns the second {@link Taxon taxon} involved in <i>this</i> taxon interaction.
         * The first taxon is the taxon described in the corresponding
         * {@link TaxonDescription taxon description}.
@@ -122,29 +123,29 @@ public class TaxonInteraction extends DescriptionElementBase implements IMultiLa
                return this.taxon2;
        }
        /**
-        * @see #getTaxon2() 
+        * @see #getTaxon2()
         */
        public void setTaxon2(Taxon taxon2){
                this.taxon2 = taxon2;
        }
 
-       /** 
+       /**
         * Returns the {@link MultilanguageText multilanguage text} used to describe
         * <i>this</i> taxon interaction. The different {@link LanguageString language strings}
         * contained in the multilanguage text should all have the same meaning.
         */
-       public Map<Language,LanguageString> getDescriptions(){
+       public Map<Language,LanguageString> getDescription(){
                return this.description;
        }
-       
-       /** 
+
+       /**
         * Returns the description string in the given {@link Language language}
-        * 
+        *
         * @param language      the language in which the description string looked for is formulated
         * @see                         #getDescriptions()
-        */ 
+        */
        public String getDescription(Language language){
-               LanguageString languageString = description.get(language);
+           LanguageString languageString = description.get(language);
                if (languageString == null){
                        return null;
                }else{
@@ -156,30 +157,65 @@ public class TaxonInteraction extends DescriptionElementBase implements IMultiLa
         * Adds a translated {@link LanguageString text in a particular language}
         * to the {@link MultilanguageText multilanguage text} used to describe
         * <i>this</i> taxon interaction.
-        * 
+        *
         * @param description   the language string describing the taxon interaction
         *                                              in a particular language
         * @see                                 #getDescription()
-        * @see                                 #addDescription(String, Language)
+        * @see                                 #putDescription(String, Language)
+        * @deprecated                  should follow the put semantic of maps, this method will be removed in v4.0
+        *                                              Use the {@link #putDescription(LanguageString) putDescription} method instead
         */
+       @Deprecated
        public void addDescription(LanguageString description){
+               this.putDescription(description);
+       }
+
+       /**
+        * Adds a translated {@link LanguageString text in a particular language}
+        * to the {@link MultilanguageText multilanguage text} used to describe
+        * <i>this</i> taxon interaction.
+        *
+        * @param description   the language string describing the taxon interaction
+        *                                              in a particular language
+        * @see                                 #getDescription()
+        * @see                                 #putDescription(String, Language)
+        */
+       public void putDescription(LanguageString description){
                this.description.put(description.getLanguage(),description);
        }
        /**
         * Creates a {@link LanguageString language string} based on the given text string
-        * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text} 
+        * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
         * used to describe <i>this</i> taxon interaction.
-        * 
+        *
+        * @param text          the string describing the taxon interaction
+        *                                      in a particular language
+        * @param language      the language in which the text string is formulated
+        * @see                         #getDescription()
+        * @see                         #putDescription(LanguageString)
+        */
+       public void putDescription(Language language, String text){
+               this.description.put(language, LanguageString.NewInstance(text, language));
+       }
+
+       /**
+        * Creates a {@link LanguageString language string} based on the given text string
+        * and the given {@link Language language} and adds it to the {@link MultilanguageText multilanguage text}
+        * used to describe <i>this</i> taxon interaction.
+        *
         * @param text          the string describing the taxon interaction
         *                                      in a particular language
         * @param language      the language in which the text string is formulated
         * @see                         #getDescription()
         * @see                         #addDescription(LanguageString)
+        * @deprecated          should follow the put semantic of maps, this method will be removed in v4.0
+        *                                      Use the {@link #putDescription(Language, String) putDescription} method instead
         */
+       @Deprecated
        public void addDescription(String text, Language language){
-               this.description.put(language, LanguageString.NewInstance(text, language));
+               this.putDescription(language, text);
        }
-       /** 
+       /**
         * Removes from the {@link MultilanguageText multilanguage text} used to describe
         * <i>this</i> taxon interaction the one {@link LanguageString language string}
         * with the given {@link Language language}.
@@ -191,4 +227,39 @@ public class TaxonInteraction extends DescriptionElementBase implements IMultiLa
        public void removeDescription(Language lang){
                this.description.remove(lang);
        }
+
+
+//*********************************** CLONE *****************************************/
+
+       /**
+        * Clones <i>this</i> taxon interaction. This is a shortcut that enables to create
+        * a new instance that differs only slightly from <i>this</i> taxon interaction 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 {
+                       TaxonInteraction result = (TaxonInteraction)super.clone();
+
+                       //description
+                       result.description = new HashMap<Language, LanguageString>();
+                       for (Language language : getDescription().keySet()){
+                               //TODO clone needed? See also IndividualsAssociation
+                               LanguageString newLanguageString = (LanguageString)getDescription().get(language).clone();
+                               result.description.put(language, newLanguageString);
+                       }
+
+
+                       return result;
+                       //no changes to: taxon2
+               } catch (CloneNotSupportedException e) {
+                       logger.warn("Object does not implement cloneable");
+                       e.printStackTrace();
+                       return null;
+               }
+       }
 }
\ No newline at end of file