Roll your own, e.g. using commons BeanUtils:

import junit.framework.Assert;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.wicket.request.mapper.parameter.INamedParameters;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.junit.Test;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class PageParametersTest {

    @Test
    public void testPageParameters() {
        // toPageParameters
        MyBean bean = new MyBean("foo", "bar");
        final PageParameters pageParameters = toPageParameters(bean);
        Assert.assertEquals(2, pageParameters.getNamedKeys().size());
        Assert.assertEquals("foo", pageParameters.get("foo").toString());
        Assert.assertEquals("bar", pageParameters.get("bar").toString());
        // repopulate                   
        MyBean newBean = new MyBean();
        populate(newBean, pageParameters);
        Assert.assertEquals("foo", newBean.getFoo());
        Assert.assertEquals("bar", newBean.getBar());
    }

    public static PageParameters toPageParameters(Object object) {
        final PageParameters params = new PageParameters();
        try {
            final Map map = BeanUtils.describe(object);
            for (Object key : map.keySet()) {
                if (!"class".equals(key))   // We don't want the result of 
getClass()
                    params.set((String) key, map.get(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
    }

    public static void populate(Object bean, PageParameters parameters) {
        final Map<String, Object> map = new HashMap<String, Object>();
        for (INamedParameters.NamedPair namedPair : parameters.getAllNamed()) {
            map.put(namedPair.getKey(), namedPair.getValue());
        }
        try {
            BeanUtils.populate(bean, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class MyBean implements Serializable {
        private String foo;
        private String bar;

        public MyBean() {
        }

        public MyBean(String foo, String bar) {
            this.foo = foo;
            this.bar = bar;
        }

        public String getBar() { return bar; }
        public void setBar(String bar) { this.bar = bar; }
        public String getFoo() { return foo; }
        public void setFoo(String foo) { this.foo = foo; }
    }

}

   -Tom


On 18.09.2012, at 04:09, Ondrej Zizka <[email protected]> wrote:

> And I was thinking - is there some mechanism to automatically create the
> params from the domain object, using properties matching against mount()
> string?



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to