Search code examples
hibernategrailsgrails-orm

Grails read GORM Mapping


I'm currently developing an AuditListener to create an History Entry for every DomainClass that I want to.

I created an AuditListener

class AuditingListener extends AbstractPersistenceEventListener {

public AuditingListener(final Datastore datastore){
    super(datastore)
}

@Override
protected void onPersistenceEvent(final AbstractPersistenceEvent event){
    switch(event.eventType){
        case EventType.PostInsert:
            onPostInsert(event as PostInsertEvent)
            break
        case EventType.PreUpdate:
            onPreUpdate(event as PreUpdateEvent)
            break
        case EventType.PreDelete:
            onPreDelete(event as PreDeleteEvent)
            break
    }
}

def onPreUpdate(PreUpdateEvent event){
    Object domainClassObject = event.entityObject
    PersistentEntity entity = event.entity

    if(isAuditableEntity(domainClassObject)){ 
        try {
            GrailsDomainClass grailsDomainClass = getGrailsDomainClass(domainClassObject)
            Set<String> propertyNames = getPropertyNames(grailsDomainClass)
            if(propertyNames){
                Map properties = getPropertiesMap(propertyNames, domainClassObject, grailsDomainClass)
                createRevisionEntity(entity, grailsDomainClass, domainClassObject, properties)
            }

        } catch(Exception e){
            log.error "Could not found Audit domainClassObject Class of ${domainClassObject}", e
        }
    }
}

Now I want to create the Method createRevisionEntity that should create the Revision for the current Entity. The Method should create an new Object called RevisionEntity and add dynamicly all the Properties of the current DomainClassObject. With that I would have an Revisioned Entity of any DomainClass I want, similiar to hibernate Envers.

I'm able to put dynamicly the properties to RevisionEntity but my Problem is that I didn't get the mapping Informations and put It in RevisionEntity.

Do U have any suggestions for me?


Solution

  • If found a solution to geht the column Mapping:

        GrailsDomainBinder grailsDomainBinder = new GrailsDomainBinder()
        grailsDomainBinder.getMapping(domainClassObject.class).columns?.each{column ->
            println column
        }