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
|
import java.util.UUID;
|
13
|
|
14
|
import javax.persistence.Basic;
|
15
|
import javax.persistence.FetchType;
|
16
|
import javax.persistence.ManyToOne;
|
17
|
import javax.persistence.MappedSuperclass;
|
18
|
import javax.xml.bind.annotation.XmlAccessType;
|
19
|
import javax.xml.bind.annotation.XmlAccessorType;
|
20
|
import javax.xml.bind.annotation.XmlElement;
|
21
|
import javax.xml.bind.annotation.XmlIDREF;
|
22
|
import javax.xml.bind.annotation.XmlSchemaType;
|
23
|
import javax.xml.bind.annotation.XmlType;
|
24
|
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
25
|
|
26
|
import org.apache.log4j.Logger;
|
27
|
import org.hibernate.annotations.Type;
|
28
|
import org.hibernate.envers.Audited;
|
29
|
import org.hibernate.search.annotations.Analyze;
|
30
|
import org.hibernate.search.annotations.Field;
|
31
|
import org.hibernate.search.annotations.FieldBridge;
|
32
|
import org.joda.time.DateTime;
|
33
|
|
34
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
35
|
import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
|
36
|
import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
|
37
|
import eu.etaxonomy.cdm.strategy.match.Match;
|
38
|
import eu.etaxonomy.cdm.strategy.match.MatchMode;
|
39
|
|
40
|
/**
|
41
|
* The class keeps track of versions via a full linked list to different version objects, or a simple updated/updatedBy property in the same object.
|
42
|
*
|
43
|
* Full versioning allows concrete subclasses to keep track of previous or later versions of an object.
|
44
|
* A different version is another (persistent) java object, but with the same UUID.
|
45
|
* The version history is established as a linked list of the version objects in time.
|
46
|
* If versioning via the linked list is used, updated/updatedBy is the same as created/createdBy (better NULL?).
|
47
|
*
|
48
|
* Versioning can be turned off and in this case this class provides updated/updatedBy to keep track of the latest change event.
|
49
|
*
|
50
|
* @author m.doering
|
51
|
* @created 08-Nov-2007 13:07:01
|
52
|
*
|
53
|
* @param <T>
|
54
|
*/
|
55
|
@XmlAccessorType(XmlAccessType.FIELD)
|
56
|
@XmlType(name = "VersionableEntity", propOrder = {
|
57
|
"updated",
|
58
|
"updatedBy"
|
59
|
})
|
60
|
@XmlJavaTypeAdapter(value=DateTimeAdapter.class,type=DateTime.class)
|
61
|
@MappedSuperclass
|
62
|
@Audited
|
63
|
public abstract class VersionableEntity extends CdmBase implements IVersionableEntity{
|
64
|
private static final long serialVersionUID = 1409299200302758513L;
|
65
|
@SuppressWarnings("unused")
|
66
|
private static final Logger logger = Logger.getLogger(VersionableEntity.class);
|
67
|
|
68
|
@XmlElement(name ="Updated", type = String.class)
|
69
|
@XmlJavaTypeAdapter(DateTimeAdapter.class)
|
70
|
//@XmlElement(name ="Updated")
|
71
|
//@XmlElement(name ="Updated")
|
72
|
@Type(type="dateTimeUserType")
|
73
|
@Basic(fetch = FetchType.LAZY)
|
74
|
@Match(MatchMode.IGNORE)
|
75
|
@Field(analyze = Analyze.NO)
|
76
|
@FieldBridge(impl = DateTimeBridge.class)
|
77
|
private DateTime updated;
|
78
|
|
79
|
@XmlElement(name = "UpdatedBy")
|
80
|
@XmlIDREF
|
81
|
@XmlSchemaType(name = "IDREF")
|
82
|
@ManyToOne(fetch=FetchType.LAZY)
|
83
|
@Match(MatchMode.IGNORE)
|
84
|
private User updatedBy;
|
85
|
|
86
|
@Override
|
87
|
public User getUpdatedBy(){
|
88
|
return this.updatedBy;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
*
|
93
|
* @param updatedBy updatedBy
|
94
|
*/
|
95
|
@Override
|
96
|
public void setUpdatedBy(User updatedBy){
|
97
|
this.updatedBy = updatedBy;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
*
|
102
|
* @return
|
103
|
*/
|
104
|
@Override
|
105
|
public DateTime getUpdated(){
|
106
|
return this.updated;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
*
|
111
|
* @param updated updated
|
112
|
*/
|
113
|
@Override
|
114
|
public void setUpdated(DateTime updated){
|
115
|
this.updated = updated;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Is true if UUID and created timestamp are the same for the passed Object and this one.
|
120
|
* @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
|
121
|
* See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
|
122
|
* or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
|
123
|
* for more information about equals and hashcode.
|
124
|
*/
|
125
|
@Override
|
126
|
public boolean equals(Object obj) {
|
127
|
if (obj == this){
|
128
|
return true;
|
129
|
}
|
130
|
if (obj == null){
|
131
|
return false;
|
132
|
}
|
133
|
if (!CdmBase.class.isAssignableFrom(obj.getClass())){
|
134
|
return false;
|
135
|
}
|
136
|
ICdmBase cdmObj = (ICdmBase)obj;
|
137
|
boolean uuidEqual;
|
138
|
UUID objUuid = cdmObj.getUuid();
|
139
|
if (objUuid == null){
|
140
|
throw new NullPointerException("CdmBase is missing UUID");
|
141
|
}
|
142
|
uuidEqual = objUuid.equals(this.getUuid());
|
143
|
//TODO is this still needed?
|
144
|
boolean createdEqual = CdmUtils.nullSafeEqual(cdmObj.getCreated(), this.getCreated());
|
145
|
if (! uuidEqual || !createdEqual){
|
146
|
return false;
|
147
|
}
|
148
|
return true;
|
149
|
}
|
150
|
|
151
|
|
152
|
//********************** CLONE *****************************************/
|
153
|
|
154
|
/**
|
155
|
* Clones this versionable entity.
|
156
|
* Set fields for nextVersion, previousVersion, updated, updatedBy and createdBy are set to <tt>null</tt>
|
157
|
* The id is set to 0.
|
158
|
* The uuid is created new.
|
159
|
* The createdWhen is set to the current date.
|
160
|
* @see java.lang.Object#clone()
|
161
|
*/
|
162
|
@Override
|
163
|
public Object clone() throws CloneNotSupportedException{
|
164
|
VersionableEntity result = (VersionableEntity)super.clone();
|
165
|
|
166
|
result.setUpdated(null);
|
167
|
result.setUpdatedBy(null);
|
168
|
|
169
|
//no changes to: -
|
170
|
return result;
|
171
|
}
|
172
|
}
|