1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/main/java/ru/indvdum/jpa/entities/AbstractEntity.groovy Fri Nov 09 03:00:49 2012 +0400
1.3 @@ -0,0 +1,132 @@
1.4 +package ru.indvdum.jpa.entities
1.5 +
1.6 +import java.lang.reflect.Modifier
1.7 +
1.8 +import javax.persistence.EmbeddedId
1.9 +import javax.persistence.Entity
1.10 +import javax.persistence.Id
1.11 +import javax.persistence.Transient
1.12 +
1.13 +import org.apache.openjpa.util.OpenJPAId
1.14 +import org.codehaus.jackson.map.ObjectMapper
1.15 +
1.16 +import ru.indvdum.jpa.dao.JPADataAccessObject;
1.17 +
1.18 +/**
1.19 + * @author indvdum (gotoindvdum@gmail.com)
1.20 + * @since 25.12.2011 13:26:57
1.21 + *
1.22 + */
1.23 +abstract class AbstractEntity implements Serializable {
1.24 +
1.25 + @Transient
1.26 + protected ObjectMapper mapper = new ObjectMapper()
1.27 +
1.28 + @Override
1.29 + public String toString() {
1.30 + return getClass().getSimpleName() + ' ' + toJSON()
1.31 + }
1.32 +
1.33 + /**
1.34 + * JSON-like representation of an object
1.35 + *
1.36 + * @return
1.37 + */
1.38 + public String toJSON() {
1.39 + return mapper.writeValueAsString(this)
1.40 + }
1.41 +
1.42 + /*
1.43 + * HashCode, based on root class name of entity class hierarchy and id value
1.44 + *
1.45 + * (non-Javadoc)
1.46 + * @see java.lang.Object#hashCode()
1.47 + */
1.48 + @Override
1.49 + public int hashCode() {
1.50 + Class clazz = getClass()
1.51 + while (
1.52 + clazz.superclass != null
1.53 + && !Modifier.isAbstract(clazz.superclass.modifiers)
1.54 + && clazz.superclass.annotations.find {it instanceof Entity} != null
1.55 + )
1.56 + clazz = clazz.superclass
1.57 + String res = clazz.getName() + ': ' + getIdentifierValue()
1.58 + return res.hashCode()
1.59 + }
1.60 +
1.61 + /*
1.62 + * Comparison, based on entities id's
1.63 + *
1.64 + * (non-Javadoc)
1.65 + * @see java.lang.Object#equals(java.lang.Object)
1.66 + */
1.67 + @Override
1.68 + public boolean equals(Object obj) {
1.69 + if(obj == null)
1.70 + return false
1.71 + if(!(obj.class.isAssignableFrom(this.class) || this.class.isAssignableFrom(obj.class)))
1.72 + return false
1.73 + def id = getIdentifierValue()
1.74 + def objId = (obj as AbstractEntity).getIdentifierValue()
1.75 + return id == objId || id != null && id != this && id.equals(objId)
1.76 + }
1.77 +
1.78 + /**
1.79 + * @return The identifier value of an entity.
1.80 + */
1.81 + @Transient
1.82 + public Object getIdentifierValue() {
1.83 + // an attempt to get id by EntityManagerFactory
1.84 + Object identifier = JPADataAccessObject.emf.getPersistenceUnitUtil().getIdentifier(this)
1.85 + if(identifier instanceof OpenJPAId)
1.86 + return ((OpenJPAId)identifier).getIdObject()
1.87 +
1.88 + def result
1.89 +
1.90 + // an attempt to find id by annotated fields
1.91 + Class clazz = getClass()
1.92 + def self = this
1.93 + Collection fields = clazz.declaredFields
1.94 + while(clazz.superclass != null) {
1.95 + clazz = clazz.superclass
1.96 + fields.addAll(clazz.declaredFields)
1.97 + }
1.98 + fields.grep {
1.99 + !it.synthetic &&
1.100 + !Modifier.isStatic(it.modifiers) &&
1.101 + !Modifier.isTransient(it.modifiers)
1.102 + }.grep {
1.103 + it.annotations.find {
1.104 + it instanceof Id || it instanceof EmbeddedId
1.105 + } != null
1.106 + }.each {
1.107 + boolean isAccessible = it.accessible
1.108 + it.accessible = true
1.109 + result = it.get(self)
1.110 + it.accessible = isAccessible
1.111 + }
1.112 + if(result != null && !(result instanceof Reference && (result as Reference).value == null))
1.113 + return result
1.114 +
1.115 + // an attempt to represent id as a aggregate of all fields
1.116 + // TODO check, why toJSON method don't work
1.117 +// return toJSON()
1.118 + result = '{'
1.119 + fields.grep {
1.120 + !it.synthetic &&
1.121 + !Modifier.isStatic(it.modifiers) &&
1.122 + !Modifier.isTransient(it.modifiers)
1.123 + }.grep {
1.124 + it.annotations.find {
1.125 + it instanceof Transient
1.126 + } == null
1.127 + }.each {
1.128 + boolean isAccessible = it.accessible
1.129 + it.accessible = true
1.130 + result += '"' + it.name + '": "' + it.get(self) + '", '
1.131 + it.accessible = isAccessible
1.132 + }
1.133 + return result
1.134 + }
1.135 +}