You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by bhorvat <ho...@gmail.com> on 2012/11/12 20:11:07 UTC

Type Coercer for Joda Time

I would like to add Joda Time for the Type Coerection but I am not sure how. 

I have the following eventlink that I would like to implement

<t:eventlink t:event="showWeekHour" t:context="currentWeek" zone="zoneHours"
>
           Click For Week View                 
</t:eventlink>

And the method that handles this would be

    @OnEvent("showWeekHour")
    public void selectWeek(LocalDate week) {
        selectedWeek = week;
        initialize();
        ajaxResponseRenderer.addRender("zoneHours", zoneHours);
    }

The type coercer 

@Contribute(TypeCoercer.class)
    public static void contributeJodaDates(Configuration<CoercionTuple>
configuration) {
        configuration.add(new CoercionTuple<java.util.Date,
DateMidnight>(java.util.Date.class, DateMidnight.class,
                new Coercion<java.util.Date, DateMidnight>() {
                    @Override
                    public DateMidnight coerce(java.util.Date input) {
                        return new DateMidnight(input);
                    }
                }));

        configuration.add(new CoercionTuple<DateMidnight,
java.util.Date>(DateMidnight.class, java.util.Date.class,
                new Coercion<DateMidnight, java.util.Date>() {
                    @Override
                    public java.util.Date coerce(DateMidnight input) {
                        return input.toDate();
                    }
                }));

        configuration.add(new CoercionTuple<java.util.Date,
LocalDate>(java.util.Date.class, LocalDate.class,
                new Coercion<java.util.Date, LocalDate>() {
                    @Override
                    public LocalDate coerce(java.util.Date input) {
                        return new LocalDate(input);
                    }
                }));

        configuration.add(new CoercionTuple<LocalDate,
java.util.Date>(LocalDate.class, java.util.Date.class,
                new Coercion<LocalDate, java.util.Date>() {
                    @Override
                    public java.util.Date coerce(LocalDate input) {
                        return input.toDate();
                    }
                }));
    }

However this doesnt really work. I am sure it is a trivial mistake but I
have no idea how to solve it. 

I guess that I could pass the long into the event and then implement
coercion from long to the joda time, but I am not sure if this is the right
way and if it is possible as well.

cheers 



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
If I add 


        configuration.add(new CoercionTuple<Long, LocalDate>(Long.class,
LocalDate.class,
                new Coercion<Long, LocalDate>() {
                    @Override
                    public LocalDate coerce(Long input) {
                        return new LocalDate(input);
                    }
                }));

        configuration.add(new CoercionTuple<LocalDate,
Long>(LocalDate.class, Long.class,
                new Coercion<LocalDate, Long>() {
                    @Override
                    public Long coerce(LocalDate input) {
                        return input.toDate().getTime();
                    }
                }));

Then it will work only if in the event I put 

<t:eventlink t:event="showWeekHour" t:context="currentWeek.toDate().time"
zone="zoneHours" >
Click
</t:event>

So it wont coerce Joda to some long number that is time like 15422410000 but
it will print it as 2012-10-10



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717951.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by Howard Lewis Ship <hl...@gmail.com>.
One of the things you might want to look at in your tests is:

http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/TypeCoercer.html#explain(java.lang.Class,
java.lang.Class)


Given an input type and an output type, this returns a string that shows
how the TypeCoercer will do the transform. It is often very useful in unit
tests of code that provides coercions, especially if you are contributing a
bunch of coercions and aren't sure how they will interact.


On Mon, Nov 12, 2012 at 3:20 PM, bhorvat <ho...@gmail.com> wrote:

> Cool. I learned a bit today I guess
>
> tnx for help
>
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717960.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
Cool. I learned a bit today I guess 

tnx for help



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717960.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 12 Nov 2012 21:11:38 -0200, bhorvat <ho...@gmail.com>  
wrote:

> So my last question is if I dont proviede Coerection from LocalDate ->
> String then the toString method is called on the localDate, right?

Yeah. I guess this is the Object -> String coercion, which is probably  
added as a fallback when no specific SomeType -> String coercion is  
provided.

-- 
Thiago H. de Paula Figueiredo

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


Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
With a bit of testing I figured most of it out 


I use the coerction below to convert from Long to Local when the event is
fire

        configuration.add(new CoercionTuple<Long, LocalDate>(Long.class,
LocalDate.class,
                new Coercion<Long, LocalDate>() {
                    @Override
                    public LocalDate coerce(Long input) {
                        return new LocalDate(input);
                    }
                }));

and this to be able to display LocalDate (Joda time) 

        configuration.add(new CoercionTuple<LocalDate,
String>(LocalDate.class, String.class,
                new Coercion<LocalDate, String>() {
                    @Override
                    public String coerce(LocalDate input) {
                        return String.valueOf(input.toDate().getTime());
                    }
                }));

using the t:context="lodaDate" in the eventlink.

