ActionValidator クラスは、ストリング・パラメーターの少なくとも 1 つが値を持つが、両方は持っていないことを確認します。
また、Value パラメーターを必須パラメーターにして、パラメーターにアスタリスクを付けるよう ActionValidator クラスをコーディングします。
- 「パッケージ・エクスプローラー」ビューで、com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action にナビゲートして、ActionValidator クラスをダブルクリックしてエディターで開きます。
- 最初に、isParameterRequired メソッドをオーバーライドします。
このサンプルでは、Value パラメーターのみを必須パラメーターにするようコーディングします。次の疑似コードはこの例です。
if paramID = paramID of value parameter
return true
else
return false
次のサンプル・ソース・コードを使用して、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;
}
- 次に、入力ストリング・パラメーターに 1 つの値のみを許可し、両方は許可しないよう validateAction メソッドをオーバーライドします。
次の疑似コードはこの例です。
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
次のサンプル・コードを使用して、このメソッドをオーバーライドします。
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;
}
- ソースを保存し、すべてのエラーをデバッグします。