Dynamic tasks

Defining a recipient

Two recipient natures may be defined:

  • Single recipient: any page extension may receive the sent message;
  • Recipient with associated class: only the associated class receives the sent message.

Recipient class

Here is an example of class representing a universal recipient This one derives from a base class named BaseRecipient. It must implement the method onMessage.

public class AnyRecipient extends BaseRecipient
{
        interface SupportedEventTypes
        {
                int ADD = 0;
                int REMOVE = 1;
                int CLEAR = 2;
        }

        public boolean onMessage( IMessage message )
        {
                switch ( message.getEventType() )
                {
                        case SupportedEventTypes.ADD :
                                // TODO : do something while adding
                                break;
                        case SupportedEventTypes.REMOVE :
                                // TODO : do something while removing
                                break;
                        case SupportedEventTypes.CLEAR :
                                // TODO : do something while clearing
                                break;
                        default :
                                // TODO : do something for default case
                                break;
                }
                return true;
        }
}

 

In this example we show how to define both recipient natures.

public void dynamic_declareRecipient( ISiteModule siteModule, IContext context ) throws SiteModuleException
{
        // define a single recipient
        siteModule.getMessageController().registerRecipient( "simple-recipient" );

        // define a recipient associated to a class
        AnyRecipient anyRecipient = (AnyRecipient)siteModule.getMessageController().registerRecipient( "any-recipient", AnyRecipient.class );
}
    

Deleting a named recipient

You may delete a recipient at any time.

public void dynamic_deleteRecipient( ISiteModule siteModule, IContext context ) throws SiteModuleException
{
        // cancel the recipient definition
        siteModule.getMessageController().unregisterRecipient( "simple-recipient" );
}
   

Sending a message to a named recipient

The messages may be sent from a page extension, a provider or any other system such as the dynamic extensions of the workflow module.

The following example shows how to send a message from a "listener" associated to a button.

public void dynamic_sendRecipientMessage( ISiteModule siteModule, IContext context ) throws SiteModuleException
{
        CtlButton addButton = new CtlButton( "add", new CtlText( "Add to basket" ) );
        addButton.addActionListener( new ActionListener()
        {
                public void onClick( ActionEvent event )
                {
                        try
                        {
                                // sending a message to a recipient from a provider
                                getSiteModule().getMessageController().sendMessage( "simple-recipient", 2, null );
                        }
                        catch( SiteModuleException e )
                        {
                                Navigator.getNavigator().processErrors( e, true );
                        }
                };
        } );
}
 

Receiving a message sent to a recipient

To receive a message sent to a recipient, you just have to implement the method onMessage(). This one is compulsory in the case of the class branching off BaseRecipient, but may be implemented in the case of an extension page.

The following example shows how to extract from the message the event type and its content (body).

public boolean onMessage( IMessage message )
{
        switch ( message.getEventType() )
        {
                case BasketHandler.IEventTypes.ADD :
                        this.add( message.getBody() );
                        break;
                case BasketHandler.IEventTypes.REMOVE :
                        this.remove( message.getBody() );
                        break;
                case BasketHandler.IEventTypes.CLEAR :
                        this.clear();
                        break;
                case BasketHandler.IEventTypes.VALIDATE :
                        this.validate();
                        break;
        }
        return true;
}
 

Position information for the site promotion from a page extension

The following example shows how to position information assigned to the site promotion from the page extension class.

public boolean onBeforeLoad()
{
        SEO seo = this.getExecutionContext().getPageContainer().getSEO();
        if ( seo != null )
        {
                // modifying the page title
                seo.setTitle( "Kit de développement VDoc" );

                // positioning some "meta"
                seo.addMeta( newMeta( HTMLConstants.Meta.DESCRIPTION, "Ensemble de composants logiciel" ) );
                seo.addMeta( newMeta( HTMLConstants.Meta.KEYWORDS, "api, framework, navigation" ) );
        }
        return super.onBeforeLoad();
}

Position information for the site promotion from a plugin

The following example shows how to position information assigned to the site promotion from the controller plugin.

protected String preparePage( IPluginRequest pluginRequest, IRenderModel renderModel ) throws Exception
{
        // creation of a SEO structure for the site promotion
        SEO seo = newSEO();

        // positioning the page title
        seo.setTitle( (String)renderModel.getValue( FIELD_NAME ) );

        // positioning a certain number of "meta"
        seo.addMeta( newMeta( HTMLConstants.Meta.DESCRIPTION, "Exemple d'un contrôleur de plugin" ) );
        seo.addMeta( newMeta( HTMLConstants.Meta.KEYWORDS, "file, plugin" ) );

        // storage of the SEO structure in the template
        renderModel.setValue( IRenderModel.VirtualPageMarkers.SEO, seo );

        // position of the render mask.
        return ( "content" );
}