Project

General

Profile

Download (10.7 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.ManyToOne;
22
import javax.persistence.OneToMany;
23
import javax.persistence.OneToOne;
24
import javax.persistence.Table;
25
import javax.validation.constraints.NotNull;
26
import javax.xml.bind.annotation.XmlAccessType;
27
import javax.xml.bind.annotation.XmlAccessorType;
28
import javax.xml.bind.annotation.XmlAttribute;
29
import javax.xml.bind.annotation.XmlElement;
30
import javax.xml.bind.annotation.XmlElementWrapper;
31
import javax.xml.bind.annotation.XmlIDREF;
32
import javax.xml.bind.annotation.XmlRootElement;
33
import javax.xml.bind.annotation.XmlSchemaType;
34
import javax.xml.bind.annotation.XmlType;
35

    
36
import org.apache.commons.lang3.StringUtils;
37
import org.apache.log4j.Logger;
38
import org.hibernate.annotations.Cascade;
39
import org.hibernate.annotations.CascadeType;
40
import org.hibernate.annotations.Type;
41
import org.hibernate.envers.Audited;
42
import org.springframework.util.Assert;
43

    
44
import eu.etaxonomy.cdm.common.CdmUtils;
45
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
46
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
47
import eu.etaxonomy.cdm.model.media.ExternalLink;
48
import eu.etaxonomy.cdm.strategy.merge.Merge;
49
import eu.etaxonomy.cdm.strategy.merge.MergeMode;
50

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

    
59
@XmlAccessorType(XmlAccessType.FIELD)
60
@XmlType(name = "OriginalSource", propOrder = {
61
    "type",
62
	"idInSource",
63
    "idNamespace",
64
    "originalNameString",
65
    "citation",
66
    "citationMicroReference",
67
    "cdmSource",
68
    "links"
69
})
70
@XmlRootElement(name = "OriginalSource")
71
@Entity
72
@Audited
73
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
74
@Table(name="OriginalSourceBase")
75
public abstract class OriginalSourceBase
76
        extends AnnotatableEntity
77
        implements IOriginalSource, IIntextReferenceTarget {
78

    
79
	private static final long serialVersionUID = -1972959999261181462L;
80
	@SuppressWarnings("unused")
81
	private static final Logger logger = Logger.getLogger(OriginalSourceBase.class);
82

    
83
	/**
84
	 * The {@link OriginalSourceType type} of this source. According to PROV the type has to be thought as
85
	 * an activity that leads from the source entity to the current entity. It is not a property of the
86
	 * source itself.
87
	 */
88
	@XmlAttribute(name ="type")
89
	@Column(name="sourceType")
90
	@NotNull
91
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
92
    	parameters = {@org.hibernate.annotations.Parameter(name="enumClass", value="eu.etaxonomy.cdm.model.reference.OriginalSourceType")}
93
    )
94
	@Audited
95
	private OriginalSourceType type;
96

    
97
	//The object's ID in the source, where the alternative string comes from
98
	@XmlElement(name = "IdInSource")
99
	private String idInSource;
100

    
101
	@XmlElement(name = "IdNamespace")
102
	private String idNamespace;
103

    
104
    @XmlElement(name = "Citation")
105
    @XmlIDREF
106
    @XmlSchemaType(name = "IDREF")
107
    @ManyToOne(fetch = FetchType.LAZY)
108
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
109
    private Reference citation;
110

    
111
    //Details of the reference. These are mostly (implicitly) pages but can also be tables or any other element of a
112
    //publication. {if the citationMicroReference exists then there must be also a reference}
113
    @XmlElement(name = "CitationMicroReference")
114
    private String citationMicroReference;
115

    
116
    @XmlElement(name = "OriginalNameString")
117
    private String originalNameString;
118

    
119
    @XmlElement(name = "CdmSource")
120
    @XmlIDREF
121
    @XmlSchemaType(name = "IDREF")
122
    @OneToOne(fetch = FetchType.EAGER, orphanRemoval=true)  //EAGER to avoid LIEs cdmSource should always be part of the OriginalSource itself
123
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
124
    private CdmLinkSource cdmSource;
125

    
126
    @XmlElementWrapper(name = "Links", nillable = true)
127
    @XmlElement(name = "Link")
128
    @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
129
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
130
    @Merge(MergeMode.ADD_CLONE)
131
	private Set<ExternalLink> links = new HashSet<>();
132

    
133
//***************** CONSTRUCTOR ***********************/
134

    
135
	//for hibernate use only
136
	protected OriginalSourceBase() {
137
	}
138

    
139
	/**
140
	 * Constructor
141
	 * @param type
142
	 */
143
	protected OriginalSourceBase(OriginalSourceType type){
144
		if (type == null){
145
			throw new IllegalArgumentException("OriginalSourceType must not be null");
146
		}
147
		this.type = type;
148
	}
149

    
150
//**************** GETTER / SETTER *******************************/
151

    
152
    @Override
153
    public OriginalSourceType getType() {
154
        return type;
155
    }
156
    @Override
157
    public void setType(OriginalSourceType type) {
158
        Assert.notNull(type, "OriginalSourceType must not be null");
159
        this.type = type;
160
    }
161

    
162
	@Override
163
	public String getIdInSource(){
164
		return this.idInSource;
165
	}
166
	@Override
167
	public void setIdInSource(String idInSource){
168
		this.idInSource = idInSource;
169
	}
170

    
171
	@Override
172
	public String getIdNamespace() {
173
		return idNamespace;
174
	}
175
	@Override
176
	public void setIdNamespace(String idNamespace) {
177
		this.idNamespace = idNamespace;
178
	}
179

    
180
    @Override
181
    public Reference getCitation(){
182
        return this.citation;
183
    }
184
    @Override
185
    public void setCitation(Reference citation) {
186
        this.citation = citation;
187
    }
188

    
189
    @Override
190
    public String getCitationMicroReference(){
191
        return this.citationMicroReference;
192
    }
193
    @Override
194
    public void setCitationMicroReference(String citationMicroReference){
195
        this.citationMicroReference = citationMicroReference;
196
    }
197

    
198
    public String getOriginalNameString(){
199
        return this.originalNameString;
200
    }
201
    public void setOriginalNameString(String originalNameString){
202
        this.originalNameString = originalNameString;
203
    }
204

    
205
	@Override
206
    public ICdmTarget getCdmSource() {
207
        return cdmSource == null? null: cdmSource.getTarget();
208
    }
209
     /* this method was implemented in the context of the CdmLinkSourceBeanProcessor which is unused
210
      *  method is preserved for the time when the REST API will be revised (#8637)
211
    @Override
212
    public CdmLinkSource getCdmSource() {
213
        if(cdmSource != null){
214
            logger.error("NOT NULL");
215
        }
216
        return cdmSource;
217
    }
218
	*/
219

    
220

    
221
//	@Override
222
//    public void setCdmSource(CdmLinkSource cdmSource) {
223
//        this.cdmSource = cdmSource;
224
//    }
225
    @Override
226
    public void setCdmSource(ICdmTarget cdmTarget){
227
        if (cdmTarget != null){
228
            this.cdmSource = CdmLinkSource.NewInstance(cdmTarget);
229
        }else{
230
            this.cdmSource = null;
231
        }
232
    }
233

    
234
//********************** External Links **********************************************
235

    
236
    public Set<ExternalLink> getLinks(){
237
        return this.links;
238
    }
239
    public void setLinks(Set<ExternalLink> links){
240
        this.links = links;
241
    }
242
    public void addLink(ExternalLink link){
243
        if (link != null){
244
            links.add(link);
245
        }
246
    }
247
    public void removeLink(ExternalLink link){
248
        if(links.contains(link)) {
249
            links.remove(link);
250
        }
251
    }
252

    
253
//********************** CLONE ************************************************/
254

    
255
	@Override
256
	public OriginalSourceBase clone() throws CloneNotSupportedException{
257

    
258
        OriginalSourceBase result = (OriginalSourceBase)super.clone();
259

    
260
		Set<ExternalLink> links = new HashSet<>();
261
		result.setLinks(links);
262
		for(ExternalLink link : this.links){
263
		    result.addLink(link.clone());
264
		}
265

    
266
		if (this.cdmSource != null){
267
		    result.setCdmSource(this.cdmSource.getTarget());
268
		}
269

    
270
		//no changes to: type, idInSource, idNamespace,
271
		//   citation, citationMicroReference, originalNameString
272
		return result;
273
	}
274

    
275
// **************** EMPTY ************************/
276

    
277
    @Override
278
    protected boolean checkEmpty(){
279
        return checkEmpty(false);
280
    }
281

    
282
    /**
283
     * Checks if the source is completely empty.
284
     *
285
     * @param excludeType if <code>true</code> the source type
286
     * is ignored for the check.
287
     *
288
     * @see #checkEmpty()
289
     * @return <code>true</code> if empty
290
     */
291
    public boolean checkEmpty(boolean excludeType){
292
	   return super.checkEmpty()
293
	        && (excludeType || this.type == null)
294
	        && this.getCitation() == null
295
	        && isBlank(this.getCitationMicroReference())
296
	        && isBlank(this.getOriginalNameString())
297
	        && isBlank(this.getIdInSource())
298
	        && isBlank(this.getIdNamespace())
299
	        && this.links.isEmpty()
300
	        && this.cdmSource == null
301
           ;
302
	}
303

    
304
//************************ toString ***************************************/
305
	@Override
306
	public String toString(){
307
		if (isNotBlank(idInSource) || isNotBlank(idNamespace) ){
308
			return "OriginalSource:" + CdmUtils.concat(":", idNamespace, idInSource);
309
		}else{
310
			return super.toString();
311
		}
312
	}
313

    
314
//*********************************** EQUALS *********************************************************/
315

    
316
    /**
317
     * Indicates whether some other object is "equal to" this one.
318
     *
319
     * Uses a content based compare strategy which avoids bean initialization. This is achieved by
320
     * comparing the cdm entity ids.
321
     */
322
    public boolean equalsByShallowCompare(OriginalSourceBase other) {
323

    
324
        int thisCitationId = -1;
325
        int otherCitationId = -1;
326
        if(this.getCitation() != null) {
327
            thisCitationId = this.getCitation().getId();
328
        }
329
        if(other.getCitation() != null) {
330
            otherCitationId = other.getCitation().getId();
331
        }
332

    
333
        if(thisCitationId != otherCitationId
334
                || !StringUtils.equals(this.getCitationMicroReference(), other.getCitationMicroReference())
335
                || !StringUtils.equals(this.getOriginalNameString(), other.getOriginalNameString())
336
                        ){
337
            return false;
338
        }
339

    
340
        OriginalSourceBase theOther = other;
341
        if(!StringUtils.equals(this.getIdInSource(), theOther.getIdInSource())
342
                || !CdmUtils.nullSafeEqual(this.getIdNamespace(), theOther.getIdNamespace())
343
                || !CdmUtils.nullSafeEqual(this.getType(), theOther.getType())
344
                || !CdmUtils.nullSafeEqual(this.getCdmSource(), theOther.getCdmSource())
345
                || !CdmUtils.nullSafeEqual(this.getLinks(), theOther.getLinks())) {
346
            return false;
347
        }
348

    
349
        return true;
350
    }
351
}
(34-34/41)