Project

General

Profile

Download (11.2 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.logging.log4j.LogManager;
38
import org.apache.logging.log4j.Logger;
39
import org.hibernate.annotations.Cascade;
40
import org.hibernate.annotations.CascadeType;
41
import org.hibernate.annotations.Type;
42
import org.hibernate.envers.Audited;
43
import org.springframework.util.Assert;
44

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

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

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

    
82
	private static final long serialVersionUID = -1972959999261181462L;
83
	@SuppressWarnings("unused")
84
	private static final Logger logger = LogManager.getLogger(OriginalSourceBase.class);
85

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

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

    
104
	@XmlElement(name = "IdNamespace")
105
	private String idNamespace;
106

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

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

    
119
    @XmlElement(name = "Accessed", type= String.class)
120
    private TimePeriod accessed = TimePeriod.NewInstance();
121

    
122
    @XmlElement(name = "OriginalInfo")
123
    private String originalInfo;
124

    
125
    @XmlElement(name = "CdmSource")
126
    @XmlIDREF
127
    @XmlSchemaType(name = "IDREF")
128
    @OneToOne(fetch = FetchType.EAGER, orphanRemoval=true)  //EAGER to avoid LIEs cdmSource should always be part of the OriginalSource itself
129
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
130
    private CdmLinkSource cdmSource;
131

    
132
    @XmlElementWrapper(name = "Links", nillable = true)
133
    @XmlElement(name = "Link")
134
    @OneToMany(fetch=FetchType.LAZY, orphanRemoval=true)
135
    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
136
    @Merge(MergeMode.ADD_CLONE)
137
	private Set<ExternalLink> links = new HashSet<>();
138

    
139
//***************** CONSTRUCTOR ***********************/
140

    
141
    //for hibernate use only, protected required by subclasses
142
    @Deprecated
143
	protected OriginalSourceBase(){}
144

    
145
	protected OriginalSourceBase(OriginalSourceType type){
146
		if (type == null){
147
			throw new IllegalArgumentException("OriginalSourceType must not be null");
148
		}
149
		this.type = type;
150
	}
151

    
152
//**************** GETTER / SETTER *******************************/
153

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

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

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

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

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

    
200
    public TimePeriod getAccessed() {
201
        return accessed;
202
    }
203
    public void setAccessed(TimePeriod accessed) {
204
        this.accessed = accessed;
205
    }
206

    
207
    public String getOriginalInfo(){
208
        return this.originalInfo;
209
    }
210
    public void setOriginalInfo(String originalInfo){
211
        this.originalInfo = originalInfo;
212
    }
213

    
214
	@Override
215
    public ICdmTarget getCdmSource() {
216
        return cdmSource == null? null: cdmSource.getTarget();
217
    }
218
     /* this method was implemented in the context of the CdmLinkSourceBeanProcessor which is unused
219
      *  method is preserved for the time when the REST API will be revised (#8637)
220
    @Override
221
    public CdmLinkSource getCdmSource() {
222
        if(cdmSource != null){
223
            logger.error("NOT NULL");
224
        }
225
        return cdmSource;
226
    }
227
	*/
228

    
229
//	@Override
230
//    public void setCdmSource(CdmLinkSource cdmSource) {
231
//        this.cdmSource = cdmSource;
232
//    }
233

    
234
    @Override
235
    public void setCdmSource(ICdmTarget cdmTarget){
236
        if (cdmTarget != null){
237
            this.cdmSource = CdmLinkSource.NewInstance(cdmTarget);
238
        }else{
239
            if (cdmSource != null){
240
                cdmSource.setTarget(null);  //as long as orphan-removal does not work #9801
241
            }
242
            this.cdmSource = null;
243
        }
244
    }
245

    
246
//********************** External Links **********************************************
247

    
248
    public Set<ExternalLink> getLinks(){
249
        return this.links;
250
    }
251
    public void setLinks(Set<ExternalLink> links){
252
        this.links = links;
253
    }
254
    public void addLink(ExternalLink link){
255
        if (link != null){
256
            links.add(link);
257
        }
258
    }
259
    public void removeLink(ExternalLink link){
260
        if(links.contains(link)) {
261
            links.remove(link);
262
        }
263
    }
264

    
265
//********************** CLONE ************************************************/
266

    
267
	@Override
268
	public OriginalSourceBase clone() throws CloneNotSupportedException{
269

    
270
        OriginalSourceBase result = (OriginalSourceBase)super.clone();
271

    
272
		Set<ExternalLink> links = new HashSet<>();
273
		result.setLinks(links);
274
		for(ExternalLink link : this.links){
275
		    result.addLink(link.clone());
276
		}
277

    
278
		if (this.cdmSource != null){
279
		    result.setCdmSource(this.cdmSource.getTarget());
280
		}
281

    
282
		//no changes to: type, idInSource, idNamespace,
283
		//   citation, citationMicroReference, originalInfo
284
		return result;
285
	}
286

    
287
// **************** EMPTY ************************/
288

    
289
    @Override
290
    protected boolean checkEmpty(){
291
        return checkEmpty(false);
292
    }
293

    
294
    /**
295
     * Checks if the source is completely empty.
296
     *
297
     * @param excludeType if <code>true</code> the source type
298
     * is ignored for the check.
299
     *
300
     * @see #checkEmpty()
301
     * @return <code>true</code> if empty
302
     */
303
    public boolean checkEmpty(boolean excludeType){
304
	   return super.checkEmpty()
305
	        && (excludeType || this.type == null)
306
	        && this.getCitation() == null
307
	        && isBlank(this.getCitationMicroReference())
308
	        && isBlank(this.getOriginalInfo())
309
	        && isBlank(this.getIdInSource())
310
	        && isBlank(this.getIdNamespace())
311
	        && this.links.isEmpty()
312
	        && this.cdmSource == null
313
           ;
314
	}
315

    
316
//************************ toString ***************************************/
317
	@Override
318
	public String toString(){
319
		if (isNotBlank(idInSource) || isNotBlank(idNamespace) ){
320
			return "OriginalSource:" + CdmUtils.concat(":", idNamespace, idInSource);
321
		}else{
322
			return super.toString();
323
		}
324
	}
325

    
326
//*********************************** EQUALS *********************************************************/
327

    
328
    /**
329
     * Indicates whether some other object is "equal to" this one.
330
     *
331
     * Uses a content based compare strategy which avoids bean initialization. This is achieved by
332
     * comparing the cdm entity ids.
333
     */
334
    public boolean equalsByShallowCompare(OriginalSourceBase other) {
335

    
336
        int thisCitationId = -1;
337
        int otherCitationId = -1;
338
        if(this.getCitation() != null) {
339
            thisCitationId = this.getCitation().getId();
340
        }
341
        if(other.getCitation() != null) {
342
            otherCitationId = other.getCitation().getId();
343
        }
344

    
345
        if(thisCitationId != otherCitationId
346
                || !StringUtils.equals(this.getCitationMicroReference(), other.getCitationMicroReference())
347
                || !StringUtils.equals(this.getOriginalInfo(), other.getOriginalInfo())
348
                        ){
349
            return false;
350
        }
351

    
352
        OriginalSourceBase theOther = other;
353
        if(!StringUtils.equals(this.getIdInSource(), theOther.getIdInSource())
354
                || !CdmUtils.nullSafeEqual(this.getIdNamespace(), theOther.getIdNamespace())
355
                || !CdmUtils.nullSafeEqual(this.getType(), theOther.getType())
356
                || !CdmUtils.nullSafeEqual(this.getCdmSource(), theOther.getCdmSource())
357
                || !CdmUtils.nullSafeEqual(this.getLinks(), theOther.getLinks())) {
358
            return false;
359
        }
360

    
361
        return true;
362
    }
363
}
(34-34/41)