diff --git a/src/groovy/grails/plugin/audittrail/AuditTrailHelper.groovy b/src/groovy/grails/plugin/audittrail/AuditTrailHelper.groovy index 3c1e2cc..08484d5 100644 --- a/src/groovy/grails/plugin/audittrail/AuditTrailHelper.groovy +++ b/src/groovy/grails/plugin/audittrail/AuditTrailHelper.groovy @@ -1,5 +1,6 @@ package grails.plugin.audittrail +import gorm.FieldProps import org.apache.log4j.Logger import org.codehaus.groovy.grails.commons.GrailsApplication import org.springframework.beans.factory.InitializingBean @@ -9,11 +10,6 @@ import org.springframework.context.ApplicationContextAware class AuditTrailHelper implements ApplicationContextAware, InitializingBean { private static final Logger log = Logger.getLogger(AuditTrailInterceptor) - private static final String CREATED_DATE_FIELD = "createdDate" - private static final String EDITED_DATE_FIELD = "editedDate" - private static final String CREATED_BY_FIELD = "createdBy" - private static final String EDITED_BY_FIELD = "editedBy" - Closure currentUserClosure //injected @@ -40,11 +36,11 @@ class AuditTrailHelper implements ApplicationContextAware, InitializingBean { void setFieldDefaults(Object entity) { Long time = System.currentTimeMillis() //assume its a new entity - [CREATED_DATE_FIELD, EDITED_DATE_FIELD].each { key -> + [FieldProps.CREATED_DATE_KEY, FieldProps.EDITED_DATE_KEY].each { key -> setDateField(entity, key, time) } - [CREATED_BY_FIELD, EDITED_BY_FIELD].each { key -> + [FieldProps.CREATED_BY_KEY, FieldProps.EDITED_BY_KEY].each { key -> setUserField(entity, key) } } @@ -83,12 +79,12 @@ class AuditTrailHelper implements ApplicationContextAware, InitializingBean { * @return boolean */ boolean isNewEntity(def entity) { - String createdDateFieldName = fieldPropsMap.get(CREATED_DATE_FIELD).name + String createdDateFieldName = fieldPropsMap.get(FieldProps.CREATED_DATE_KEY).name MetaProperty createdDateProperty = entity.hasProperty(createdDateFieldName) //see issue#41 if(createdDateProperty != null) { - Date existingValue = entity.getProperty(createdDateFieldName) + def existingValue = createdDateProperty.getProperty(entity) return (existingValue == null) } else { def session = applicationContext.sessionFactory.currentSession diff --git a/src/groovy/grails/plugin/audittrail/AuditTrailInterceptor.groovy b/src/groovy/grails/plugin/audittrail/AuditTrailInterceptor.groovy index 4ee2db7..6d6d704 100644 --- a/src/groovy/grails/plugin/audittrail/AuditTrailInterceptor.groovy +++ b/src/groovy/grails/plugin/audittrail/AuditTrailInterceptor.groovy @@ -1,12 +1,10 @@ package grails.plugin.audittrail +import gorm.FieldProps +import org.apache.commons.lang.ArrayUtils +import org.apache.log4j.Logger import org.hibernate.EmptyInterceptor import org.hibernate.type.Type -import org.apache.log4j.Logger -import org.springframework.context.ApplicationContextAware -import org.springframework.context.ApplicationContext -import org.springframework.beans.factory.InitializingBean -import org.apache.commons.lang.ArrayUtils class AuditTrailInterceptor extends EmptyInterceptor { private static final Logger log = Logger.getLogger(AuditTrailInterceptor) @@ -35,12 +33,12 @@ class AuditTrailInterceptor extends EmptyInterceptor { if(disableAuditTrailStamp(entity)) return true def time = System.currentTimeMillis() - ['createdDate','editedDate'].each{ key-> + [FieldProps.CREATED_DATE_KEY, FieldProps.EDITED_DATE_KEY].each{ key-> def valToSet = auditTrailHelper.setDateField(entity,key, time) if(valToSet) setValue(currentState, propertyNames, fieldPropsMap.get(key).name, valToSet) } - ['createdBy','editedBy'].each{ key-> + [FieldProps.CREATED_BY_KEY, FieldProps.EDITED_BY_KEY].each{ key-> def valToSet = auditTrailHelper.setUserField(entity,key) if(valToSet) setValue(currentState, propertyNames, fieldPropsMap.get(key).name, valToSet) diff --git a/src/java/gorm/AuditStampASTTransformation.java b/src/java/gorm/AuditStampASTTransformation.java index 986e744..a90133f 100644 --- a/src/java/gorm/AuditStampASTTransformation.java +++ b/src/java/gorm/AuditStampASTTransformation.java @@ -48,11 +48,11 @@ public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) { doBeforeValidate(classNode); //debugFieldNodes(classNode); - createUserField( classNode, fprops.get("editedBy")); - createUserField( classNode, fprops.get("createdBy")); + createUserField( classNode, fprops.get(FieldProps.EDITED_BY_KEY)); + createUserField( classNode, fprops.get(FieldProps.CREATED_BY_KEY)); - createDateField( classNode, fprops.get("editedDate")); - createDateField( classNode, fprops.get("createdDate")); + createDateField( classNode, fprops.get(FieldProps.EDITED_DATE_KEY)); + createDateField( classNode, fprops.get(FieldProps.CREATED_DATE_KEY)); } } diff --git a/src/java/gorm/FieldProps.java b/src/java/gorm/FieldProps.java index 2999dc2..4897261 100644 --- a/src/java/gorm/FieldProps.java +++ b/src/java/gorm/FieldProps.java @@ -6,7 +6,13 @@ class FieldProps { private static final String DATE_CONS = "nullable:false, display:false, editable:false, bindable:false"; private static final String USER_CONS = "nullable:false, display:false, editable:false, bindable:false"; - + + public static final String CREATED_DATE_KEY = "createdDate"; + public static final String EDITED_DATE_KEY = "editedDate"; + public static final String CREATED_BY_KEY = "createdBy"; + public static final String EDITED_BY_KEY = "editedBy"; + + String name; Class type; //Object initValue; @@ -51,11 +57,11 @@ public static FieldProps init(String defaultName,String defaultType, String defa public static Map buildFieldMap(ConfigObject config){ Map map = new HashMap(); - map.put("createdBy",FieldProps.init("createdBy","java.lang.Long",USER_CONS,null,config)); - map.put("editedBy",FieldProps.init("editedBy","java.lang.Long",USER_CONS,null,config)); + map.put(CREATED_BY_KEY,FieldProps.init(CREATED_BY_KEY,"java.lang.Long",USER_CONS,null,config)); + map.put(EDITED_BY_KEY,FieldProps.init(EDITED_BY_KEY,"java.lang.Long",USER_CONS,null,config)); - map.put("editedDate",FieldProps.init("editedDate","java.util.Date",DATE_CONS,null,config)); - map.put("createdDate",FieldProps.init("createdDate","java.util.Date",DATE_CONS,null,config)); + map.put(EDITED_DATE_KEY,FieldProps.init(EDITED_DATE_KEY,"java.util.Date",DATE_CONS,null,config)); + map.put(CREATED_DATE_KEY,FieldProps.init(CREATED_DATE_KEY,"java.util.Date",DATE_CONS,null,config)); return map; }