Keyword "mainbody"

For making it easier to test or quickly use a generated service implementation class, you can now add code to the definition, that will go into the main() method of the implementation class.

Idea

Imagine that you want to have a main() method in your implementation class, that will contain some custom code like the following line:

    System.out.println("Hello world!");

Then just put the following in the amalegeni service definition as follows:

    mainbody
    {{
        System.out.println("Hello world!");
    }}

Run amalegeni, and have a look at the generated Service Implementation class. You'll find your code, wrapped by a try/catch, in the body of the main(..) method:

public static void main(String[] args)
{
    try
    {
        System.out.println("Hello world!");
    }
    catch (ApplicationException sqe)
    {
        sqe.printStackTrace();
    }
}

Can you spot your line of code? Perfect! Note for the people with an eye for detail: the above main() will not compile because of no Application Exception being thrown in the try{} body.

But now you have an impression of what the idea is.

Example

Note: the data used in this example is that of examples/masterdetail, of which we are only using the MasterBean.

bean MasterBean 
{
    String masterId;
    String artist;
    String album;
}

Service method definition:

List<MasterBean> getMasterList()
<<
    select id, artist, album
    from   t_master
>>

The code calling the 'getMasterList()' function looks like this:

    mainbody
    {{
        MainBodyExampleServiceImpl service=new MainBodyExampleServiceImpl();
        
        for (MasterBean mb : service.getMasterList())
        {
            System.out.println( mb.toString());
        }
    }}

Now generate the code with amalegeni, and have a look at the service implementation class:

public static void main(String[] args)
{
    try
    {
        MainBodyExampleServiceImpl service=new MainBodyExampleServiceImpl();
            
        for (MasterBean mb : service.getMasterList())
        {
            System.out.println( mb.toString());
        }

        
    }
    catch (ApplicationException sqe)
    {
        sqe.printStackTrace();
    }
}

If the other classes (eg. BaseService etc) are correctly setup, then execute your class and admire the output...

Example code

The example code, with all necessary other code bits, can be downloaded here:

It should be pretty easy to tweak a file (or two) and have this stand-alone example up and running in 5-min. See the instructions in the included readme.txt file.

For the sql scripts (create the table, insert data) use:

© Willem Moors, 2009 - 2013