开发者

How to populate Cmd Object with subset of properties from Domain class?

开发者 https://www.devze.com 2023-01-25 04:05 出处:网络
I am developing a webflow based workflow and during the initialisation action I am trying to populate a set of command objects from a开发者_开发知识库 single domain class, with each command object con

I am developing a webflow based workflow and during the initialisation action I am trying to populate a set of command objects from a开发者_开发知识库 single domain class, with each command object containing just a subset of the fields available in the domain class... there's a LOT of fields you see..

What I'm struggling with is how to populate the 'properties' of each command object with just the matching properties from the domain class.

Has anyone had experience with this and knows how to accomplish it ?

Thanks

Dave


You could do the following:

class Domain {
    String lastName
    String firstName
    int age
}

class Command {
    String lastName
    int age
}

def domain = new Domain(lastName:'last', firstName:'first', age:33)

def command = new Command()
command.properties.findAll{ !["metaClass","class"].contains(it.key)}.each { k,v ->
   command[k] = domain[k]
}

assert 33 == command.age
assert 'last' == command.lastName    

The problem with .properties is that it includes 'class' and 'metaClass'. Setting these two a bad idea, so they're getting filtered out.

0

精彩评论

暂无评论...
验证码 换一张
取 消