|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
AJAXWorld News Desk AJAX Reporting Challenges and Solutions with Adobe Flex
Bringing together JavaScript, Flash Player and Web Services
By: Yakov Fain
Oct. 10, 2007 01:30 PM
We’ll start with identifying some major challenges of creating reports in AJAX applications. Browsers’ incompatibilities Performance Robustness Solution: Use a virtual machine with reliable code delivery and good development and debugging tools. Web browsers have unpredictable future I’ve listed some major challenges that any RIA developers would face regardless of what protocol is being used for data delivery. Recently, our company, Farata Systems that was working mainly with Flex/Java applications, decided to create a version of our reporter ClearBI as a component that can be added to AJAX applications as well. While experimenting with various protocols, we’ve created a component called WebService.swf, which is a Flash Player and can easily turn a SOAP WebService into an object with methods that correspond to WSDL operations, and the XML processing is done using ECMAScript for XML (E4X) that allows you to forget about XML parsing headaches . Below is a diagram that shows you an HTML/JavaScript application that uses an invisible Flash Player component (WebService.swf) and one visible (ClearBI.swf). In this example we’ll use the data coming from a publicly available book search Web Service from Amazon .com. The entire process works as follows: 1. The JavaScript code gives a URL of Amazon’s WSDL to a hidden WebService.swf 2. The WebServices.swf loads WSDL from Amazon, and automatically converts XML into an object with properties (it uses E4X that will be explained below). 3. The JavaScript asks WebService.swf to call the selected operation from Amazon’s WSDL. 5.To produce reports that look as shown below and can be customized by the end users, pass the data from JavaScript to ClearBI swf. What’s under the hoodThe entire workflow consists of two major steps described below: 1. Using JavaScript initialize the WebService.swf object, register event listeners, and load the WSDL: var ws = com.farata.jsfx.WebServices("MyWebService"); Add the WSDL load listener ws.addEventListener("serviceload", onWsdlLoaded); Add the error listener ws.addEventListener("servicefault", onError); Initialize the WebService.swf with wsdl. Load wsdl and notify ServiceLoadListeners on success. ws.useService( "http://soap.amazon.com/schemas2/AmazonWebServices.wsdl", "Amazon" ); This call will load Amazon’s WSDL, which among others will contain the operation KeywordSearchRequest: As you can see, this operation expects an argument of type KeywordRequest, which is described in the same WSDL: <xsd:complexType name="KeywordRequest"> Now we need to call the operation KeywordSearchRequest providing the data according to the KewordRequest format. 2. Call the Web service Add the web service operation result listener. Later in this article I’ll show you how to pass the operation’s arguments as a JavaScript object instead of XML. Now the data from Amazon come back and WebService.swf will receive something like this : <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:amazon="http://soap.amazon.com" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body>-<namesp1099:KeywordSearchRequestResponse xmlns:namesp1099="http://soap.amazon.com"> <return xsi:type="amazon:ProductInfo"> <ImageUrlLarge xsi:type="xsd:string">http://ec1.images-amazon.com/images/I/51OY5KP5ydL.jpg</ImageUrlLarge> As you can see, there is 355 AJAX books that can be returned as 36 pages, and Amazon has returned the page number one as was requested in the argument object. Parsing of this XML will be done for you automatically by the WebService.swf component that will turn it into an object for easy access via dot notation. A mini-tutorial on E4X, a standardized processing of XML Working with XML(SOAP) based Web Services in AJAX is not easy: each browser handles XML differently. But consuming Web Services with AJAX can be fun if you delegate XML processing to our WebService.swf component that engages E4X. In the next diagram you’ll see how ActionScript 3 (we used it for creation of WebService.swf) can work with any XML source (i.e. SOAP) using familiar dot notation. This is an example from ActionScript documentation, and it will give you a feeling of the ease of dealing with XML in ActionScript (for more detailed explanation of E4X visit Adobe documentation). The XML file on the left is processed by the code on the right. The first three ActionScript trace statements demonstrate how you can access the elements and attributes of this XML stored in the variable myXML. The second line has a filtering criteria in parentheses: @id==2. This line means “find me all XML elements nameed item, and print the content of the element menuName for those that have the attribute id equals to 2. Pretty loaded and elegant line, isn’t it? After that, the code snippet above creates a newItem element and appends it to myXML, and then again uses dot notation to access XML elements and attributes. From SOAP to JavaScript and to FlashPlayer with ClearBI ClearBI is a commercial Web reported that was originally created for Flex/Java applications and it was working only with the server side applications that had Adobe LiveCycle Data Services, which implemented a fast communication protocol called AMF. But the newest version of ClearBI 1.1 can work with the data that come in a form of JavaScript objects, a Web service or use OpenAMF, an open source implementation of AMF0 communication protocol. In this section I’ll go over the process of getting the data from Amazon’s SOAP Web Service to JavaScript and then to ClearBI. This is what has to be done after the Web Service is initialized by WebService.swf. This time we’ll use JavaScript object as an argument to the operation KeywordSearchRequest. Pass a JavaScript object with retrieval args to a hidden swf object, which will return the data back to JavaScript, which in turn passes the data to ClearBI swf: Add web service operation result listener. The next line maps the WebService.swf method serviceresult to JavaScript’s function onJavaScriptResult that will receive the data from WebService.swf when available and will format them as JSON objects. ws.addEventListener("serviceresult", onJavaScriptResult); var args = { }; Call Amazon’s "KeywordSearchRequest" web service operation. ws.Amazon.KeywordSearchRequest( args ); … The JavaScript function that receives the data arriving as JavaScript into a variable lastData. function onJavaScriptResult(ev) { Now we can pass the data from lastData to clearBI.swf for displaying and customizing the report. Pass the data to ClearBI from a JavaScript array In JavaScript, get a reference to Flash Player object and call the function loadReport giving the report name (a template customized by the user and stored in a database) and the data itself: var report report.loadReport(reportName, lastData); The data exchange between JavaScript and ClearBI.swf is supported internally by the ActionScript class ExternalInterface. The report variable above represents ClearBI.swf, which internally (in ActionScript) allowed JavaScript calling its function loadReport that’s mapped to ActionScript’s function loadReport ExternalInterface.addCallback(“loadReport", loadReport); That’s all there is to it. The end users can run and customize reports themselves. For example, their Web browser window may look as shown below. The top portion of the screen is regular JavaScript while the report and its editor are rendered by Flash Player 9. Using faster communication protocol: OpenAMF If you are building your application in house and it does not need to connect to someone else’s Web service, you may be better off creating a Java based data feed using faster communication protocol called OpenAMF. This open source implementation of the AMF communication protocol that performs serialization of the server-side POJO into its ActionScript peer on the client. This is how it goes: Client: a Web browser with Flash Player 9 plugin or ActiveX Mid-Tier: A servlet container with open source OpenAMF (5 jars), open source Flex SDK, ClearBI’s jars to support report compilation and hot deployment, JOTM, JTS, JDBC drivers Report persistence: ClearBI uses two database tables to store reports’ metadata (can be co-located with other data). SummaryA proper integration of JavaScript and Flash Player’s components can enrich your AJAX application with powerful reporting capabilities offered by ClearBI (written in Adobe Flex). If you are not into reporting, just use the WebService.swf to communication with SOAP Web Services. You can download the description of the API for JavaScript wrapper to WebService.swf at http://www.myflex.org/demo/webservice_swf/doc/ . The WebService.swf is located at http://www.myflex.org/demo/webservice_swf/WebService.swf . The trial version of ClearBI 1.1 will become available on October 15, 2007 at http://www.myflex.org This demo application that uses JavaScript, WebService.swf and ClearBI.swf is deployed at http://www.myflex.org/theriabook/ClearBI.Ajax/main.html. It looks like this: In the first three tabs you’ll be able to play with sending XML and JavaScript requests to Amazon, and the last three tabs are supported by ClearBI (the name of thos report is Amazon.KeywordSearchRequest. You might want to watch a short pre-recorded demo that shows working with a sample Employees database and using ClearBI editor for report customization. This demo is located at http://myflex.org/screencast/clearbi_ajax/clearbi_ajax.html. This demo shows how end-users can add grouping, modify styling, apply formulas and more. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||