commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dewitte Rémi <r...@gide.net>
Subject [BeanUtils] Re: LazyDynaBean in a pojo
Date Tue, 19 Jul 2005 17:48:45 GMT
I wrote this method, can i have your comments for improvements ?

	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 18:27, Dewitte Rémi a écrit :
> Hi !
> I use LazyDynaBean to gather results from the user. In my main
> LazyDynaBean, I've elements which are subclasses of LazyDynaBean and theses
> elements can be indexed (LazyDynaBean[]). I do this because I'm interested
> in lazy features for indexed properties and mapped properties.
>
> Now I'd like to pass this LazyDynaBean to a corresponding POJO.
>
> I tried to create my LazyDynaForm like this :
> public class LazyForm extends LazyValidatorForm {
>
> 	public LazyForm(){
> 		super((new WrapDynaBean(new PojoForm())));
> 	}
> But interesting lazy features are lost...
>
> An another way with BeanUtils.copyProperties();
> For simple elements (String or even String[]), everything is good with
> BeanUtils.copyProperties(pojo,lazybean). The problem is when I want to copy
> a LazyElement to a PojoElement (inside the PojoForm) : how can I handle
> this.
>
> How would you do this ?
>
> Rémi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message