I wrote this method to achieve : it can perfectible i think.

        public static void copyProperties(Object pojo, DynaBean bean)
                        throws IllegalAccessException, 
InvocationTargetException,
                        ClassNotFoundException, InstantiationException {
                DynaProperty[] dynaProperties = 
bean.getDynaClass().getDynaProperties();
                for (DynaProperty d : dynaProperties) {
                        Object value = bean.get(d.getName());
                        Class type = d.getType();
                        Class c;

                        if (d.isIndexed())
                                c = type.getComponentType();
                        else
                                c = type;

                        // c'est un dynaBean
                        if (DynaBean.class.isAssignableFrom(c)) {
                                String pojoName = 
c.getName().replaceFirst("Lazy", "dao.Pojo");
                                if (d.getType().isArray()) {
                                        Object[] objs = (Object[]) 
Array.newInstance(Class.forName(pojoName),Array.getLength(value));
                                        for(int i =0 ; i<objs.length;i++){
                                                objs[i] = 
Class.forName(pojoName).newInstance();
                                                copyProperties(objs[i], 
(DynaBean) Array.get(value,i));
                                        }
                                        BeanUtils.copyProperty(pojo, 
d.getName(), objs);
                                } else {
                                        Object in = 
Class.forName(pojoName).newInstance();
                                        copyProperties(in, (DynaBean) value);
                                        BeanUtils.copyProperty(pojo, 
d.getName(), in);
                                }
                                // copie simple
                        } else {
                                BeanUtils.copyProperty(pojo, d.getName(), 
value);
                        }
                }
        }

Rémi
Le Mardi 19 Juillet 2005 14:52, Martin Gainty a écrit :
> Bonjour Remi
> Am glad that satisfies your requirement..
> Bon Chance,
> Martin-
> ----- Original Message -----
> From: "Dewitte Rémi" <[EMAIL PROTECTED]>
> To: "Struts Users Mailing List" <user@struts.apache.org>
> Sent: Tuesday, July 19, 2005 5:32 AM
> Subject: Re: saving a DynaForm
>
>
> I think i'll take your second option. I could make something work with
> serializable as said Martin (and confirmed by you), but if we decide a day
> to
> change the way to make data persistent, many frameworks exist for POJOs
> (eg. hibernate).
> And Benautils provides :
> BeanUtilsBean.populate(bean,map)
> BeanUtilsBean.copyProperties(bean,bean)
> So even i must maintain an other class, this is propably the best way.
> Thanks Martin and Laurie.
>
> Le Lundi 18 Juillet 2005 19:07, Laurie Harper a écrit :
> > Dewitte Rémi wrote:
> > > Hello !
> > > I uses LazyDynaForm to gather user results.
> > > Now I'd like to save those results. All persisence framework are for
> > > POJO, do you know a common solution ?
> > > May I create a POJO to make the transition/link ?
> >
> > It all depends on your requirements and what persistence technologies you
> > want to use. As Martin said, your LazyDynaForms are Serializable so you
> > can
> > write them to an object stream as with any other Serializable class. Many
> > persistence frameworks, including (most?) ORM tools can persist
> > Serializable instances directly.
> >
> > Your other option is to define a concrete POJO which you can map to
> > persistent storage using your chosen solution and copy data from your
> > LazyDyanForm into your POJO prior to persisting. BeanUtils can make it
> > pretty trivial (sometimes as little as a single line of code) to manage
> > the
> > copying of data between form beans and POJOs.
> >
> > If that doesn't help, you might want to post a more detailed description
> > of
> > what you want to do.
> >
> > L.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

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

Reply via email to