Skip to content

Commit

Permalink
small clean up
Browse files Browse the repository at this point in the history
- Move field name constants to FieldProps
- get created date field value directly from metabean property
  • Loading branch information
snimavat committed Oct 6, 2017
1 parent 7301555 commit 49e5ae7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
14 changes: 5 additions & 9 deletions src/groovy/grails/plugin/audittrail/AuditTrailHelper.groovy
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/groovy/grails/plugin/audittrail/AuditTrailInterceptor.groovy
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/java/gorm/AuditStampASTTransformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}
}
Expand Down
16 changes: 11 additions & 5 deletions src/java/gorm/FieldProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,11 +57,11 @@ public static FieldProps init(String defaultName,String defaultType, String defa

public static Map<String, FieldProps> buildFieldMap(ConfigObject config){
Map<String, FieldProps> map = new HashMap<String, FieldProps>();
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;
}

Expand Down

0 comments on commit 49e5ae7

Please sign in to comment.