Hum
Hum

Reputation: 15

How to populate a List in the domain class in grails?

Hi folks I have a map in my bootstrap.groovy, how can use this map to populate another map in my domain class? here is the code

bootstrap.groovy

def data = [ x:'45.5',
             y:'7',
             z:[ z0:'2.5', z1:'3.5', z2:'4.0', z3:'3.5', z4:'5.0']
           ]

what code should I write here?

//some code

domain class

class target implements Serializable{
  //Just for the time being
  List list = new ArrayList()
}

any ideas would be appreciated

Upvotes: 0

Views: 1930

Answers (2)

Victor Sergienko
Victor Sergienko

Reputation: 13475

I believe it's better to use Grails configuration and update Config.groovy.

Upvotes: 0

virtualeyes
virtualeyes

Reputation: 11237

First thing is your question asks how to populate one map with another map, but you define a list in your domain. So if I'm understanding you correctly, your domain would more likely be:

class Target implements Serializable {
    Map data = [:]
}

// BootStrap.groovy
import package.name.Target
class BootStrap {

    def grailsApplication

    def init = { servletContext ->

        // no need to quote your map keys in this case
        Map data = [x:"45.5", y:"7", z:[z0 :"2.5", z1:"3.5", z2:"4.0", z3:"3.5", z4:"5.0"]

        Target targ = new Target()
        targ.data = data.z // set Target data map to nested portion of map above
        targ.data = data // set equal (could add to ctor [data:"$data"] instead)
        data.each{k,v->
            // do some calc that changes local map values and applies to target data map
        }

        // if you are unable to get a reference to Target domain, you can try
        def inst = grailsApplication.getClassForName("package.name.Target").newInstance()
        inst.data = data
        // etc.
    }
}

Upvotes: 1

Related Questions