Examples

This section includes the following sample code:

Example.java

// this is a simple class used as a parameter to show how the
// framework can separate out command arguments that are object
// types embedded in XML. For more information on how the
// framework uses XML to transport "generic" objects between the
// mid-tier and the client, please see the Java Docs references
// for the XMLTransferObject class.
public Example extends Object {
  private String name = "";
  private String[] text = new String[0];
  // no-argument constructor. Must be public for XML Transfer
  // to work.
  public Example() {
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String value) {
    name = value;
  }
  
  public String[] getSampleText() {
    String[] result = new String[text.length];
    for (int i = 0; i < result.length; ++i)
      result[i] = text[i];
    return result;
  }
  
  public void setSampleText(String[] values) {
    if (values != null) {
      text = new String[values.length];
      for (int i = 0; i < values.length; ++i)
        text[i] = values[i];
    }
    else {
        text = new String[0];
    }
  }
}

ExampleCommandString.java

public ExampleCommandString extends CommandString {
    // declare some static String objects in a way that we know these							
    // objects do not need to be translated to different locales.
    public static final String GET_EXAMPLES_TEXT = "GetExamples";
    public static final String ADD_EXAMPLE_TEXT = "AddExample";
    public static final String DELETE_EXAMPLE_TEXT = "DeleteExample";							
    
    // now we declare the actual commands
    public static final ExampleCommandString GET_EXAMPLES =
        new ExampleCommandString(GET_EXAMPLES_TEXT);	
    public static final ExampleCommandString ADD_EXAMPLE =
        new ExampleCommandString(ADD_EXAMPLE_TEXT);	
    public static final ExampleCommandString DELETE_EXAMPLE =
        new ExampleCommandString(DELETE_EXAMPLE_TEXT);	
        
    // for organizational purposes, we also declare the parameters for each
    // of these commands in this file.
    public static final String PARAM_LOCATION = "location";
    public static final String PARAM_EXAMPLE = "example";
    public static final String PARAM_NAME = "examplename";
    
    // declare a CommandArgument object for each of these parameters
    private static final CommandArgument ARGUMENT_LOCATION =
        new CommandArgument(PARAM_LOCATION,
        true,
        String.class,
        null);
    private static final CommandArgument ARGUMENT_EXAMPLE =
        new CommandArgument(PARAM_EXAMPLE,
        true,
        Example.class,
        null);
    private static final CommandArgument ARGUMENT_NAME =
        new CommandArgument(PARAM_NAME,
        true,
        String.class,
        null);
    
    // declare an array of arguments for each command.
    public static final CommandArgument[] GET_EXAMPLES_ARGS =
        new CommandArgument[] { ARGUMENT_LOCATION };	
    public static final CommandArgument[] ADD_EXAMPLE_ARGS =
        new CommandArgument[] { ARGUMENT_LOCATION,	
            ARGUMENT_EXAMPLE };
    public static final CommandArgument[] DELETE_EXAMPLE_ARGS =
        New CommandArgument[] { ARGUMENT_LOCATION,	
            ARGUMENT_NAME };
}

This class declares command strings and describes the arguments for three commands that will be supported by the ExampleCommandListener class. If the toString() method of each ExampleCommandString object declared in this source code file were called, the results would be:

ExampleCommandString.GetExamples
ExampleCommandString.AddExample
ExampleCommandString.DeleteExample 

Every CommandDescriptor object contains a reference to an object derived from CommandString; it is through this mechanism that the framework guarantees every command name is unique.

ExampleDescriptor.java

public class ExampleDescriptor extends CommandDescriptor {
    private static final String GET_EXAMPLES_METHOD = "getExamples";
    private static final String ADD_EXAMPLE_METHOD = "addExample";
    private static final String DELETE_EXAMPLE_METHOD = "deleteExample";
    
    public static final CommandDescriptor GET_EXAMPLES =
        new CommandDescriptor(ExampleCommands.GET_EXAMPLES,			
            GET_EXAMPLES_METHOD,
            ExampleCommands.GET_EXAMPLES_ARGS);
    public static final CommandDescriptor ADD_EXAMPLE =
        new CommandDescriptor(ExampleCommands.ADD_EXAMPLE,
            ADD_EXAMPLE_METHOD,
            ExampleCommands.ADD_EXAMPLE_ARGS);
    public static final CommandDescriptor DELETE_EXAMPLE =
        new CommandDescriptor(ExampleCommands.DELETE_EXAMPLE,
            DELETE_EXAMPLE_METHOD,
            ExampleCommands.DELETE_EXAMPLE_ARGS);
}

ExampleCommandListener.java

public class ExampleCommandListener extends AppManCommandListener {
    // the method called when the GetExamples command is received.
    public boolean getExamples(CommandEvent theEvent,
            ServiceContext theContext,
            String theLocation) {
        // the details will be filled in later
        return true;
    }
    
    // the method called when the AddExample command is received.
    Public Boolean addExample(CommandEvent theEvent,
            ServiceContext theContext,
            String theLocation,
            Example theExample) {
        // the details will be filled in later
        return true;
    }
    
    // the method called when the DeleteExample command is
    // received.
    public boolean deleteExample(CommandEvent theEvent,
            ServiceContext theContext,
            String theLocation,
            String theName) {
        // the details will be filled in later.
        return true;
    }
    
    // the framework calls this method to get the descriptors for
    // the commands supported by this command listener.
    public CommandDescriptor[] getCommands() {
        return new CommandDescriptor[] {
            ExampleDescriptor.GET_EXAMPLES,
            ExampleDescriptor.ADD_EXAMPLE,
            ExampleDescriptor.DELETE_EXAMPLE };
    }
}

The preceding example shows the skeleton of a command listener:

  1. Extend the correct class

  2. Add the command handling methods

  3. Override the getCommands() method to return the descriptors for those commands.

The difficulty is in the details of the command handling methods, which is covered in the next section.