Class CommandSet
java.lang.Object
org.pipservices3.commons.commands.CommandSet
Contains a set of commands and events supported by a
ICommandable
commandable object.
The CommandSet supports command interceptors to extend and the command call chain.
CommandSets can be used as alternative commandable interface to a business object. It can be used to auto generate multiple external services for the business object without writing much code.
### Example ###
public class MyDataCommandSet extends CommandSet {
private IMyDataController _controller;
public MyDataCommandSet(IMyDataController controller) { // Any data controller interface
super();
this._controller = controller;
this.addCommand(this.makeGetMyDataCommand());
}
private ICommand makeGetMyDataCommand() {
return new Command(
'get_mydata',
null,
(correlationId, args) -> {
String param = args.getAsString('param');
return this._controller.getMyData(correlationId, param);
}
);
}
}
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommand
(ICommand command) Adds a ICommand command to this command set.void
addCommands
(List<ICommand> commands) Adds multiple ICommand commands to this command set.void
addCommandSet
(CommandSet commandSet) Adds all of the commands and events from specified CommandSet command set into this one.void
Adds an IEvent event to this command set.void
Adds multiple IEvent events to this command set.void
addInterceptor
(ICommandInterceptor interceptor) Adds a ICommandInterceptor command interceptor to this command set.void
addListener
(IEventListener listener) Adds a IEventListener listener to receive notifications on fired events.execute
(String correlationId, String commandName, Parameters args) Executes a ICommand command specified by its name.findCommand
(String commandName) Searches for a command by its name.Searches for an event by its name in this command set.Gets all commands registered in this command set.Gets all events registered in this command set.void
notify
(String correlationId, String eventName, Parameters args) Fires event specified by its name and notifies all registered IEventListener listenersvoid
removeListener
(IEventListener listener) Removes previosly added IEventListener listener.validate
(String commandName, Parameters args) Validates Parameters args for command specified by its name using defined schema.
-
Constructor Details
-
CommandSet
public CommandSet()Creates an empty CommandSet object.
-
-
Method Details
-
getCommands
Gets all commands registered in this command set.- Returns:
- a list of commands.
- See Also:
-
getEvents
Gets all events registered in this command set.- Returns:
- a list of events.
-
findCommand
Searches for a command by its name.- Parameters:
commandName
- the name of the command to search for.- Returns:
- the command, whose name matches the provided name.
- See Also:
-
findEvent
Searches for an event by its name in this command set.- Parameters:
eventName
- the name of the event to search for.- Returns:
- the event, whose name matches the provided name.
- See Also:
-
addCommand
Adds a ICommand command to this command set.- Parameters:
command
- the command to add.- See Also:
-
addCommands
Adds multiple ICommand commands to this command set.- Parameters:
commands
- the array of commands to add.- See Also:
-
addEvent
Adds an IEvent event to this command set.- Parameters:
event
- the event to add.- See Also:
-
addEvents
Adds multiple IEvent events to this command set.- Parameters:
events
- the array of events to add.- See Also:
-
addCommandSet
Adds all of the commands and events from specified CommandSet command set into this one.- Parameters:
commandSet
- the CommandSet to add.
-
addListener
Adds a IEventListener listener to receive notifications on fired events.- Parameters:
listener
- the listener to add.- See Also:
-
removeListener
Removes previosly added IEventListener listener.- Parameters:
listener
- the listener to remove.- See Also:
-
addInterceptor
Adds a ICommandInterceptor command interceptor to this command set.- Parameters:
interceptor
- the interceptor to add.- See Also:
-
execute
public Object execute(String correlationId, String commandName, Parameters args) throws ApplicationException Executes a ICommand command specified by its name.- Parameters:
correlationId
- optional transaction id to trace calls across components.commandName
- the name of that command that is to be executed.args
- the parameters (arguments) to pass to the command for execution.- Returns:
- the execution result.
- Throws:
ApplicationException
- when execution fails for any reason.- See Also:
-
validate
Validates Parameters args for command specified by its name using defined schema. If validation schema is not defined than the methods returns no errors. It returns validation error if the command is not found.- Parameters:
commandName
- the name of the command for which the 'args' must be validated.args
- the parameters (arguments) to validate.- Returns:
- an 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.
- See Also:
-
notify
public void notify(String correlationId, String eventName, Parameters args) throws ApplicationException Fires event specified by its name and notifies all registered IEventListener listeners- Parameters:
correlationId
- optional transaction id to trace calls across components.eventName
- the name of the event that is to be fired.args
- the event arguments (parameters).- Throws:
ApplicationException
- when execution fails for any reason.
-