Project

General

Profile

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

    
31
import org.apache.commons.lang.StringUtils;
32
import org.apache.log4j.Logger;
33
import org.hibernate.annotations.Cascade;
34
import org.hibernate.annotations.CascadeType;
35
import org.hibernate.annotations.Table;
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.media.ExternalLink;
42
import eu.etaxonomy.cdm.strategy.merge.Merge;
43
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
44

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

    
53
@XmlAccessorType(XmlAccessType.FIELD)
54
@XmlType(name = "OriginalSource", propOrder = {
55
    "type",
56
	"idInSource",
57
    "idNamespace"
58
})
59
@XmlRootElement(name = "OriginalSource")
60
@Entity
61
@Audited
62
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
63
@Table(appliesTo="OriginalSourceBase")
64
public abstract class OriginalSourceBase<T extends ISourceable>
65
        extends ReferencedEntityBase
66
        implements IOriginalSource<T>, IIntextReferenceTarget,Cloneable {
67
	private static final long serialVersionUID = -1972959999261181462L;
68
	@SuppressWarnings("unused")
69
	private static final Logger logger = Logger.getLogger(OriginalSourceBase.class);
70

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

    
85
	//The object's ID in the source, where the alternative string comes from
86
	@XmlElement(name = "IdInSource")
87
	private String idInSource;
88

    
89
	@XmlElement(name = "IdNamespace")
90
	private String idNamespace;
91

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

    
99
//***************** CONSTRUCTOR ***********************/
100

    
101
	//for hibernate use only
102
	protected OriginalSourceBase() {
103

    
104
	}
105

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

    
117
//**************** GETTER / SETTER *******************************/
118

    
119

    
120
	@Override
121
	public String getIdInSource(){
122
		return this.idInSource;
123
	}
124
	@Override
125
	public void setIdInSource(String idInSource){
126
		this.idInSource = idInSource;
127
	}
128

    
129

    
130
	@Override
131
	public String getIdNamespace() {
132
		return idNamespace;
133
	}
134
	@Override
135
	public void setIdNamespace(String idNamespace) {
136
		this.idNamespace = idNamespace;
137
	}
138

    
139

    
140
	@Override
141
	public OriginalSourceType getType() {
142
		return type;
143
	}
144
	@Override
145
	public void setType(OriginalSourceType type) {
146
		Assert.notNull(type, "OriginalSourceType must not be null");
147
		this.type = type;
148
	}
149

    
150
//********************** External Links **********************************************
151

    
152
    public Set<ExternalLink> getLinks(){
153
        return this.links;
154
    }
155
    public void addLink(ExternalLink link){
156
        if (link != null){
157
            links.add(link);
158
        }
159
    }
160
    public void removeLink(ExternalLink link){
161
        if(links.contains(link)) {
162
            links.remove(link);
163
        }
164
    }
165

    
166
//********************** CLONE ************************************************/
167

    
168
	@Override
169
	public Object clone() throws CloneNotSupportedException{
170
		OriginalSourceBase<?> result = (OriginalSourceBase<?>)super.clone();
171

    
172
		//no changes to: idInSource
173
		return result;
174
	}
175

    
176

    
177
//************************ toString ***************************************/
178
	@Override
179
	public String toString(){
180
		if (StringUtils.isNotBlank(idInSource) || StringUtils.isNotBlank(idNamespace) ){
181
			return "OriginalSource:" + CdmUtils.concat(":", idNamespace, idInSource);
182
		}else{
183
			return super.toString();
184
		}
185
	}
186

    
187
//*********************************** EQUALS *********************************************************/
188

    
189
	/**
190
     * {@inheritDoc}
191
     */
192
    @Override
193
    public boolean equalsByShallowCompare(ReferencedEntityBase other) {
194

    
195
        if(!super.equalsByShallowCompare(other)) {
196
            return false;
197
        }
198
        OriginalSourceBase<T> theOther = (OriginalSourceBase<T>)other;
199
        if(!StringUtils.equals(this.getIdInSource(), theOther.getIdInSource())
200
                || !StringUtils.equals(this.getIdNamespace(), theOther.getIdNamespace())) {
201
            return false;
202
        }
203

    
204
        return true;
205
    }
206

    
207
}
(58-58/80)