Project

General

Profile

Download (6.22 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.cdm.model.reference;
11

    
12

    
13
import java.util.HashSet;
14
import java.util.Set;
15

    
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.Table;
23
import javax.validation.constraints.NotNull;
24
import javax.xml.bind.annotation.XmlAccessType;
25
import javax.xml.bind.annotation.XmlAccessorType;
26
import javax.xml.bind.annotation.XmlAttribute;
27
import javax.xml.bind.annotation.XmlElement;
28
import javax.xml.bind.annotation.XmlElementWrapper;
29
import javax.xml.bind.annotation.XmlRootElement;
30
import javax.xml.bind.annotation.XmlType;
31

    
32
import org.apache.commons.lang.StringUtils;
33
import org.apache.log4j.Logger;
34
import org.hibernate.annotations.Cascade;
35
import org.hibernate.annotations.CascadeType;
36
import org.hibernate.annotations.Type;
37
import org.hibernate.envers.Audited;
38
import org.springframework.util.Assert;
39

    
40
import eu.etaxonomy.cdm.common.CdmUtils;
41
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
42
import eu.etaxonomy.cdm.model.common.ReferencedEntityBase;
43
import eu.etaxonomy.cdm.model.media.ExternalLink;
44
import eu.etaxonomy.cdm.strategy.merge.Merge;
45
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
46

    
47
/**
48
 * Abstract base class for classes implementing {@link eu.etaxonomy.cdm.model.reference.IOriginalSource IOriginalSource}.
49
 * @see eu.etaxonomy.cdm.model.reference.IOriginalSource
50
 *
51
 * @author m.doering
52
 * @since 08-Nov-2007 13:06:22
53
 */
54

    
55
@XmlAccessorType(XmlAccessType.FIELD)
56
@XmlType(name = "OriginalSource", propOrder = {
57
    "type",
58
	"idInSource",
59
    "idNamespace",
60
    "links"
61
})
62
@XmlRootElement(name = "OriginalSource")
63
@Entity
64
@Audited
65
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
66
@Table(name="OriginalSourceBase")
67
public abstract class OriginalSourceBase<T extends ISourceable>
68
        extends ReferencedEntityBase
69
        implements IOriginalSource<T>, IIntextReferenceTarget,Cloneable {
70

    
71
	private static final long serialVersionUID = -1972959999261181462L;
72
	@SuppressWarnings("unused")
73
	private static final Logger logger = Logger.getLogger(OriginalSourceBase.class);
74

    
75
	/**
76
	 * The {@link OriginalSourceType type} of this source. According to PROV the type has to be thought as
77
	 * an activity that leads from the source entity to the current entity. It is not a property of the
78
	 * source itself.
79
	 */
80
	@XmlAttribute(name ="type")
81
	@Column(name="sourceType")
82
	@NotNull
83
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
84
    	parameters = {@org.hibernate.annotations.Parameter(name="enumClass", value="eu.etaxonomy.cdm.model.common.OriginalSourceType")}
85
    )
86
	@Audited
87
	private OriginalSourceType type;
88

    
89
	//The object's ID in the source, where the alternative string comes from
90
	@XmlElement(name = "IdInSource")
91
	private String idInSource;
92

    
93
	@XmlElement(name = "IdNamespace")
94
	private String idNamespace;
95

    
96
    @XmlElementWrapper(name = "Links", nillable = true)
97
    @XmlElement(name = "Link")
98
    @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
99
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
100
    @Merge(MergeMode.ADD_CLONE)
101
	private Set<ExternalLink> links = new HashSet<>();
102

    
103
//***************** CONSTRUCTOR ***********************/
104

    
105
	//for hibernate use only
106
	protected OriginalSourceBase() {
107

    
108
	}
109

    
110
	/**
111
	 * Constructor
112
	 * @param type2
113
	 */
114
	protected OriginalSourceBase(OriginalSourceType type){
115
		if (type == null){
116
			throw new IllegalArgumentException("OriginalSourceType must not be null");
117
		}
118
		this.type = type;
119
	}
120

    
121
//**************** GETTER / SETTER *******************************/
122

    
123

    
124
	@Override
125
	public String getIdInSource(){
126
		return this.idInSource;
127
	}
128
	@Override
129
	public void setIdInSource(String idInSource){
130
		this.idInSource = idInSource;
131
	}
132

    
133

    
134
	@Override
135
	public String getIdNamespace() {
136
		return idNamespace;
137
	}
138
	@Override
139
	public void setIdNamespace(String idNamespace) {
140
		this.idNamespace = idNamespace;
141
	}
142

    
143

    
144
	@Override
145
	public OriginalSourceType getType() {
146
		return type;
147
	}
148
	@Override
149
	public void setType(OriginalSourceType type) {
150
		Assert.notNull(type, "OriginalSourceType must not be null");
151
		this.type = type;
152
	}
153

    
154
//********************** External Links **********************************************
155

    
156
    public Set<ExternalLink> getLinks(){
157
        return this.links;
158
    }
159
    public void setLinks(Set<ExternalLink> links){
160
        this.links = links;
161
    }
162
    public void addLink(ExternalLink link){
163
        if (link != null){
164
            links.add(link);
165
        }
166
    }
167
    public void removeLink(ExternalLink link){
168
        if(links.contains(link)) {
169
            links.remove(link);
170
        }
171
    }
172

    
173
//********************** CLONE ************************************************/
174

    
175
	@Override
176
	public Object clone() throws CloneNotSupportedException{
177
		OriginalSourceBase<?> result = (OriginalSourceBase<?>)super.clone();
178

    
179
		Set<ExternalLink> links = new HashSet<>();
180
		result.setLinks(links);
181
		for(ExternalLink link : this.links){
182
		    result.addLink((ExternalLink)link.clone());
183
		}
184

    
185
		//no changes to: idInSource
186
		return result;
187
	}
188

    
189

    
190
//************************ toString ***************************************/
191
	@Override
192
	public String toString(){
193
		if (StringUtils.isNotBlank(idInSource) || StringUtils.isNotBlank(idNamespace) ){
194
			return "OriginalSource:" + CdmUtils.concat(":", idNamespace, idInSource);
195
		}else{
196
			return super.toString();
197
		}
198
	}
199

    
200
//*********************************** EQUALS *********************************************************/
201

    
202
	/**
203
     * {@inheritDoc}
204
     */
205
    @Override
206
    public boolean equalsByShallowCompare(ReferencedEntityBase other) {
207

    
208
        if(!super.equalsByShallowCompare(other)) {
209
            return false;
210
        }
211
        OriginalSourceBase<T> theOther = (OriginalSourceBase<T>)other;
212
        if(!StringUtils.equals(this.getIdInSource(), theOther.getIdInSource())
213
                || !StringUtils.equals(this.getIdNamespace(), theOther.getIdNamespace())) {
214
            return false;
215
        }
216

    
217
        return true;
218
    }
219

    
220
}
(30-30/37)