Lesson 8: Create the code for the ActionValidator class

The ActionValidator class verifies that at least one of the string parameters has a value, but not both. You will also make the Value parameter a required parameter, and codes the ActionValidator class to mark the parameter with an asterisk.

Procedure

  1. In the Package Explorer view, open the ActionValidator class by navigating com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action and double-clicking on the class to open it in the editor.
  2. You start by overriding the isParameterRequired method. For this sample, you will code for only the Value parameter to be required. The following pseudo code demonstrates this:
    if paramID = paramID of value parameter
    	return true
    else
    	return false
    Use the following example source code to override the isParameterRequired method:
    /( Mark the value parameter as required.*/
    public boolean isParameterRequired(Action action, Parameter param)
    {
       //The value parameterID is 000
       if(param.getParameterID().equals("000"))
       {
          return true;
       }
    
       return false;
    }
  3. Next, you override the validateAction method to allow only one value in the input strings parameter, but not both. The following pseudocode demonstrates this:
    Collect all the Parameter objects associated with the action
    Iterate through the Parameter objects and get the string1 and string2 Parameter objects
    Retrieve the string value of string1, check to see if it's null or has a length of 0
    Retrieve the string value of string2, check to see if it's null or has a length of 0
    Determine if both the parameters have values
    	Return an error message
    Determine if neither of the parameters have values
    	Return an informational message 
    Use the following example sample code to override this method:
    public ValidationResult validateAction(ActionValidationEvent event) 
    {
       Parameter string1Param = null;
       Parameter string2Param = null;
       ValidationResult result = new ValidationResult();
    		
       //Iterate through and retrieve the parameter objects
       for (Object o : event.action.getParameters()) 
       {
          if (((Parameter) o).getParameterId().equals("001"))  
          {
             string1Param = (Parameter) o;
          } 
          else if (((Parameter) o).getParameterId().equals("002")) 
          {
             string2Param = (Parameter) o;
          }
       ) 
    		
       //Retrieve the parameter value for string1, and determine if there was input
       String string1Val = (String) event.parameterValueMap.get(string1Param);
       boolean string1IsThere = !(string1Val == null || string1Val.length() == 0);
    		
       //Retrieve the parameter value for string2, and determine if there was input
       String string2Val = (String) event.parameterValueMap.get(string2Param);
       boolean string2IsThere = !(string2Val == null || string2Val.length() == 0);
    		
       //Determine if both string1 and string2 are provided.
       //Returns an error message.
       if (string1IsThere && string2IsThere)
       {
          result.severity = ValidationResult.ERROR;
          result.message = "Provide either String1 or String2.";
       }
    		
       //Determine if neither string1 nor string2 is provided
       //Returns an info message.
       else if(!string1IsThere && !string2IsThere)
       {
          result.severity = ValidationResult.INFO;
          result.message = "Enter a value for String1 or String2.";
       }
    							
       return result;
    }
  4. Save the source and debug any errors.