A classe ActionValidator verificará
se pelo menos um dos parâmetros de cadeia tem um valor, mas não ambos.
Você também tornará o parâmetro de Valor um parâmetro necessário e codificará
a classe ActionValidator para marcar o parâmetro
com um asterisco.
- Na visualização Package Explorer, abra a classe ActionValidator
navegando por com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action e
dando um clique duplo na classe para abri-la no editor.
- Comece substituindo o método isParameterRequired.
Para essa amostra, faça a codificação somente para o parâmetro de Valor
necessário. O pseudocódigo a seguir demonstra isso:
if paramID = paramID of value parameter
return true
else
return false
Use o código de origem de exemplo a seguir para
substituir o método isParameterRequired:
/( 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;
}
- Em seguida, substitua o método validateAction
para permitir somente um valor no parâmetro de cadeias de entrada, mas não ambos.
O pseudocódigo a seguir demonstra isso:
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 o código de amostra de exemplo a seguir para substituir
esse método:
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;
}
- Salve a origem e depure qualquer erro.