Sending Results Back to the Client

There are two types of results to return to a client:

  1. Status of the command

  2. Data that the client needs to display

The CommandStatus class is used to return the success/failure of the command to the client. The CommandStatus class only understands two types of status: SUCCESS and FAILURE. The original intent of this class was to indicate whether a command was routed successfully by the framework. However, this wasn’t made explicit and, as a result, many existing command handling methods use this SUCCESS/FAILURE to indicate the status of their specific processing.

It would be a good practice to always extend this class to enable returning more specific error codes than just SUCCESS/FAILURE.

So, let’s return to our example and fill in one of the command handling methods to return data and a SUCCESSFUL status to the client.

public boolean getExamples(CommandEvent theEvent,
          ServiceContext theContext,
          String theLocation) {
  //object used to transmit results back to the client
  XMLTransferObject xto=new XMLTransferObject();
  Example [] theResults=someMethod(theLocation);
  if (theResults == null) {
    //this is simplistic, but it shows what we need
    xto.setCommandStatus(CommandStatus.SIMPLE_FAILURE);
  }
  else {
    if (theResults.length != 0)
      xto.addAll(theResults);
    xto.setCommandStatus(CommandStatus.SIMPLE_SUCCESS);
  }
  this.storeService.set(theContext,
          DefaultScopeType.REQUEST_SCOPE,
          AppManServlet.RESULT,
          xto.exportXml());
  return true;
}

The XMLTransferObject is used to transmit the data and the command status back to the client; we use the defined CommandStatus.SIMPLE_FAILURE or CommandStatus.SIMPLE_SUCCESS objects to return the correct status. If results were available, they were then added to the XMLTransferObject using the addAll() method. The results were then placed in the command listener's store service using the REQUEST_SCOPE and using the key AppManServlet.RESULT. After this method returns to the framework, the framework will take any data stored using the combination DefaultScopeType.REQUEST_SCOPE and AppManServlet.RESULT and send that data back to the client as the results of the command.