You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Glen <gl...@model3.net> on 2005/12/09 18:45:08 UTC

adding a custom Type converter

I am converting a pre-existing webapp to use Tapestry 4.0.  It uses
hibernate for the business objects and long's for the uid's on those
objects.  I would like to use tapestry to coerce from the uid as a
string into the actual business object

Ultimately what I want to be able to do is to write a direct link like
this...


<span jwcid="@DirectLink" listener="listener:gotoDetails"
parameters="ognl:bizObject">view details</span>

public void gotoDetails( BizObject bizObject ) {
   // rest of code goes here
}

instead of how I am currently doing it as follows...

<span jwcid="@DirectLink" listener="listener:gotoDetails"
parameters="ognl:bizobject.uid">view details</span>

public void gotoDetails( Long  uid ) {
    BizObject bizObject = DAO.getBizObject( uid );
}


I know I need to use the tapestry.coerce.ValueConverter service.  Can
anyone point me in the right direction as to how I would implement this?



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


Re: adding a custom Type converter

Posted by an...@di.uoa.gr.
>From  Glen <gl...@model3.net>:

> 
> This worked wonderfully thanks...
> 
> One curiousity...  The "public static final char PREFIX = '*';"  Does
> this have to be globally unique among all the SqueezeAdaptor's?  I am
> assuming this must be unique to the squeeze adapter and that tapestry
> will use this to differentiate what adaptor created the squeezed string
> and hence what adaptor should unsqeeze the string.

You're right

> 
> Are there any guidelines about what prefixes are already taken?
> 

See the adaptors in package o.a.tapestry.util.io or in
http://jakarta.apache.org/tapestry/tapestry/clover/org/apache/tapestry/util/io/pkg-classes.html

Also, X is reserved for null, see o.a.t.util.io.DataSqueezerImpl
The prefix should also be between characters '!' and 'z'

