Create a simple Web Service with IBM EGL

 

Requirements
IBM EGL Community Edition
http://www.ibm.com/developerworks/downloads/r/eglcommunity/index.html?S_TACT=105AGX28&S_CMP=DLMAIN
 
 
Creating a new Project

Start the IBM eclipse EGL community editon.
Select File, New EGL Project.
Enter a Project name (MyFirstProject) and select Finish.
In the Project Explorer a Project with the name MyFirstProject should be visible.

Creating a Webs Service in the Project
Open the MyFirstProject in the Project Explorer, a Folder EGLSource should be visible.
Click with the right mouse button on the EGLSource Folder and select Service.
Enter in EGL source file name: MyFirstWebService Click Finish and accept the package name.
// service
service MyFirstWebService
 
 // Variable Declarations
 variableName string;
 
 // Function Declarations
 function functionName()
 end
end
Adding a Function to the Web Service
Now we can add a simple Service Function.
the function will accept a input string (in) (something like "HELLO") and return the input string with "WORLD".

// service
service MyFirstWebService
 
  // Variable Declarations
  variableName string;
  
  function Reply(input string in) returns (string)
     result string;
     result = input+" WORLD";
     return(result);
  end

 
  // Function Declarations
  function functionName()
  end

end

 
Publishing the Web Service

In order to use the Web Service it must be published first.
Open the MyFirstProject in the Project Explorer
Open the EGLSource folder.
Open the (default package) folder.
Locate the MyFirstWebService and click on it with the right mouse button.
Select EGL Services and choose Create EGL Service Client Binding.
Keep the names default and click on Finish.
The Web Service is now published.

 
Adding a Rich UI to consume the WebService

Open the MyFirstProject in the Project Explorer.
Click with the right mouse button on the EGLSource Folder and select Rich UI Handler.
Enter in EGL source file name: MyFirstRichUI Click Finish.
Place a TextField and a Button on the design area with the default names.
Go to the Events Tab in the Properties box and select onClick.
Click on the + sign and add a new Button_onClick event handler.

// RUI Handler

import com.ibm.egl.rui.widgets.Box;
import com.ibm.egl.rui.widgets.TextField;
import com.ibm.egl.rui.widgets.Button;

//
//
handler MyFirstRichUI type RUIhandler {initialUI = [ ui ],onConstructionFunction = start, cssFile="css/MyFirstProject.css"}
 
 ui Box{ columns = 2, width = "400", height = "400", children = [ TextField, Button ] };
 TextField TextField{};
 Button Button{ text="Button", onClick ::= Button_onClick };
 
 function start()
 end
 
 function Button_onClick(event Event in)
  
 end 
end

Calling the Web Service from the  Rich UI
To call the Web Service from the Rich UI we will add a Callback function and a Exception handler.
If an Exception occurs i will just display it in the added TextField.
 
The Exception Handler

Function myExceptionHandler(exp AnyException in)
   case
   when (exp isa ServiceBindingException)
   TextField.Text = exp.message;
    ;
   when (exp isa ServiceInvocationException)
   TextField.Text = exp.message;
   ;
   otherwise
   TextField.Text = exp.message;
   ;
   end
end

 
The Callback Function
Function myCallBack(result string IN)
   TextField.Text = result;
end
The Callback Function will return the result of the WebService to the TextField.
 
Calling the functions from the Button_onClick Event
myService MyFirstWebService{};
call myService.Reply(TextField.text)
returning to myCallback onException myExceptionHandler;
 
 
Running the app
Now you can go to the preview tab and enter HELLO in the TextField and click on the Button.
The Web Service should now return HELLO WORLD in the TextField.
 

// RUI Handler

import com.ibm.egl.rui.widgets.Box;
import com.ibm.egl.rui.widgets.TextField;
import com.ibm.egl.rui.widgets.Button;

//
//
handler MyFirstRichUI type RUIhandler {initialUI = [ ui ],onConstructionFunction = start, cssFile="css/MyFirstProject.css"}
 
 ui Box{ columns = 2, width = "400", height = "400", children = [ TextField, Button ] };
 TextField TextField{};
 Button Button{ text="Button", onClick ::= Button_onClick };
 
 function start()
 end
 
 Function myExceptionHandler(exp AnyException in)
   case
    when (exp isa ServiceBindingException)
     TextField.Text = exp.message;
    ;
    when (exp isa ServiceInvocationException)
     TextField.Text = exp.message;
    ;
    otherwise
     TextField.Text = exp.message;
    ;
   end
 end
 
 Function myCallBack(result string IN)
  TextField.Text = result;
 end
 
 function Button_onClick(event Event in)
  myService MyFirstWebService{};
  call myService.Reply(TextField.text)
  returning to myCallback onException myExceptionHandler;
 end
 
end

 
www.programmingsystems.eu