So my last question is if I dont proviede Coerection from LocalDate ->
String then the toString method is called on the localDate, right? Which
would explain why without the given coercion on I only get 2012-10-10
instead of long representation of the date.




--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717958.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
So I need to set the coercer for String as well. 

btw does tapestry (I think this is where my confusion is coming from) can do
multiple coercer's at once. For example String -> Long -> Date -> JodaDate



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717957.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 12 Nov 2012 19:47:35 -0200, bhorvat <ho...@gmail.com>  
wrote:

> It doesnt work as  I get the exception
>
>  Could not find a coercion from type java.lang.String to type
> org.joda.time.LocalDate.

So this coercion is missing. Add it and you should be fine. Or create a  
ValueEncoder for LocalDate.

-- 
Thiago H. de Paula Figueiredo

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


Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
It doesnt work as  I get the exception

 Could not find a coercion from type java.lang.String to type
org.joda.time.LocalDate.

The problem is that what is passed into the context is the date in the
format 2012-10-10 and then when that tries to convert back to the Joda Time
it will break.

Tapestry has java.util.Date --> java.util.Calendar so I guess it knows how
to pars date and I have implemented Date to Joda, so it should work by some
logic :S





--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717950.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Mon, 12 Nov 2012 18:28:29 -0200, bhorvat <ho...@gmail.com>  
wrote:

> But still my code above doesnt work, and according to the jumbstart code  
> it should

Please define 'doesn't work'. In addition, I think you also should (maybe  
need) to create and contribute a ValueEncoder for the JodaTime classes you  
want to use.

-- 
Thiago H. de Paula Figueiredo

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


Re: Type Coercer for Joda Time

Posted by bhorvat <ho...@gmail.com>.
Sadly no, I did use that as the template of my code. I think that what is
different is that they use the date component while I use the event. But I
am not sure.

But still my code above doesnt work, and according to the jumbstart code it
should



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717944.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Type Coercer for Joda Time

Posted by Geoff Callender <ge...@gmail.com>.
Does this help?

	http://jumpstart.doublenegative.com.au/jumpstart/examples/lang/typecoercers

Geoff

On 13/11/2012, at 6:11 AM, bhorvat wrote:

> I would like to add Joda Time for the Type Coerection but I am not sure how. 
> 
> I have the following eventlink that I would like to implement
> 
> <t:eventlink t:event="showWeekHour" t:context="currentWeek" zone="zoneHours"
>> 
>           Click For Week View                 
> </t:eventlink>
> 
> And the method that handles this would be
> 
>    @OnEvent("showWeekHour")
>    public void selectWeek(LocalDate week) {
>        selectedWeek = week;
>        initialize();
>        ajaxResponseRenderer.addRender("zoneHours", zoneHours);
>    }
> 
> The type coercer 
> 
> @Contribute(TypeCoercer.class)
>    public static void contributeJodaDates(Configuration<CoercionTuple>
> configuration) {
>        configuration.add(new CoercionTuple<java.util.Date,
> DateMidnight>(java.util.Date.class, DateMidnight.class,
>                new Coercion<java.util.Date, DateMidnight>() {
>                    @Override
>                    public DateMidnight coerce(java.util.Date input) {
>                        return new DateMidnight(input);
>                    }
>                }));
> 
>        configuration.add(new CoercionTuple<DateMidnight,
> java.util.Date>(DateMidnight.class, java.util.Date.class,
>                new Coercion<DateMidnight, java.util.Date>() {
>                    @Override
>                    public java.util.Date coerce(DateMidnight input) {
>                        return input.toDate();
>                    }
>                }));
> 
>        configuration.add(new CoercionTuple<java.util.Date,
> LocalDate>(java.util.Date.class, LocalDate.class,
>                new Coercion<java.util.Date, LocalDate>() {
>                    @Override
>                    public LocalDate coerce(java.util.Date input) {
>                        return new LocalDate(input);
>                    }
>                }));
> 
>        configuration.add(new CoercionTuple<LocalDate,
> java.util.Date>(LocalDate.class, java.util.Date.class,
>                new Coercion<LocalDate, java.util.Date>() {
>                    @Override
>                    public java.util.Date coerce(LocalDate input) {
>                        return input.toDate();
>                    }
>                }));
>    }
> 
> However this doesnt really work. I am sure it is a trivial mistake but I
> have no idea how to solve it. 
> 
> I guess that I could pass the long into the event and then implement
> coercion from long to the joda time, but I am not sure if this is the right
> way and if it is possible as well.
> 
> cheers 
> 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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


Re: Type Coercer for Joda Time

Posted by Lance Java <la...@googlemail.com>.
If the documentation [1] is complete then tapestry does not include coercers
for java.uttil.Date by default. You might need to coerce to long instead.

[1] http://tapestry.apache.org/typecoercer-service.html



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Type-Coercer-for-Joda-Time-tp5717942p5717945.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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