Velocity service

This service let you use the great template mechanism Velocity as injectable service in your Tapestry application.

Configuration

optionaly insert your individual Velocity configuration This may be null, so the service create its own minimalistic configuration.

public class AppModule
{
    public static void contributeVelocityService(MappedConfiguration<String, Resource> configuration)
    {
        Resource velocityConfig = new ClasspathResource("/velocity.properties");
        configuration.add("velocity.configuration", velocityConfig);
    }
}

First Sample

here a easy sample to generate a email body with the VelocityService

the template file

if you think i am crazy if you read the sample text, read this

Dear ${user_name},

you've tried ${login_tries} times to log into our system with wrong password!
for this reason your home planet would removed for an intergalactical highway
in 1000 years from today (${block_date}).

If you feel that be a fault, please send your antilogy to the ministry at Vogsphere,
or contact your <a href="mailto:${sysadm_email}">system administrator</a> via the babelfish.

dont panic and thanks for all the fish,
Zaphod Beeblebrox

the page class

public class LostPassword
{
    @Inject @Velocity
    private TemplateService _templateService;

    public void afterThirdTryToLogin()
    {
        OutputStream emailBodyStream = new EmailBodyStream();
        Map parameterMap = new HashMap();
        parameterMap.put("user_name", "Athur Dent");
        parameterMap.put("login_tries", getLoginTries());
        parameterMap.put("block_date", new Date());
        parameterMap.put("sysadm_email", "Zaphod.Beeblebrox@beteigeuze.behind.the.moon");
        _templateService.mergeDataWithResource(new URIResource("./templates/email_body.ftl"),
                                               emailBodyStream, parameterMap)

        Email email = createEmail(emailBodyStream);
        email.send();
    }
}

Second Sample

here a easy sample to generate a report with the VelocityService

the template file

This is the passenger list on flight to
${target}.

=====================================================
#foreach(passenger in elementList)
${passenger.lastname}, ${passenger.firstname}
#end
=====================================================

they are mostly harmless!

the page class

public class PassengerList
{
    @Inject @VelocityMarker
    private TemplateService _templateService;

    public StreamResponse getPassengerList()
    {
        List<Passenger> passengerList = getDAO(Passenger.class).findAllForFlight0815();
        OutputStream os = new OutputStream();
        Map parameterMap = new HashMap();
        parameterMap.put("target", "the restaurant at the end of the universe");
        _templateService.mergeDataWithResource(new URIResource("./templates/report.ftl"),os,
                                               parameterMap, passengerList)

        return TextStreamResponse("text/plain", os.toString())
    }
}