Recipient

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 can 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;
}