Listing 1
class MathAction extends Object {
var intArgumentOne:Number;
var intArgumentTwo:Number;
var myMathService:Object;
var operateType:String;
// passing webservice object to this class;
public function setConnection(MathService:Object) {
myMathService=MathService;
}
// set first argument to operate with
public function setArgumentOne(strArgumentOne:String) {
intArgumentOne=new Number(strArgumentOne);
}
// set second argument to operate with
public function setArgumentTwo(strArgumentTwo:String) {
intArgumentTwo=new Number(strArgumentTwo);
}
// method that wrap the ApplicationInfo method at mth web service
public function getAppInfo(putObject:Object) {
var objResult:Object;
operateType = "AppInfo";
// call ApplicationInfo method from web service
objResult=myMathService.ApplicationInfo("Sonny Hastomo");
// the onResult event call when the service invoke success
objResult.onResult=function(result) {
var strResult=new String(result);
trace("result from services invoked result = " + strResult);
putObject.text=strResult;
}
// the onFault event call when the service invoke success
objResult.onFault=function(result) {
trace("Invoke Error : " + result.faultCode + " Definition : " + result.faultstring);
}
}
public function Operate(strOperation:String,putObject:Object) {
var objResult:Object;
operateType = "Math";
if (strOperation == "Multiply") {
// call Multiply method from web service
objResult=myMathService.Multiply(intArgumentOne,intArgumentTwo);
}
if (strOperation == "Add") {
// call Add method from web service
objResult=myMathService.Add(intArgumentOne,intArgumentTwo);
}
if (strOperation == "Subtract") {
// call substract method from web service
objResult=myMathService.Substract(intArgumentOne,intArgumentTwo);
}
// the onResult event call when the service invoke success
objResult.onResult=function(result) {
var strResult=new String(result);
trace("result from services invoked result = " + strResult);
putObject.text=strResult;
}
// the onFault event call when the service invoke success
objResult.onFault=function(result) {
trace("Invoke Error : " + result.faultCode + " Definition : " + result.faultstring);
}
}
public function getOperationType():String {
//return last operation type;
return operateType;
}
}
Listing 2
// import mx.service package to get WebService Object
import mx.services.*;
// declare the variabel
var MathService:WebService;
var strWSDL:String;
var strResult:String;
var strOPType:String;
var MathObject:MathAction;
var objResult:Object;
// put the wsdl link on variabel strWSDL
strWSDL="http://localhost:9080/MATHServicesWeb/wsdl/com/math/service/MathOperationService.wsdl";
// load web service object
MathService=new WebService(strWSDL);
MathService.onLoad=trace("loading math web service");
// use MathObject class (web service wraper) to invoke the web service method
MathObject = new MathAction();
MathObject.setConnection(MathService);
// call getAppInfo which call the webservice ApplicationInfo.
MathObject.getAppInfo(lblAppNotified);
Listing 3
on(click) {
var strArgumentOne:String;
var strArgumentTwo:String;
strArgumentOne=_root.txtArgument1.text;
strArgumentTwo=_root.txtArgument2.text;
_root.MathObject.setArgumentOne(strArgumentOne);
_root.MathObject.setArgumentTwo(strArgumentTwo);
// invoke Operate global object MathObject from the root variabel declaration
_root.MathObject.Operate("Multiply",_root.txtResult);
}
Listing 4
// code for button instance btnAdd
on(click) {
var strArgumentOne:String;
var strArgumentTwo:String;
strArgumentOne=_root.txtArgument1.text;
strArgumentTwo=_root.txtArgument2.text;
_root.MathObject.setArgumentOne(strArgumentOne);
_root.MathObject.setArgumentTwo(strArgumentTwo);
_root.MathObject.Operate("Add",_root.txtResult);
}
// code for button instance btnSubtract
on(click) {
var strArgumentOne:String;
var strArgumentTwo:String;
strArgumentOne=_root.txtArgument1.text;
strArgumentTwo=_root.txtArgument2.text;
_root.MathObject.setArgumentOne(strArgumentOne);
_root.MathObject.setArgumentTwo(strArgumentTwo);
_root.MathObject.Operate("Subtract",_root.txtResult);
}