Class CommandSet

java.lang.Object
org.pipservices3.commons.commands.CommandSet

public class CommandSet extends Object
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 Details

    • CommandSet

      public CommandSet()
      Creates an empty CommandSet object.
  • Method Details

    • getCommands

      public List<ICommand> getCommands()
      Gets all commands registered in this command set.
      Returns:
      a list of commands.
      See Also:
    • getEvents

      public List<IEvent> getEvents()
      Gets all events registered in this command set.
      Returns:
      a list of events.
    • findCommand

      public ICommand findCommand(String commandName)
      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

      public IEvent findEvent(String eventName)
      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

      public void addCommand(ICommand command)
      Adds a ICommand command to this command set.
      Parameters:
      command - the command to add.
      See Also:
    • addCommands

      public void addCommands(List<ICommand> commands)
      Adds multiple ICommand commands to this command set.
      Parameters:
      commands - the array of commands to add.
      See Also:
    • addEvent

      public void addEvent(IEvent event)
      Adds an IEvent event to this command set.
      Parameters:
      event - the event to add.
      See Also:
    • addEvents

      public void addEvents(List<IEvent> events)
      Adds multiple IEvent events to this command set.
      Parameters:
      events - the array of events to add.
      See Also:
    • addCommandSet

      public void addCommandSet(CommandSet commandSet)
      Adds all of the commands and events from specified CommandSet command set into this one.
      Parameters:
      commandSet - the CommandSet to add.
    • addListener

      public void addListener(IEventListener listener)
      Adds a IEventListener listener to receive notifications on fired events.
      Parameters:
      listener - the listener to add.
      See Also:
    • removeListener

      public void removeListener(IEventListener listener)
      Removes previosly added IEventListener listener.
      Parameters:
      listener - the listener to remove.
      See Also:
    • addInterceptor

      public void addInterceptor(ICommandInterceptor interceptor)
      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

      public List<ValidationResult> validate(String commandName, Parameters args)
      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.