import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
+import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.UUID;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlID;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSchemaType;
+import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
+
/**
* The base class for all CDM domain classes implementing UUIDs and bean property change event firing.
* It provides a globally unique UUID and keeps track of creation date and person.
* @author m.doering
*
*/
+@XmlAccessorType(XmlAccessType.PROPERTY)
+@XmlType(name = "CdmBase", propOrder = {
+ "created",
+ "createdBy"
+})
+@XmlRootElement(name = "CdmBase")
@MappedSuperclass
public abstract class CdmBase implements Serializable, ICdmBase{
+
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private int id;
- private UUID uuid;
+ private UUID uuid;
private Calendar created;
- private Person createdBy;
+ private Person createdBy;
/**
* Class constructor assigning a unique UUID and creation date.
propertyChangeSupport.firePropertyChange(evt);
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.model.common.ICdmBase#getId()
- */
- @Id
- @GeneratedValue(generator = "system-increment")
- public int getId() {
- return this.id;
- }
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.model.common.ICdmBase#setId(int)
- */
- public void setId(int id) {
- this.id = id;
- }
-
-
/**
* Method for hibernate only to read the UUID value as a simple string from the object and persist it (e.g. in a database).
* For reading the UUID please use getUuid method
* @return String representation of the UUID
*/
+ @XmlAttribute(name = "uuid", required = true)
+ @XmlID
+ @XmlSchemaType(name = "ID")
private String getStrUuid() {
return this.uuid.toString();
}
}
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.common.ICdmBase#getId()
+ */
+ @XmlAttribute(name = "id", required = true)
+ @Id
+ @GeneratedValue(generator = "system-increment")
+ public int getId() {
+ return this.id;
+ }
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.common.ICdmBase#setId(int)
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+
/* (non-Javadoc)
* @see eu.etaxonomy.cdm.model.common.ICdmBase#getUuid()
*/
+ @XmlTransient
@Transient
public UUID getUuid() {
return this.uuid;
/* (non-Javadoc)
* @see eu.etaxonomy.cdm.model.common.ICdmBase#getCreated()
*/
+ @XmlElement (name = "Created")
@Temporal(TemporalType.TIMESTAMP)
@Basic(fetch = FetchType.LAZY)
public Calendar getCreated() {
/* (non-Javadoc)
* @see eu.etaxonomy.cdm.model.common.ICdmBase#getCreatedBy()
*/
+ @XmlElement (name = "CreatedBy")
@ManyToOne(fetch=FetchType.LAZY)
@Cascade( { CascadeType.SAVE_UPDATE })
public Person getCreatedBy() {
/**
- * Is true if UUID and created timestamp are the same for the passed Object and this one.
+ * Is true if UUID is the same for the passed Object and this one.
* @see java.lang.Object#equals(java.lang.Object)
* See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
* or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
public int hashCode() {
int hashCode = 7;
hashCode = 29 * hashCode + this.getUuid().hashCode();
- //hashCode = 29 * hashCode + this.getCreated().hashCode();
return hashCode;
}
/**
- * Returns the class, id and uuid as a string for any CDM object.
+ * Overrides {@link java.lang.Object#toString()}.
+ * This returns an String that identifies the object well without beeing necessarily unique.
+ * Specification: This method should never call other object' methods so it can be well used for debugging
+ * without problems like lazy loading, unreal states etc.
+ * Note: If overriding this method's javadoc always copy or link the above requirement.
+ * If not overwritten by a subclass method returns the class, id and uuid as a string for any CDM object.
* For example: Taxon#13<b5938a98-c1de-4dda-b040-d5cc5bfb3bc0>
* @see java.lang.Object#toString()
*/
return this.getClass().getSimpleName()+"#"+this.getId()+"<"+this.getUuid()+">";
}
+ protected void invokeSetMethod(Method method, Object object){
+ try {
+ method.invoke(object, this);
+ } catch (Exception e) {
+ e.printStackTrace();
+ //TODO handle exceptioin;
+ }
+ }
+
+ protected void invokeSetMethodWithNull(Method method, Object object){
+ try {
+ Object[] nul = new Object[]{null};
+ method.invoke(object, nul);
+ } catch (Exception e) {
+ e.printStackTrace();
+ //TODO handle exceptioin;
+ }
+ }
+
+//********************** CLONE *****************************************/
+
+ protected void clone(CdmBase clone){
+ clone.setCreatedBy(createdBy);
+ clone.setId(id);
+ //Constructor Attributes
+ //clone.setCreated(created);
+ //clone.setUuid(getUuid());
+
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#clone()
+ */
+ public Object clone() throws CloneNotSupportedException{
+ CdmBase result = (CdmBase)super.clone();
+
+ //TODO ?
+ result.setId(0);
+ result.setUuid(UUID.randomUUID());
+ result.setCreated(Calendar.getInstance());
+ result.setCreatedBy(null);
+
+ //no changes to: -
+ return result;
+ }
+
}