Description
The CommandSet class allows you to create a set of commands and events supported by a commandable object. In addition, it supports command interceptors and command call chains.
Important points
- CommandSets can be used as an alternative commandable interface to a business object.
- This class can be used to auto generate multiple external services for a business object.
Constructors
Creates an empty CommandSet object.
public
CommandSet()
Instance methods
addCommand
Adds a command to this command set.
See ICommand
public
void addCommand(ICommand command)
- command: ICommand - command to add.
addCommandSet
Adds all of the commands and events from a specified command set into this one.
public
void addCommandSet(CommandSet commandSet)
- commandSet: CommandSet - CommandSet to add.
addCommands
Adds multiple commands to this command set.
See ICommand
public
void addCommands(List<ICommand> commands)
- commands: List<ICommand> - list of commands to add.
addEvent
Adds an event to this command set.
See IEvent
public
void addEvent(IEvent event)
- event: IEvent - event to add.
addEvents
Adds multiple events to this command set.
See IEvent
public
void addEvents(List<IEvent> events)
- event: List<IEvent> - array of events to add.
addInterceptor
Adds a command interceptor to this command set.
public
void addInterceptor(ICommandInterceptor interceptor)
- interceptor: ICommandInterceptor interceptor to add.
addListener
Adds a listener to receive notifications on fired events.
See IEventListener
public
void addListener(IEventListener listener)
- listener: IEventListener - listener to add.
execute
Executes a command specificed by its name.
See ICommand, Parameters
public
Object execute(IContext context, String commandName, Parameters args) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- commandName: String - name of the command that is to be executed.
- args: Parameters - parameters (arguments) to pass to the command for execution.
- returns: Object - execution result
findCommand
Searches for a command by its name.
See ICommand
public
ICommand findCommand(String commandName)
- commandName: String - name of the command to search for.
- returns: ICommand - command, whose name matches the provided name.
findEvent
Searches for an event by its name in this command set.
public
IEvent findEvent(String eventName)
- eventName: string - name of the event to search for.
- returns: IEvent - event, whose name matches the provided name.
getCommands
Gets all commands registered in this command set.
See ICommand
public
List<ICommand> getCommands()
- returns: List<ICommand> - list of commands.
getEvents
Gets all events registred in this command set.
See IEvent
public
List<IEvent> getEvents()
- returns: List<IEvent> - list of events.
notify
Fires an event specified by its name and notifies all registered listeners
public
void notify(IContext context, String eventName, Parameters args) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- eventName: string - name of the event that is to be fired.
- args: Parameters - event arguments (parameters).
removeListener
Removes a previosly added listener.
See IEventListener
public
void removeListener(IEventListener listener)
- listener: IEventListener - listener to remove.
validate
Validates the args for a command specified by its name using a defined schema. If the validation schema is not defined, then the methods returns no errors. It returns a validation error if the command is not found.
public
List<ValidationResult> validate(String commandName, Parameters args)
- commandName: String - name of the command for which the ‘args’ must be validated.
- args: Parameters - parameters (arguments) to validate.
- returns: List<ValidationResult> - array of ValidationResults. If no command is found by the given name, then the returned array of ValidationResults will contain a single entry, whose type will be ValidationResultType.Error.
Examples
{
public class MyDataCommandSet extends CommandSet {
private IMyDataService _service;
public MyDataCommandSet(IMyDataService service) { // Any data service interface
super();
this._service = service;
this.addCommand(this.makeGetMyDataCommand());
}
private ICommand makeGetMyDataCommand() {
return new Command(
'get_mydata',
null,
(context, args) -> {
String param = args.getAsString('param');
return this._service.getMyData(context, param);
}
);
}
}
}