Project

General

Profile

Download (6.74 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.model.common;
2

    
3
import java.beans.PropertyChangeEvent;
4
import java.beans.PropertyChangeListener;
5
import java.beans.PropertyChangeSupport;
6
import java.io.Serializable;
7
import java.util.Calendar;
8
import java.util.UUID;
9

    
10
import javax.persistence.Basic;
11
import javax.persistence.FetchType;
12
import javax.persistence.GeneratedValue;
13
import javax.persistence.Id;
14
import javax.persistence.ManyToOne;
15
import javax.persistence.MappedSuperclass;
16
import javax.persistence.Temporal;
17
import javax.persistence.TemporalType;
18
import javax.persistence.Transient;
19

    
20
import org.hibernate.annotations.Cascade;
21
import org.hibernate.annotations.CascadeType;
22

    
23
import eu.etaxonomy.cdm.model.agent.Person;
24

    
25

    
26

    
27
/**
28
 * The base class for all CDM domain classes implementing UUIDs and bean property change event firing.
29
 * It provides a globally unique UUID and keeps track of creation date and person.
30
 * The UUID is the same for different versions (see {@link VersionableEntity}) of a CDM object, so a locally unique id exists in addition 
31
 * that allows to safely access and store several objects (=version) with the same UUID.
32
 * 
33
 * This class together with the {@link eu.etaxonomy.cdm.aspectj.PropertyChangeAspect} 
34
 * will fire bean change events to all registered listeners. Listener registration and event firing
35
 * is done with the help of the {@link PropertyChangeSupport} class.
36
 * 
37
 * @author markus
38
 *
39
 */
40
/**
41
 * @author markus
42
 *
43
 */
44
@MappedSuperclass
45
public abstract class CdmBase implements Serializable{
46
	private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
47
	private int id;
48
	private UUID uuid;
49
	private Calendar created;
50
	private Person createdBy;
51

    
52
	/**
53
	 * Class constructor assigning a unique UUID and creation date.
54
	 * UUID can be changed later via setUuid method.
55
	 */
56
	public CdmBase() {
57
		this.uuid = UUID.randomUUID();
58
		this.setCreated(Calendar.getInstance());
59
	}
60

    
61
	
62
	/**
63
	 * see {@link PropertyChangeSupport#addPropertyChangeListener(PropertyChangeListener)}
64
	 * @param listener
65
	 */
66
	public void addPropertyChangeListener(PropertyChangeListener listener) {
67
		propertyChangeSupport.addPropertyChangeListener(listener);
68
	}
69

    
70
	/**
71
	 * see {@link PropertyChangeSupport#addPropertyChangeListener(String, PropertyChangeListener)}
72
	 * @param propertyName
73
	 * @param listener
74
	 */
75
	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
76
		propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
77
	}
78

    
79
	/**
80
	 * see {@link PropertyChangeSupport#addPropertyChangeListener(PropertyChangeListener)}
81
	 * @param listener
82
	 */
83
	public void removePropertyChangeListener(PropertyChangeListener listener) {
84
		propertyChangeSupport.removePropertyChangeListener(listener);
85
	}
86
	
87
	/**
88
	 * @see PropertyChangeSupport#addPropertyChangeListener(String, PropertyChangeListener)
89
	 */
90
	public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
91
		propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
92
	}
93
	
94
	@Transient
95
	public boolean hasListeners(String propertyName) {
96
		return propertyChangeSupport.hasListeners(propertyName);
97
	}
98

    
99
	public void firePropertyChange(String property, String oldval, String newval) {
100
		propertyChangeSupport.firePropertyChange(property, oldval, newval);
101
	}
102
	public void firePropertyChange(String property, int oldval, int newval) {
103
		propertyChangeSupport.firePropertyChange(property, oldval, newval);
104
	}
105
	public void firePropertyChange(String property, float oldval, float newval) {
106
		propertyChangeSupport.firePropertyChange(property, oldval, newval);
107
	}
108
	public void firePropertyChange(String property, boolean oldval, boolean newval) {
109
		propertyChangeSupport.firePropertyChange(property, oldval, newval);
110
	}
