1

Let's say i have the following domain classes: The first domain class Tag.groovy allows me to build a dynamic category structure with different layers (layer1, layer2 and layer3). Each activity from Activity.groovy belongs to a certain category and is connected to its category via ActivityTag.groovy. So far so good. :)

Tag.groovy

class Tag {
    String name
    String layer
    Tag parent
    static transients = ['activityCount', 'children']
    int activityCount
    static constraints = {
        name    nullable: false, unique: true
        layer   nullable: false, inList:['generic','layer1','layer2','layer3']
        parent  nullable: true
    }
    Set<Tag> getChildren() {
        return Tag.findAllByParent(this) as Set
    }
    Set<Activity> getActivities() {
        return ActivityTag.findAllByTag(this)*.activity
    }
    Set<Activity> getActivities(VirtualCity virtualCity) {
        def activitiesWithTag = ActivityTag.findAllByTag(this)*.activity
        return activitiesWithTag.findAll({it.virtualCity==virtualCity})
    }
    def getActivityCount(){
        if (this.layer == "layer1") {
            return this.children*.children*.activityCount.sum().sum()
        } else if (this.layer == "layer2") {
            return this.children*.activityCount.sum()
        } else {
            return this.getActivities().size()
        }
    }
    def getActivityCount(VirtualCity virtualCity){
        if (this.layer == "layer1") {
            return this.children*.children*.activityCount.sum().sum()
        } else if (this.layer == "layer2") {
            return this.children*.activityCount.sum()
        } else {
            return this.getActivities(virtualCity).size()
        }
    }
}

Activity.groovy

class ActivityTag {console
    Activity activity
    Tag tag
    static mapping = {
        id composite: ['activity', 'tag']
        version false
    }
}

ActivityTag.groovy

class Activity {
    VirtualCity virtualCity
    String name
}

Now I would like to render my categories as JSON in a tree view. Can you tell me how I can achieve this?

I tried render(template: "tree", collection: Tag.findAllByLayer("layer1"), var: 'tag')

with this template _tree.gson:

model {
    Tag tag
}

json {
    id              tag.id
    name            tag.name
    activityCount   tag?.activityCount ?: null
    children        g.render(template: 'tree', collection: tag?.children, var: 'tag') ?: null
}

But this method fails with java.lang.reflect.InvocationTargetException: null

feuernurmitm
  • 312
  • 2
  • 11

0 Answers0