Wrapbeans and implementation methods

This example introduces the wrapbean: this is a bean that doesn't come directly out of the database, but that is created in a self-defined Java-method.

Suppose you need the change the result that comes from the database, an operation that for some reason has to happen in java code.

In the following trivial example the extra operation is turning the name into uppercase. Have a look at the getEnrichedNameList() method defined below.

The NameBean is a regular bean, that is returned by getNameList() from the database. The method getEnrichedNameList() uses getNameList() as input and creates a list of EnrichedNameBeans.

Bean definition


bean NameBean
{
    String id;
    String name;
}

wrapbean EnrichedNameBean
{
    String id;
    String name;
    String upperCaseName;
}

Service definition


service WrimpService
{
    implementation List<NameBean> getNameList()
    <<
        select id, name
        from   t_name
    >>

    List<EnrichedNameBean> getEnrichedNameList()
    {{
        List<EnrichedNameBean> list=new ArrayList<EnrichedNameBean>(); 
        for ( NameBean b : getNameList() )
        {
            EnrichedNameBean n = new EnrichedNameBean();     
            n.setId( b.getId() );
            n.setName( b.getName() );
            n.setUpperCaseName( b.getName().toUpperCase() );
            list.add(n);
        } 
        return list;
    }}
}

Full bean and service definition : Wrimp.mlg

Note that the method getNameList() is delared to be 'implementation' only. This means that in the service implementation this method will be declared private, and that it won't even show up in the service interface definition.

The database table

t_name

 id | name  
----+-------
 n1 | Noah
 n2 | Ethan
 n3 | Jack
 n4 | Liam
 n5 | Logan

The Main java class

WrimpService service=new WrimpServiceImpl();
    
for (EnrichedNameBean n : service.getEnrichedNameList() )
{
    System.out.println( n.getName() + " -> " + n.getUpperCaseName() );
}

Output

Noah -> NOAH
Ethan -> ETHAN
Jack -> JACK
Liam -> LIAM
Logan -> LOGAN

All Files

SQL file(s):

Java file(s):

Mlg file(s):

© Willem Moors, 2009 - 2013