commit 4afd76e7642b5d023dfb9a07bad56d5c05072c81
Author: Andreas Müller <a.mueller@bgbm.org>
Date:   Mon Jun 5 13:10:12 2017 +0200

    fix #6472 add key as target for IntextReference

diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IntextReference.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IntextReference.java
index 5d26f3a0a3..f3911a135b 100644
--- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IntextReference.java
+++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IntextReference.java
@@ -19,6 +19,7 @@ import org.hibernate.annotations.CascadeType;
 import org.hibernate.envers.Audited;
 
 import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
 import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
@@ -47,6 +48,7 @@ import eu.etaxonomy.cdm.model.taxon.TaxonBase;
     "agent",
     "reference",
     "media",
+    "key",
     "languageString",
     "annotation",
     "startPos",
@@ -94,6 +96,13 @@ public class IntextReference extends VersionableEntity {
     @ManyToOne(fetch = FetchType.LAZY)
 	private Media media;
 
+    @XmlElement(name = "SingleAccessKey")
+    @XmlIDREF
+    @XmlSchemaType(name = "IDREF")
+    @ManyToOne(fetch = FetchType.LAZY)
+    private PolytomousKey key;
+
+
     //TODO or do we want to link to LanguageString Base??
     @XmlElement(name = "LanguageString")
     @XmlIDREF
@@ -119,7 +128,8 @@ public class IntextReference extends VersionableEntity {
         AGENT("agent"),
         MEDIA("media"),
         OCCURRENCE("occurrence"),
-        TAXON("taxon")
+        TAXON("taxon"),
+        KEY("key")
         ;
         String tag;
 
@@ -142,29 +152,6 @@ public class IntextReference extends VersionableEntity {
         return result;
     }
 
-//	public static IntextReference NewTaxonNameInstance(TaxonName taxonName, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(taxonName, null, null, null, null, null, languageString, start, end);
-//	}
-//
-//	public static IntextReference NewTaxonInstance(TaxonBase<?> taxon, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(null, taxon, null, null, null, null, languageString, start, end);
-//	}
-//
-//	public static IntextReference NewOccurrenceInstance(SpecimenOrObservationBase<?> occurrence, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(null, null, occurrence, null, null, null, languageString, start, end);
-//	}
-//
-//	public static IntextReference NewAgentInstance(AgentBase<?> agent, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(null, null, null, agent, null, null, languageString, start, end);
-//	}
-//
-//	public static IntextReference NewReferenceInstance(Reference reference, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(null, null, null, null, reference, null, languageString, start, end);
-//	}
-//
-//	public static IntextReference NewMediaInstance(Media media, LanguageStringBase languageString, int start, int end){
-//		return new IntextReference(null, null, null, null, null, media, languageString, start, end);
-//	}
 
 	public static LanguageString NewReferencedLanguageString(IIntextReferenceTarget target, String pre, String middle, String post, Language language){
         LanguageString result = LanguageString.NewInstance(null, language);
@@ -195,27 +182,6 @@ public class IntextReference extends VersionableEntity {
 	@Deprecated //for hibernate use only
 	private IntextReference(){}
 
-//	private IntextReference(TaxonName taxonName, TaxonBase<?> taxon,
-//				SpecimenOrObservationBase<?> occurrence, AgentBase<?> agent,
-//				Reference reference, Media media, LanguageStringBase languageString, int start, int end) {
-//			super();
-//			this.taxonName = taxonName;
-//			this.taxon = taxon;
-//			this.occurrence = occurrence;
-//			this.agent = agent;
-//			this.reference = reference;
-//			this.media = media;
-//			if (languageString != null && languageString.isInstanceOf(LanguageString.class)){
-//				this.languageString = CdmBase.deproxy(languageString, LanguageString.class);
-//				this.languageString.addIntextReference(this);
-//			}else if (languageString != null && languageString.isInstanceOf(Annotation.class)){
-//				this.annotation = CdmBase.deproxy(languageString, Annotation.class);
-//				this.annotation.addIntextReference(this);
-//			}
-//			this.startPos = start;
-//			this.endPos = end;
-//	}
-
 	   private IntextReference(IIntextReferenceTarget target, IIntextReferencable referencedEntity, int start, int end) {
            super();
            setTarget(target);
@@ -239,6 +205,8 @@ public class IntextReference extends VersionableEntity {
             return CDM_INTEXT_CLASS.REFERENCE;
         }else if (occurrence != null){
             return CDM_INTEXT_CLASS.OCCURRENCE;
+        }else if (key != null){
+            return CDM_INTEXT_CLASS.KEY;
         }else{
             throw new IllegalStateException("Intext reference has no target object defined");
         }
@@ -265,6 +233,8 @@ public class IntextReference extends VersionableEntity {
            return reference;
        }else if (occurrence != null){
            return occurrence;
+       }else if (key != null){
+           return key;
        }else{
            throw new IllegalStateException("Intext reference has no target object defined");
        }
@@ -287,6 +257,8 @@ public class IntextReference extends VersionableEntity {
             this.reference = (Reference)target;
         }else if (target instanceof Media){
             this.media = (Media)target;
+        }else if (target instanceof PolytomousKey){
+            this.key = (PolytomousKey)target;
         }else{
             throw new IllegalArgumentException("Target entity not yet handled: " + target.getClass().getName());
         }
@@ -328,64 +300,6 @@ public class IntextReference extends VersionableEntity {
        }
    }
 
-//	public TaxonName getTaxonName() {
-//		return taxonName;
-//	}
-//	public void setTaxonName(TaxonName taxonName) {
-//		this.taxonName = taxonName;
-//	}
-//
-//
-//	public TaxonBase<?> getTaxon() {
-//		return taxon;
-//	}
-//	public void setTaxon(TaxonBase<?> taxon) {
-//		this.taxon = taxon;
-//	}
-//
-//	public SpecimenOrObservationBase<?> getOccurrence() {
-//		return occurrence;
-//	}
-//	public void setOccurrence(SpecimenOrObservationBase<?> occurrence) {
-//		this.occurrence = occurrence;
-//	}
-//
-//	public AgentBase<?> getAgent() {
-//		return agent;
-//	}
-//	public void setAgent(AgentBase<?> agent) {
-//		this.agent = agent;
-//	}
-//
-//	public Reference getReference() {
-//		return reference;
-//	}
-//	public void setReference(Reference reference) {
-//		this.reference = reference;
-//	}
-//
-//	public Media getMedia() {
-//		return media;
-//	}
-//	public void setMedia(Media media) {
-//		this.media = media;
-//	}
-
-//	public LanguageString getLanguageString() {
-//		return languageString;
-//	}
-//	protected void setLanguageString(LanguageString languageString) {
-//		this.languageString = languageString;
-//	}
-//
-//	public Annotation getAnnotation() {
-//		return annotation;
-//	}
-//
-//	public void setAnnotation(Annotation annotation) {
-//		this.annotation = annotation;
-//	}
-
 	public int getStartPos() {
 		return startPos;
 	}
diff --git a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/PolytomousKey.java b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/PolytomousKey.java
index 0c0c387968..ccfb742259 100644
--- a/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/PolytomousKey.java
+++ b/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/description/PolytomousKey.java
@@ -37,6 +37,7 @@ import org.hibernate.envers.Audited;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.model.common.DefinedTerm;
+import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.location.NamedArea;
@@ -74,7 +75,10 @@ import eu.etaxonomy.cdm.strategy.generate.PolytomousKeyGenerator;
 //@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
 //@Indexed(index = "eu.etaxonomy.cdm.model.description.PolytomousKey")
 @Audited
-public class PolytomousKey extends IdentifiableEntity<PolytomousKeyDefaultCacheStrategy> implements IIdentificationKey {
+public class PolytomousKey
+            extends IdentifiableEntity<PolytomousKeyDefaultCacheStrategy>
+            implements IIdentificationKey, IIntextReferenceTarget {
+
     private static final long serialVersionUID = -3368243754557343942L;
     private static final Logger logger = Logger.getLogger(PolytomousKey.class);
 
diff --git a/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v41_47/SchemaUpdater_41_47.java b/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v41_47/SchemaUpdater_41_47.java
index 549d0f9cf3..65a5d61bec 100644
--- a/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v41_47/SchemaUpdater_41_47.java
+++ b/cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/v41_47/SchemaUpdater_41_47.java
@@ -23,8 +23,8 @@ import eu.etaxonomy.cdm.database.update.MnTableCreator;
 import eu.etaxonomy.cdm.database.update.SchemaUpdaterBase;
 import eu.etaxonomy.cdm.database.update.SimpleSchemaUpdaterStep;
 import eu.etaxonomy.cdm.database.update.TableCreator;
-import eu.etaxonomy.cdm.database.update.UniqueIndexDropper;
 import eu.etaxonomy.cdm.database.update.TableNameChanger;
+import eu.etaxonomy.cdm.database.update.UniqueIndexDropper;
 import eu.etaxonomy.cdm.database.update.v40_41.SchemaUpdater_40_41;
 
 /**
@@ -210,10 +210,13 @@ public class SchemaUpdater_41_47 extends SchemaUpdaterBase {
         step = ColumnAdder.NewStringInstance(stepName, tableName, newColumnName, length, INCLUDE_AUDIT);
         stepList.add(step);
 
-        //ModelUpdateResult
-
-        //Remove termupdater and allow update only from version 4.0
-
+        //#6472 add key to IntextReference
+        stepName = "Add key to IntextReference";
+        tableName = "IntextReference";
+        newColumnName = "key_id";
+        referencedTable = "PolytomousKey";
+        step = ColumnAdder.NewIntegerInstance(stepName, tableName, newColumnName, INCLUDE_AUDIT, !NOT_NULL, referencedTable);
+        stepList.add(step);
 
 
         return stepList;
@@ -368,14 +371,14 @@ public class SchemaUpdater_41_47 extends SchemaUpdaterBase {
         stepList.add(step);
 
         //#6368
-        chnageTaxonNameTableName(stepList);
+        changeTaxonNameTableName(stepList);
 
     }
 
     /**
      * #6368
      */
-    private void chnageTaxonNameTableName(List<ISchemaUpdaterStep> stepList) {
+    private void changeTaxonNameTableName(List<ISchemaUpdaterStep> stepList) {
 
         //Update
         String oldName = "TaxonNameBase";
diff --git a/cdmlib-test/src/main/resources/dbscripts/001-cdm.h2.sql b/cdmlib-test/src/main/resources/dbscripts/001-cdm.h2.sql
index bc9cceb628..ce4973b299 100644
--- a/cdmlib-test/src/main/resources/dbscripts/001-cdm.h2.sql
+++ b/cdmlib-test/src/main/resources/dbscripts/001-cdm.h2.sql
@@ -2240,7 +2240,8 @@ CREATE CACHED TABLE PUBLIC.INTEXTREFERENCE(
   	OCCURRENCE_ID INT,
   	REFERENCE_ID INT,
   	TAXON_ID INT,
-  	TAXONNAME_ID INT
+  	TAXONNAME_ID INT,
+	KEY_ID INT
 );
 -- 0 +/- SELECT COUNT(*) FROM PUBLIC.INTEXTREFERENCE;
 CREATE CACHED TABLE PUBLIC.INTEXTREFERENCE_AUD(
@@ -2261,7 +2262,8 @@ CREATE CACHED TABLE PUBLIC.INTEXTREFERENCE_AUD(
   	OCCURRENCE_ID INT,
   	REFERENCE_ID INT,
   	TAXON_ID INT,
-  	TAXONNAME_ID INT
+  	TAXONNAME_ID INT,
+	KEY_ID INT
 );
 -- 0 +/- SELECT COUNT(*) FROM PUBLIC.INTEXTREFERENCE_AUD;
 
