Hi,

Many people are using the DTO "pattern" in their webapps to transfer data back and 
forth between their persistence and presentation layers.  For those who decide to 
adopt this pattern, they find themselves with two different object models: the 
persistence (domain) model which is mapped to an underlying data store and a Java Bean 
model that is used by the JSPs and Struts actions or what have you.  The problem with 
this is that you find yourself writing a tonne of this kind of get()/set() code:

  domainObject.setName(beanObject.getName());

Additonally, if you are using some kind of O/R mapping runtime, you want to do a set() 
only if it required, to limit the number of SQL UPDATE statements, so you have code 
like this everywhere:

if ( !(domainObject.getName().equals(beanObject.getName())) ) {
        domainObject.setName(beanObject.getName());
}

I am half way through a set of Jelly tags that allow me succinctly describe the rules 
involved in this kind of situation.

The above Java can be stated as

        <graph:set from="name" to="name"/>

(assuming domainObject and beanObject are in the JellyContext and named as "target" 
and "source").

Sometimes more complex mappings are required:

<graph:map name="person.site" sourceKeys="country.id,language.id" 
targetKeys="country.countryCode,language.languageCode">
    <graph:change>
        <graph:lookup target="site" sourceKeys="country.id,language.id"/>
    </graph:change>
    <graph:prune objects="site"/>
</graph:map>

What this says is "if source.getCountry().getId() and source.getLanguage().getId() are 
different to target.getCountry().getCountryCode() and 
target.getLanguage().getLanguageCode() resp, then set target.site to the result of a 
lookup based on specified params.  (lookup uses an ObjectManager.lookup() instance, 
available from the context).  Alternatively, if either of the sourceKeys is missing or 
valueless, prune target.site".

Now, I guess this is more likely to make sense if, like me, you are using JDO as your 
persistence runtime.  This allows the graph mapper to surf references as it needs, 
driven by the Jelly script.  But, what I wanted to know is is there a better way to do 
this, is somebody else working on something similar and, if not, whether you would 
consider sucking these tags into the Jelly dist and allow many minds to make them 
better ;-)

Thanks a mil,
Mike.

PS I initially looked at using JXPath to do this, but that is good at pulling data out 
of a graph, but not so good at transforming a target graph based on the content of a 
source graph.  Pruning is the biggest problem I see, but I am open to correction.

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to