4 package eu
.etaxonomy
.cdm
.model
.common
;
6 import javax
.persistence
.Entity
;
7 import javax
.persistence
.FetchType
;
8 import javax
.persistence
.ManyToOne
;
9 import javax
.xml
.bind
.annotation
.XmlAccessType
;
10 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
11 import javax
.xml
.bind
.annotation
.XmlElement
;
12 import javax
.xml
.bind
.annotation
.XmlIDREF
;
13 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
14 import javax
.xml
.bind
.annotation
.XmlType
;
16 import org
.apache
.commons
.lang
.StringUtils
;
17 import org
.hibernate
.annotations
.Cascade
;
18 import org
.hibernate
.annotations
.CascadeType
;
19 import org
.hibernate
.envers
.Audited
;
21 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
22 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
23 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
24 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
29 * This class represents a link to another CDM Base class from within a text.
30 * If a text, e.g. a LanguageString links parts of its text to an other CDM Base
31 * one may create a tag around the according text including id and uuid as attributes
32 * and adding an IntextReference to the LanguageString. The IntextReference then points
33 * to the according CdmBase.
34 * This way we may keep referential integrity and we may also support correct
35 * deduplication or deletion of the referenced objects.
42 @XmlAccessorType(XmlAccessType
.FIELD
)
43 @XmlType(name
= "IntextReference", propOrder
= {
57 public class IntextReference
extends VersionableEntity
{
58 private static final long serialVersionUID
= -7002541566256975424L;
60 @XmlElement(name
= "TaxonName")
62 @XmlSchemaType(name
= "IDREF")
63 @ManyToOne(fetch
= FetchType
.LAZY
)
64 private TaxonNameBase
<?
,?
> taxonName
;
66 @XmlElement(name
= "Taxon")
68 @XmlSchemaType(name
= "IDREF")
69 @ManyToOne(fetch
= FetchType
.LAZY
)
70 private TaxonBase
<?
> taxon
;
72 @XmlElement(name
= "Occurrence")
74 @XmlSchemaType(name
= "IDREF")
75 @ManyToOne(fetch
= FetchType
.LAZY
)
76 private SpecimenOrObservationBase
<?
> occurrence
;
78 @XmlElement(name
= "Agent")
80 @XmlSchemaType(name
= "IDREF")
81 @ManyToOne(fetch
= FetchType
.LAZY
)
82 private AgentBase
<?
> agent
;
84 @XmlElement(name
= "Reference")
86 @XmlSchemaType(name
= "IDREF")
87 @ManyToOne(fetch
= FetchType
.LAZY
)
88 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
})
89 private Reference reference
;
91 @XmlElement(name
= "Media")
93 @XmlSchemaType(name
= "IDREF")
94 @ManyToOne(fetch
= FetchType
.LAZY
)
97 //TODO or do we want to link to LanguageString Base??
98 @XmlElement(name
= "LanguageString")
100 @XmlSchemaType(name
= "IDREF")
101 @ManyToOne(fetch
= FetchType
.LAZY
)
102 private LanguageString languageString
;
104 //TODO or do we want to link to LanguageString Base??
105 @XmlElement(name
= "Annotation")
107 @XmlSchemaType(name
= "IDREF")
108 @ManyToOne(fetch
= FetchType
.LAZY
)
109 private Annotation annotation
;
111 private int startPos
;
116 private enum CDM_INTEXT_CLASS
{
117 REFERENCE("reference"),
121 OCCURRENCE("occurrence"),
126 private CDM_INTEXT_CLASS(String tag
) {
137 // ***************** FACTORY METHOD ***********************************
139 public static IntextReference
NewTaxonNameInstance(TaxonNameBase
<?
,?
> taxonName
, LanguageStringBase languageString
, int start
, int end
){
140 return new IntextReference(taxonName
, null, null, null, null, null, languageString
, start
, end
);
143 public static IntextReference
NewTaxonInstance(TaxonBase
<?
> taxon
, LanguageStringBase languageString
, int start
, int end
){
144 return new IntextReference(null, taxon
, null, null, null, null, languageString
, start
, end
);
147 public static IntextReference
NewOccurrenceInstance(SpecimenOrObservationBase
<?
> occurrence
, LanguageStringBase languageString
, int start
, int end
){
148 return new IntextReference(null, null, occurrence
, null, null, null, languageString
, start
, end
);
151 public static IntextReference
NewAgentInstance(AgentBase
<?
> agent
, LanguageStringBase languageString
, int start
, int end
){
152 return new IntextReference(null, null, null, agent
, null, null, languageString
, start
, end
);
155 public static IntextReference
NewReferenceInstance(Reference reference
, LanguageStringBase languageString
, int start
, int end
){
156 return new IntextReference(null, null, null, null, reference
, null, languageString
, start
, end
);
159 public static IntextReference
NewMediaInstance(Media media
, LanguageStringBase languageString
, int start
, int end
){
160 return new IntextReference(null, null, null, null, null, media
, languageString
, start
, end
);
163 //********************** CONSTRUCTOR ********************************************/
166 * @deprecated for internal use only
168 @Deprecated //for hibernate use only
169 private IntextReference(){}
171 private IntextReference(TaxonNameBase
<?
, ?
> taxonName
, TaxonBase
<?
> taxon
,
172 SpecimenOrObservationBase
<?
> occurrence
, AgentBase
<?
> agent
,
173 Reference reference
, Media media
, LanguageStringBase languageString
, int start
, int end
) {
175 this.taxonName
= taxonName
;
177 this.occurrence
= occurrence
;
179 this.reference
= reference
;
181 if (languageString
!= null && languageString
.isInstanceOf(LanguageString
.class)){
182 this.languageString
= CdmBase
.deproxy(languageString
, LanguageString
.class);
183 this.languageString
.addIntextReference(this);
184 }else if (languageString
!= null && languageString
.isInstanceOf(Annotation
.class)){
185 this.annotation
= CdmBase
.deproxy(languageString
, Annotation
.class);
186 this.annotation
.addIntextReference(this);
188 this.startPos
= start
;
193 private CDM_INTEXT_CLASS
myClass(){
195 return CDM_INTEXT_CLASS
.AGENT
;
196 }else if (media
!= null){
197 return CDM_INTEXT_CLASS
.MEDIA
;
198 }else if (taxonName
!= null){
199 return CDM_INTEXT_CLASS
.TAXONNAME
;
200 }else if (taxon
!= null){
201 return CDM_INTEXT_CLASS
.TAXON
;
202 }else if (reference
!= null){
203 return CDM_INTEXT_CLASS
.REFERENCE
;
204 }else if (occurrence
!= null){
205 return CDM_INTEXT_CLASS
.OCCURRENCE
;
207 throw new IllegalStateException("Intext reference has no target object defined");
211 private IdentifiableEntity
<?
> myEntity(){
214 }else if (media
!= null){
216 }else if (taxonName
!= null){
218 }else if (taxon
!= null){
220 }else if (reference
!= null){
222 }else if (occurrence
!= null){
225 throw new IllegalStateException("Intext reference has no target object defined");
229 // **************** GETTER / SETTER ******************************************/
231 public TaxonNameBase
<?
, ?
> getTaxonName() {
234 public void setTaxonName(TaxonNameBase
<?
, ?
> taxonName
) {
235 this.taxonName
= taxonName
;
239 public TaxonBase
<?
> getTaxon() {
242 public void setTaxon(TaxonBase
<?
> taxon
) {
246 public SpecimenOrObservationBase
<?
> getOccurrence() {
249 public void setOccurrence(SpecimenOrObservationBase
<?
> occurrence
) {
250 this.occurrence
= occurrence
;
253 public AgentBase
<?
> getAgent() {
256 public void setAgent(AgentBase
<?
> agent
) {
260 public Reference
getReference() {
263 public void setReference(Reference reference
) {
264 this.reference
= reference
;
269 public Media
getMedia() {
272 public void setMedia(Media media
) {
276 public LanguageString
getLanguageString() {
277 return languageString
;
279 public void setLanguageString(LanguageString languageString
) {
280 this.languageString
= languageString
;
283 public Annotation
getAnnotation() {
287 public void setAnnotation(Annotation annotation
) {
288 this.annotation
= annotation
;
291 public int getStartPos() {
294 public void setStartPos(int startPos
) {
295 this.startPos
= startPos
;
298 public int getEndPos() {
301 public void setEndPos(int endPos
) {
302 this.endPos
= endPos
;
305 private static final String CDM_PREFIX
= "cdm:";
306 public String
toInlineString(String innerText
){
307 String tag
= CDM_PREFIX
+ myClass().tag();
308 IdentifiableEntity
<?
> entity
= myEntity();
309 String attributes
= " cdmId='" + entity
.getUuid() + "' intextId='" + this.getUuid() + "'" + otherAttributes(entity
);
311 if (StringUtils
.isNotEmpty(innerText
)){
312 result
= "<" + tag
+ attributes
+ ">" + innerText
+ "</" + tag
+ ">";
314 result
= "<" + tag
+ attributes
+ "/>";
320 * Entity class dependent attributes
324 private String
otherAttributes(IdentifiableEntity
<?
> entity
) {