Rational Developer for System z


第 8 課:建立 ActionValidator 類別的程式碼

ActionValidator 類別將驗證至少其中一個(而非兩個)字串參數具有值。 您還將讓 Value 參數變更為必要參數,並將編碼 ActionValidator 類別,以使用星號來標示參數。

  1. 套件瀏覽器視圖中,透過導覽 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action,然後按兩下 ActionValidator 類別以在編輯器中開啟,來開啟該類別。
  2. 您將從置換 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;
    }
  3. 接下來,您將置換 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;
    }
  4. 儲存原始檔,並對任何錯誤進行除錯。

使用條款 | 意見



本資訊中心採用 Eclipse 技術。(http://www.eclipse.org)