2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.model
.reference
;
13 import java
.util
.HashSet
;
16 import javax
.persistence
.Column
;
17 import javax
.persistence
.Entity
;
18 import javax
.persistence
.FetchType
;
19 import javax
.persistence
.Inheritance
;
20 import javax
.persistence
.InheritanceType
;
21 import javax
.persistence
.OneToMany
;
22 import javax
.persistence
.OneToOne
;
23 import javax
.persistence
.Table
;
24 import javax
.validation
.constraints
.NotNull
;
25 import javax
.xml
.bind
.annotation
.XmlAccessType
;
26 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
27 import javax
.xml
.bind
.annotation
.XmlAttribute
;
28 import javax
.xml
.bind
.annotation
.XmlElement
;
29 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
30 import javax
.xml
.bind
.annotation
.XmlIDREF
;
31 import javax
.xml
.bind
.annotation
.XmlRootElement
;
32 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
33 import javax
.xml
.bind
.annotation
.XmlType
;
35 import org
.apache
.commons
.lang
.StringUtils
;
36 import org
.apache
.log4j
.Logger
;
37 import org
.hibernate
.annotations
.Cascade
;
38 import org
.hibernate
.annotations
.CascadeType
;
39 import org
.hibernate
.annotations
.Type
;
40 import org
.hibernate
.envers
.Audited
;
41 import org
.springframework
.util
.Assert
;
43 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
44 import eu
.etaxonomy
.cdm
.model
.common
.IIntextReferenceTarget
;
45 import eu
.etaxonomy
.cdm
.model
.common
.ReferencedEntityBase
;
46 import eu
.etaxonomy
.cdm
.model
.media
.ExternalLink
;
47 import eu
.etaxonomy
.cdm
.strategy
.merge
.Merge
;
48 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeMode
;
51 * Abstract base class for classes implementing {@link eu.etaxonomy.cdm.model.reference.IOriginalSource IOriginalSource}.
52 * @see eu.etaxonomy.cdm.model.reference.IOriginalSource
55 * @since 08-Nov-2007 13:06:22
58 @XmlAccessorType(XmlAccessType
.FIELD
)
59 @XmlType(name
= "OriginalSource", propOrder
= {
66 @XmlRootElement(name
= "OriginalSource")
69 @Inheritance(strategy
=InheritanceType
.SINGLE_TABLE
)
70 @Table(name
="OriginalSourceBase")
71 public abstract class OriginalSourceBase
<T
extends ISourceable
>
72 extends ReferencedEntityBase
73 implements IOriginalSource
<T
>, IIntextReferenceTarget
{
75 private static final long serialVersionUID
= -1972959999261181462L;
76 @SuppressWarnings("unused")
77 private static final Logger logger
= Logger
.getLogger(OriginalSourceBase
.class);
80 * The {@link OriginalSourceType type} of this source. According to PROV the type has to be thought as
81 * an activity that leads from the source entity to the current entity. It is not a property of the
84 @XmlAttribute(name
="type")
85 @Column(name
="sourceType")
87 @Type(type
= "eu.etaxonomy.cdm.hibernate.EnumUserType",
88 parameters
= {@org.hibernate
.annotations
.Parameter(name
="enumClass", value
="eu.etaxonomy.cdm.model.reference.OriginalSourceType")}
91 private OriginalSourceType type
;
93 //The object's ID in the source, where the alternative string comes from
94 @XmlElement(name
= "IdInSource")
95 private String idInSource
;
97 @XmlElement(name
= "IdNamespace")
98 private String idNamespace
;
100 @XmlElement(name
= "CdmSource")
102 @XmlSchemaType(name
= "IDREF")
103 @OneToOne(fetch
= FetchType
.EAGER
, orphanRemoval
=true) //EAGER to avoid LIEs cdmSource should always be part of the OriginalSource itself
104 @Cascade({CascadeType
.SAVE_UPDATE
,CascadeType
.MERGE
, CascadeType
.DELETE
})
105 private CdmLinkSource cdmSource
;
107 @XmlElementWrapper(name
= "Links", nillable
= true)
108 @XmlElement(name
= "Link")
109 @OneToMany(fetch
=FetchType
.LAZY
, orphanRemoval
=true)
110 @Cascade({CascadeType
.SAVE_UPDATE
, CascadeType
.MERGE
, CascadeType
.DELETE
})
111 @Merge(MergeMode
.ADD_CLONE
)
112 private Set
<ExternalLink
> links
= new HashSet
<>();
114 //***************** CONSTRUCTOR ***********************/
116 //for hibernate use only
117 protected OriginalSourceBase() {
125 protected OriginalSourceBase(OriginalSourceType type
){
127 throw new IllegalArgumentException("OriginalSourceType must not be null");
132 //**************** GETTER / SETTER *******************************/
136 public String
getIdInSource(){
137 return this.idInSource
;
140 public void setIdInSource(String idInSource
){
141 this.idInSource
= idInSource
;
146 public String
getIdNamespace() {
150 public void setIdNamespace(String idNamespace
) {
151 this.idNamespace
= idNamespace
;
156 public OriginalSourceType
getType() {
160 public void setType(OriginalSourceType type
) {
161 Assert
.notNull(type
, "OriginalSourceType must not be null");
166 public ICdmTarget
getCdmSource() {
167 return cdmSource
== null?
null: cdmSource
.getTarget();
169 /* this method was implemented in the context of the CdmLinkSourceBeanProcessor which is unused
170 * method is preserved for the time when the REST API will be revised (#8637)
172 public CdmLinkSource getCdmSource() {
173 if(cdmSource != null){
174 logger.error("NOT NULL");
182 // public void setCdmSource(CdmLinkSource cdmSource) {
183 // this.cdmSource = cdmSource;
186 public void setCdmSource(ICdmTarget cdmTarget
){
187 if (cdmTarget
!= null){
188 this.cdmSource
= CdmLinkSource
.NewInstance(cdmTarget
);
190 this.cdmSource
= null;
194 //********************** External Links **********************************************
197 public Set
<ExternalLink
> getLinks(){
200 public void setLinks(Set
<ExternalLink
> links
){
203 public void addLink(ExternalLink link
){
208 public void removeLink(ExternalLink link
){
209 if(links
.contains(link
)) {
214 //********************** CLONE ************************************************/
217 public Object
clone() throws CloneNotSupportedException
{
218 OriginalSourceBase
<?
> result
= (OriginalSourceBase
<?
>)super.clone();
220 Set
<ExternalLink
> links
= new HashSet
<>();
221 result
.setLinks(links
);
222 for(ExternalLink link
: this.links
){
223 result
.addLink(link
.clone());
226 if (this.cdmSource
!= null){
227 result
.setCdmSource(this.cdmSource
.getTarget());
230 //no changes to: idInSource
234 // **************** EMPTY ************************/
237 protected boolean checkEmpty(){
238 return checkEmpty(false);
241 public boolean checkEmpty(boolean excludeType
){
242 return super.checkEmpty()
243 && isBlank(this.getIdInSource())
244 && isBlank(this.getIdNamespace())
245 && this.links
.isEmpty()
246 && this.cdmSource
== null
247 && (excludeType
|| this.type
== null)
251 //************************ toString ***************************************/
253 public String
toString(){
254 if (isNotBlank(idInSource
) || isNotBlank(idNamespace
) ){
255 return "OriginalSource:" + CdmUtils
.concat(":", idNamespace
, idInSource
);
257 return super.toString();
261 //*********************************** EQUALS *********************************************************/
264 public boolean equalsByShallowCompare(ReferencedEntityBase other
) {
266 if(!super.equalsByShallowCompare(other
)) {
269 @SuppressWarnings("unchecked")
270 OriginalSourceBase
<T
> theOther
= (OriginalSourceBase
<T
>)other
;
271 if(!StringUtils
.equals(this.getIdInSource(), theOther
.getIdInSource())
272 || !CdmUtils
.nullSafeEqual(this.getIdNamespace(), theOther
.getIdNamespace())
273 || !CdmUtils
.nullSafeEqual(this.getType(), theOther
.getType())
274 || !CdmUtils
.nullSafeEqual(this.getCdmSource(), theOther
.getCdmSource())
275 || !CdmUtils
.nullSafeEqual(this.getLinks(), theOther
.getLinks())) {