> 
> 
> 
> Paul Cantrell wrote:
> > I've done this, and it works quite well. You need to implement the  
> > interface org.apache.tapestry.util.io.SqueezeAdaptor, something like  
> > this:
> > 
> > public class DomainObjectSqueezeAdaptor
> >      implements SqueezeAdaptor
> >      {
> >      public static final char PREFIX = '*';
> >      private static final Pattern squeezeFormat = Pattern.compile("\\* 
> > ([A-Za-z0-9_\\.\\$]+)_([0-9]+)");
> > 
> >      public String getPrefix()
> >          { return String.valueOf(PREFIX); }
> > 
> >      public Class getDataClass()
> >          { return DomainObject.class; }
> > 
> >      public String squeeze(DataSqueezer squeezer, Object data)
> >          { return squeeze(data); }
> > 
> >      public static String squeeze(Object data)
> >          {
> >          return PREFIX
> >               + data.getClass().getName()
> >               + '_'
> >               + ((DomainObject) data).getId();
> >          }
> > 
> >      public DomainObject unsqueeze(DataSqueezer squeezer, String s)
> >          { return unsqueeze(s); }
> > 
> >      public static DomainObject unsqueeze(String s)
> >          {
> >          Matcher matcher = squeezeFormat.matcher(s);
> >          if(!matcher.matches())
> >              throw new IllegalArgumentException("Unable to parse  
> > squeezed ID: " + s);
> >          String className = matcher.group(1);
> >          String id = matcher.group(2);
> > 
> >          Class type;
> >          try { type = Class.forName(className); }
> >          catch(ClassNotFoundException e)
> >              { throw new IllegalArgumentException("Unable to parse  
> > squeezed ID " + s, e); }
> > 
> >          return HibernateHelper.fromId((Class<? extends  
> > DomainObject>) type, id);
> >          }
> >      }
> > 
> > Then add this to your hivemodule.xml:
> > 
> >      <contribution configuration-id="tapestry.data.SqueezeAdaptors">
> >        <adaptor  
> > object="instance:net.innig.core.web.tapestry.DomainObjectSqueezeAdaptor" 
> > />
> >      </contribution>
> > 
> > Cheers,
> > 
> > Paul
> > 
> > 
> > On Dec 9, 2005, at 11:45 AM, Glen wrote:
> > 
> > 
> >>I am converting a pre-existing webapp to use Tapestry 4.0.  It uses
> >>hibernate for the business objects and long's for the uid's on those
> >>objects.  I would like to use tapestry to coerce from the uid as a
> >>string into the actual business object
> >>
> >>Ultimately what I want to be able to do is to write a direct link like
> >>this...
> >>
> >>
> >><span jwcid="@DirectLink" listener="listener:gotoDetails"
> >>parameters="ognl:bizObject">view details</span>
> >>
> >>public void gotoDetails( BizObject bizObject ) {
> >>   // rest of code goes here
> >>}
> >>
> >>instead of how I am currently doing it as follows...
> >>
> >><span jwcid="@DirectLink" listener="listener:gotoDetails"
> >>parameters="ognl:bizobject.uid">view details</span>
> >>
> >>public void gotoDetails( Long  uid ) {
> >>    BizObject bizObject = DAO.getBizObject( uid );
> >>}
> >>
> >>
> >>I know I need to use the tapestry.coerce.ValueConverter service.  Can
> >>anyone point me in the right direction as to how I would implement  
> >>this?
> >>
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >>
> >>
> > 
> > 
> > _________________________________________________________________
> > Piano music podcast: http://inthehands.com
> > Other interesting stuff: http://innig.net
> > 
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


-- 



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


Re: adding a custom Type converter

Posted by Glen <gl...@model3.net>.
This worked wonderfully thanks...

One curiousity...  The "public static final char PREFIX = '*';"  Does
this have to be globally unique among all the SqueezeAdaptor's?  I am
assuming this must be unique to the squeeze adapter and that tapestry
will use this to differentiate what adaptor created the squeezed string
and hence what adaptor should unsqeeze the string.

Are there any guidelines about what prefixes are already taken?




Paul Cantrell wrote:
> I've done this, and it works quite well. You need to implement the  
> interface org.apache.tapestry.util.io.SqueezeAdaptor, something like  
> this:
> 
> public class DomainObjectSqueezeAdaptor
>      implements SqueezeAdaptor
>      {
>      public static final char PREFIX = '*';
>      private static final Pattern squeezeFormat = Pattern.compile("\\* 
> ([A-Za-z0-9_\\.\\$]+)_([0-9]+)");
> 
>      public String getPrefix()
>          { return String.valueOf(PREFIX); }
> 
>      public Class getDataClass()
>          { return DomainObject.class; }
> 
>      public String squeeze(DataSqueezer squeezer, Object data)
>          { return squeeze(data); }
> 
>      public static String squeeze(Object data)
>          {
>          return PREFIX
>               + data.getClass().getName()
>               + '_'
>               + ((DomainObject) data).getId();
>          }
> 
>      public DomainObject unsqueeze(DataSqueezer squeezer, String s)
>          { return unsqueeze(s); }
> 
>      public static DomainObject unsqueeze(String s)
>          {
>          Matcher matcher = squeezeFormat.matcher(s);
>          if(!matcher.matches())
>              throw new IllegalArgumentException("Unable to parse  
> squeezed ID: " + s);
>          String className = matcher.group(1);
>          String id = matcher.group(2);
> 
>          Class type;
>          try { type = Class.forName(className); }
>          catch(ClassNotFoundException e)
>              { throw new IllegalArgumentException("Unable to parse  
> squeezed ID " + s, e); }
> 
>          return HibernateHelper.fromId((Class<? extends  
> DomainObject>) type, id);
>          }
>      }
> 
> Then add this to your hivemodule.xml:
> 
>      <contribution configuration-id="tapestry.data.SqueezeAdaptors">
>        <adaptor  
> object="instance:net.innig.core.web.tapestry.DomainObjectSqueezeAdaptor" 
> />
>      </contribution>
> 
> Cheers,
> 
> Paul
> 
> 
> On Dec 9, 2005, at 11:45 AM, Glen wrote:
> 
> 
>>I am converting a pre-existing webapp to use Tapestry 4.0.  It uses
>>hibernate for the business objects and long's for the uid's on those
>>objects.  I would like to use tapestry to coerce from the uid as a
>>string into the actual business object
>>
>>Ultimately what I want to be able to do is to write a direct link like
>>this...
>>
>>
>><span jwcid="@DirectLink" listener="listener:gotoDetails"
>>parameters="ognl:bizObject">view details</span>
>>
>>public void gotoDetails( BizObject bizObject ) {
>>   // rest of code goes here
>>}
>>
>>instead of how I am currently doing it as follows...
>>
>><span jwcid="@DirectLink" listener="listener:gotoDetails"
>>parameters="ognl:bizobject.uid">view details</span>
>>
>>public void gotoDetails( Long  uid ) {
>>    BizObject bizObject = DAO.getBizObject( uid );
>>}
>>
>>
>>I know I need to use the tapestry.coerce.ValueConverter service.  Can
>>anyone point me in the right direction as to how I would implement  
>>this?
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>
> 
> 
> _________________________________________________________________
> Piano music podcast: http://inthehands.com
> Other interesting stuff: http://innig.net
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 


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


Re: adding a custom Type converter

Posted by Cosmin Bucur <co...@gmail.com>.
So you need to configure a SqueezeAdaptor for each data object you are
using in tapestry ?

Also what's not clear to me is , does the tapestry end need to be
modified at all , or theese SqueezeAdaptors will  simply work
automatically on the existing tap4 code .

Thanks

On 12/9/05, Paul Cantrell <ca...@pobox.com> wrote:
> I've done this, and it works quite well. You need to implement the
> interface org.apache.tapestry.util.io.SqueezeAdaptor, something like
> this:
>
> public class DomainObjectSqueezeAdaptor
>     implements SqueezeAdaptor
>     {
>     public static final char PREFIX = '*';
>     private static final Pattern squeezeFormat = Pattern.compile("\\*
> ([A-Za-z0-9_\\.\\$]+)_([0-9]+)");
>
>     public String getPrefix()
>         { return String.valueOf(PREFIX); }
>
>     public Class getDataClass()
>         { return DomainObject.class; }
>
>     public String squeeze(DataSqueezer squeezer, Object data)
>         { return squeeze(data); }
>
>     public static String squeeze(Object data)
>         {
>         return PREFIX
>              + data.getClass().getName()
>              + '_'
>              + ((DomainObject) data).getId();
>         }
>
>     public DomainObject unsqueeze(DataSqueezer squeezer, String s)
>         { return unsqueeze(s); }
>
>     public static DomainObject unsqueeze(String s)
>         {
>         Matcher matcher = squeezeFormat.matcher(s);
>         if(!matcher.matches())
>             throw new IllegalArgumentException("Unable to parse
> squeezed ID: " + s);
>         String className = matcher.group(1);
>         String id = matcher.group(2);
>
>         Class type;
>         try { type = Class.forName(className); }
>         catch(ClassNotFoundException e)
>             { throw new IllegalArgumentException("Unable to parse
> squeezed ID " + s, e); }
>
>         return HibernateHelper.fromId((Class<? extends
> DomainObject>) type, id);
>         }
>     }
>
> Then add this to your hivemodule.xml:
>
>     <contribution configuration-id="tapestry.data.SqueezeAdaptors">
>       <adaptor
> object="instance:net.innig.core.web.tapestry.DomainObjectSqueezeAdaptor"
> />
>     </contribution>
>
> Cheers,
>
> Paul
>
>
> On Dec 9, 2005, at 11:45 AM, Glen wrote:
>
> > I am converting a pre-existing webapp to use Tapestry 4.0.  It uses
> > hibernate for the business objects and long's for the uid's on those
> > objects.  I would like to use tapestry to coerce from the uid as a
> > string into the actual business object
> >
> > Ultimately what I want to be able to do is to write a direct link like
> > this...
> >
> >
> > <span jwcid="@DirectLink" listener="listener:gotoDetails"
> > parameters="ognl:bizObject">view details</span>
> >
> > public void gotoDetails( BizObject bizObject ) {
> >    // rest of code goes here
> > }
> >
> > instead of how I am currently doing it as follows...
> >
> > <span jwcid="@DirectLink" listener="listener:gotoDetails"
> > parameters="ognl:bizobject.uid">view details</span>
> >
> > public void gotoDetails( Long  uid ) {
> >     BizObject bizObject = DAO.getBizObject( uid );
> > }
> >
> >
> > I know I need to use the tapestry.coerce.ValueConverter service.  Can
> > anyone point me in the right direction as to how I would implement
> > this?
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
>
> _________________________________________________________________
> Piano music podcast: http://inthehands.com
> Other interesting stuff: http://innig.net
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

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


Re: adding a custom Type converter

Posted by Paul Cantrell <ca...@pobox.com>.
I've done this, and it works quite well. You need to implement the  
interface org.apache.tapestry.util.io.SqueezeAdaptor, something like  
this:

public class DomainObjectSqueezeAdaptor
     implements SqueezeAdaptor
     {
     public static final char PREFIX = '*';
     private static final Pattern squeezeFormat = Pattern.compile("\\* 
([A-Za-z0-9_\\.\\$]+)_([0-9]+)");

     public String getPrefix()
         { return String.valueOf(PREFIX); }

     public Class getDataClass()
         { return DomainObject.class; }

     public String squeeze(DataSqueezer squeezer, Object data)
         { return squeeze(data); }

     public static String squeeze(Object data)
         {
         return PREFIX
              + data.getClass().getName()
              + '_'
              + ((DomainObject) data).getId();
         }

     public DomainObject unsqueeze(DataSqueezer squeezer, String s)
         { return unsqueeze(s); }

     public static DomainObject unsqueeze(String s)
         {
         Matcher matcher = squeezeFormat.matcher(s);
         if(!matcher.matches())
             throw new IllegalArgumentException("Unable to parse  
squeezed ID: " + s);
         String className = matcher.group(1);
         String id = matcher.group(2);

         Class type;
         try { type = Class.forName(className); }
         catch(ClassNotFoundException e)
             { throw new IllegalArgumentException("Unable to parse  
squeezed ID " + s, e); }

         return HibernateHelper.fromId((Class<? extends  
DomainObject>) type, id);
         }
     }

Then add this to your hivemodule.xml:

     <contribution configuration-id="tapestry.data.SqueezeAdaptors">
       <adaptor  
object="instance:net.innig.core.web.tapestry.DomainObjectSqueezeAdaptor" 
/>
     </contribution>

Cheers,

Paul


On Dec 9, 2005, at 11:45 AM, Glen wrote:

> I am converting a pre-existing webapp to use Tapestry 4.0.  It uses
> hibernate for the business objects and long's for the uid's on those
> objects.  I would like to use tapestry to coerce from the uid as a
> string into the actual business object
>
> Ultimately what I want to be able to do is to write a direct link like
> this...
>
>
> <span jwcid="@DirectLink" listener="listener:gotoDetails"
> parameters="ognl:bizObject">view details</span>
>
> public void gotoDetails( BizObject bizObject ) {
>    // rest of code goes here
> }
>
> instead of how I am currently doing it as follows...
>
> <span jwcid="@DirectLink" listener="listener:gotoDetails"
> parameters="ognl:bizobject.uid">view details</span>
>
> public void gotoDetails( Long  uid ) {
>     BizObject bizObject = DAO.getBizObject( uid );
> }
>
>
> I know I need to use the tapestry.coerce.ValueConverter service.  Can
> anyone point me in the right direction as to how I would implement  
> this?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>

_________________________________________________________________
Piano music podcast: http://inthehands.com
Other interesting stuff: http://innig.net



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