Re: Auto PageParameters from a POJO?
you can create some reusable bookmarkablelink like below public class ReleaseBookmarkablePageLink extends BookmarkablePageLink{ private Release release; public ReleaseBookmarkablePageLink(String id,ClasspageClass,Release release){ super(id,pageClass); this.release=release; } @Override protected void onInitialize() { super.onInitialize(); getPageParameters().set("product", release.getProduct().getName()); getPageParameters().set("version", release.getVersion() ); } } if you don't want to serialize release object,you can just serialize it's key and retrieve release object from it On Tue, Sep 18, 2012 at 7:39 AM, Ondrej Zizka wrote: > Hi, > > I found myself repeatedly creating a PageParameters object from some > domain object for BookmarkablePageLink just to have it then "parsed" > back to that same domain object. > Example: > > Release rel { product: AS; version: 7.1.2 } > => > add( new BookmarkablePageLink ( "link", ReleasePage.class, new > PageParameters() > .add("product", rel.getProduct().getName()) > .add("version", rel.getVersion() ) > ) ); > > So to avoid repeating that all the time, I created (besides a link > component) this in ReleasePage: > > public static PageParameters createPageParameters( Release rel ){ > return new PageParameters() > .add("product", rel.getProduct().getName()) > .add("version", rel.getVersion() ); > } > > And I was thinking - is there some mechanism to automatically create the > params from the domain object, using properties matching against mount() > string? > E.g. like this: > > mountPage("/release/${product.name}/${version}", ReleasePage.class); > new BookmarkablePageLink ( "link", ReleasePage.class, rel); // > This would create page params the properties. > > Anything like this available? If not, is it doable? It would reduce > quite some boilerplate code. > > Thanks, > Ondra > > -- regards, Vineet Semwal - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Auto PageParameters from a POJO?
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 map = new HashMap(); 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 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: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Auto PageParameters from a POJO?
Hi, On Tue, Sep 18, 2012 at 5:09 AM, Ondrej Zizka wrote: > Hi, > > I found myself repeatedly creating a PageParameters object from some > domain object for BookmarkablePageLink just to have it then "parsed" > back to that same domain object. > Example: > > Release rel { product: AS; version: 7.1.2 } > => > add( new BookmarkablePageLink ( "link", ReleasePage.class, new > PageParameters() > .add("product", rel.getProduct().getName()) > .add("version", rel.getVersion() ) > ) ); > > So to avoid repeating that all the time, I created (besides a link > component) this in ReleasePage: > > public static PageParameters createPageParameters( Release rel ){ > return new PageParameters() > .add("product", rel.getProduct().getName()) > .add("version", rel.getVersion() ); > } > > And I was thinking - is there some mechanism to automatically create the > params from the domain object, using properties matching against mount() > string? > E.g. like this: > > mountPage("/release/${product.name}/${version}", ReleasePage.class); > new BookmarkablePageLink ( "link", ReleasePage.class, rel); // > This would create page params the properties. > > Anything like this available? If not, is it doable? It would reduce > quite some boilerplate code. No, there is no such functionality in Wicket. Whether it is doable - yes, everything is doable. But your approach is much more simpler. If something like this should be in the framework then BookmarkablePageLink (and other clients of this API) should use a helper that will have to find the respective IRequestMapper that is responsible for that page, then this mapper will have to expose some of its internals to be able to read its page class and named parameters placeholders and finally it should use some Expression Language parser to match the placeholders against the passed POJO ... You see that it is much more complex. > > Thanks, > Ondra > > -- Martin Grigorov jWeekend Training, Consulting, Development http://jWeekend.com - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Auto PageParameters from a POJO?
Hi, I found myself repeatedly creating a PageParameters object from some domain object for BookmarkablePageLink just to have it then "parsed" back to that same domain object. Example: Release rel { product: AS; version: 7.1.2 } => add( new BookmarkablePageLink ( "link", ReleasePage.class, new PageParameters() .add("product", rel.getProduct().getName()) .add("version", rel.getVersion() ) ) ); So to avoid repeating that all the time, I created (besides a link component) this in ReleasePage: public static PageParameters createPageParameters( Release rel ){ return new PageParameters() .add("product", rel.getProduct().getName()) .add("version", rel.getVersion() ); } And I was thinking - is there some mechanism to automatically create the params from the domain object, using properties matching against mount() string? E.g. like this: mountPage("/release/${product.name}/${version}", ReleasePage.class); new BookmarkablePageLink ( "link", ReleasePage.class, rel); // This would create page params the properties. Anything like this available? If not, is it doable? It would reduce quite some boilerplate code. Thanks, Ondra