Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Improve Neo4j GraphDB usability for Groovy/Grails #66

Closed
make opened this issue Sep 12, 2012 · 0 comments
Closed

Improve Neo4j GraphDB usability for Groovy/Grails #66

make opened this issue Sep 12, 2012 · 0 comments
Assignees
Labels

Comments

@make
Copy link

make commented Sep 12, 2012

This in grails-app/conf/BootStrap.groovy makes neo4j rock in Groovy/Grails:

def init = { servletContext ->
    def getOrSet = { name, value = null ->
        if(value) {
            delegate.setProperty(name,value instanceof Date ? value.time : value)
        } else {
            def val = delegate.getProperty(name, null)
            if(val instanceof Long && name.endsWith('Date'))
                return new Date(val)
            return val
        }
    }
    NodeProxy.metaClass.propertyMissing = getOrSet
    RelationshipProxy.metaClass.propertyMissing = getOrSet
    RestNode.metaClass.propertyMissing = getOrSet
    Relationship.metaClass.propertyMissing = getOrSet

    JSON.registerObjectMarshaller(NodeProxy) { n ->
        def m = [:]
        m.id = n.id
        n.propertyKeys.each { k ->
            m[(k)] = n."$k"
        }
        m.relationships = n.relationships.collect{it}
        m
    }
    JSON.registerObjectMarshaller(RelationshipProxy) { r ->
        def m = [:]
        m.id = r.id
        m.type = r.type.name()
        m.startNode = r.startNode.id
        m.endNode = r.endNode.id
        r.propertyKeys.each { k ->
            m[(k)] = r."$k"
        }
        m
    }
}

Maybe it would be worth to add something like this into the neo4j Grails plugin. ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants