package com.coherentlogic.coherent.data.model.core.domain;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.coherentlogic.coherent.data.model.core.exceptions.CloneFailedException;
/**
* A class which all domain classes inherit from and that contains
* implementations of the hashCode, equals, and toString methods.
*
* http://rolandtapken.de/blog/2010-08/
* let-xstream-call-default-constructor-where-possible
*
* @author <a href="support@coherentlogic.com">Support</a>
*/
@Entity()
@Table(name=DefaultObject.DEFAULT_OBJECT)
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class DefaultObject implements Serializable {
private static final long serialVersionUID = -1914917862686826505L;
public static final String DEFAULT_OBJECT = "default_object";
private Long primaryKey = null;
// private transient final PropertyChangeSupport propertyChangeSupport;
public DefaultObject () {
// this.propertyChangeSupport = new PropertyChangeSupport (this);
}
@Override
public int hashCode () {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals (Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public Long getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(Long primaryKey) {
this.primaryKey = primaryKey;
}
/**
* Method returns a copy of the date parameter or null if the reference to
* the date is null.
*
* @param date The date to be cloned.
*/
protected static Date clone (Date date) {
Date newDate = null;
if (date != null)
newDate = (Date) date.clone();
return newDate;
}
public <T> T clone (Class<T> type) {
Object object = clone ();
T result = type.cast(object);
return result;
}
@Override
public Object clone () {
try {
return BeanUtils.cloneBean(this);
} catch (IllegalAccessException illegalAccessException) {
throw new CloneFailedException("Illegal access exception thrown",
illegalAccessException);
} catch (InstantiationException instantiationException) {
throw new CloneFailedException("Failed to instantiate the class.",
instantiationException);
} catch (InvocationTargetException invocationTargetException) {
throw new CloneFailedException(
"Invocation target exception thrown.",
invocationTargetException);
} catch (NoSuchMethodException noSuchMethodException) {
throw new CloneFailedException(
"No such method exception thrown.", noSuchMethodException);
}
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}