GRAILS: Save Nesting Objects with XML Data

I created a REST service in Grails to accept data from a python script. The python script generates an XML representation of the object graph and submits it to the controller. Things are for my flat object Very useful, but I can’t figure out how to deal with the situation where the domain object contains a set of sub-objects. For unrelated reasons, my DOA layer is pure Java JPA.

For example, my domain Class (leave getter / setters / etc):

class Schedule {
String name;
@OneToMany;
HashSet steps ;
}

class Step {
String name;
@ManyToOne;
Schedule schedule;
}

My python script generates the following XML:


Foo


Bar


Blatz


In my controller I have this:

def save = {< br /> def schedInstance = new Schedule(params['schedule'])
...
}

The steps attribute will never be filled. If I dump params to In the log, the step data is all stuck together (in the above example, it will generate the step: “BarBlatz”

I must do something very wrong. I think this is a common Task. Everything I can find about nested objects is related to the command object. If it can be avoided, I don’t want to copy my domain object code in the command object.

def save = {
def schedInstance = new Schedule(params['schedule'])
params ['schedule?.step'].each{stepXml->
schedInstance.addTo(new step(stepXml)
}
}

Untested, but I I’ve done something similar before. Once I have a chance to test it, I will edit it as needed.

I create a REST service in Grails to accept python scripts Data. The python script generates an XML representation of the object graph and submits it to the controller. Things are useful for my flat objects, but I can’t figure out how to handle the case where the domain object contains a set of sub-objects. For unrelated reasons , My DOA layer is pure Java JPA.

For example, my domain class (leave getter/setters/etc):

class Schedule {
String name;
@OneToMany;
HashSet steps;
}

class Step {
String name;< br /> @ManyToOne;
Schedule schedule;
}

My python script generates the following XML:

 
Foo


Bar


Blatz


In my control I have this in the device:

def save = {
def schedInstance = new Schedule(params['schedule'])
...
}

The steps attribute will never be filled .If I dump the params into the log, the step data is all stuck together (in the example above, it will produce the step: “BarBlatz”

I must do something very wrong. I think this is a common task. Everything I can find about nested objects is related to the command object. If it can be avoided, I don’t want to copy my domain object code in the command object.

def save = {
def schedInstance = new Schedule(params['schedule'])
params['schedule?.step'].each {stepXml->
schedInstance.addTo(new step(stepXml)
}
}

Untested, but I have done something similar before. Once I have Opportunity to test it, I will edit it as needed.

Leave a Comment

Your email address will not be published.