111
	public void firePropertyChange(String property, Object oldval, Object newval) {
112
		propertyChangeSupport.firePropertyChange(property, oldval, newval);
113
	}
114
	public void firePropertyChange(PropertyChangeEvent evt) {
115
		propertyChangeSupport.firePropertyChange(evt);
116
	}
117

    
118
	/**
119
	 * Returns local unique identifier for the concrete subclass
120
	 * @return
121
	 */
122
	@Id
123
	@GeneratedValue(generator = "system-increment")
124
	public int getId() {
125
		return this.id;
126
	}
127
	/**
128
	 * Assigns a unique local ID to this object. 
129
	 * Because of the EJB3 @Id and @GeneratedValue annotation this id will be
130
	 * set automatically by the persistence framework when object is saved.
131
	 * @param id
132
	 */
133
	public void setId(int id) {
134
		this.id = id;
135
	}
136

    
137
	
138
	/**
139
	 * Method for hibernate only to read the UUID value as a simple string from the object and persist it (e.g. in a database).
140
	 * For reading the UUID please use getUuid method
141
	 * @return String representation of the UUID
142
	 */
143
	private String getStrUuid() {
144
		return this.uuid.toString();
145
	}
146
	/**
147
	 * Method for hibernate only to set the UUID value as a simple string as it was stored in the persistence layer (e.g. a database).
148
	 * For setting the UUID please use setUuid method
149
	 */
150
	private void setStrUuid(String uuid) {
151
		this.uuid = UUID.fromString(uuid);
152
	}
153
	
154
	
155
	@Transient
156
	public UUID getUuid() {
157
		return this.uuid;
158
	}
159
	public void setUuid(UUID uuid) {
160
		this.uuid = uuid;
161
	}
162

    
163
	
164
	@Temporal(TemporalType.TIMESTAMP)
165
	@Basic(fetch = FetchType.LAZY)
166
	public Calendar getCreated() {
167
		return created;
168
	}
169
	/**
170
	 * Sets the timestamp this object was created. 
171
	 * Most databases cannot store milliseconds, so they are removed by this method.
172
	 * Caution: We are planning to replace the Calendar class with a different datetime representation which is more suitable for hibernate
173
	 * see {@link http://dev.e-taxonomy.eu/trac/ticket/247 TRAC ticket} 
174
	 * 
175
	 * @param created
176
	 */
177
	public void setCreated(Calendar created) {
178
		if (created != null){
179
			created.set(Calendar.MILLISECOND, 0);
180
		}
181
		this.created = created;
182
	}
183

    
184

    
185
	@ManyToOne(fetch=FetchType.LAZY)
186
	@Cascade( { CascadeType.SAVE_UPDATE })
187
	public Person getCreatedBy() {
188
		return this.createdBy;
189
	}
190
	public void setCreatedBy(Person createdBy) {
191
		this.createdBy = createdBy;
192
	}
193

    
194
	
195
	/**
196
	 * Is true if UUID and created timestamp are the same for the passed Object and this one.
197
	 * @see java.lang.Object#equals(java.lang.Object)
198
	 */
199
	@Override
200
	public boolean equals(Object obj) {
201
		if (obj == null){
202
			return false;
203
		}else if (CdmBase.class.isAssignableFrom(obj.getClass())){
204
			CdmBase cdmObj = (CdmBase)obj;
205
			boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
206
			boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
207
			if (uuidEqual && createdEqual){
208
				return true;
209
			}
210
		}
211
		return false;
212
	}
213
	
214
	/**
215
	 * Returns the class, id and uuid as a string for any CDM object. 
216
	 * For example: Taxon#13<b5938a98-c1de-4dda-b040-d5cc5bfb3bc0>
217
	 * @see java.lang.Object#toString()
218
	 */
219
	@Override
220
	public String toString() {
221
		return this.getClass().getSimpleName()+"#"+this.getId()+"<"+this.getUuid()+">";
222
	}
223
	
224
}
(4-4/42)