Tuesday, January 11, 2011

Integrating Flex with Oracle EPG (using XML)

Flex is a dynamic, open source framework that is being used extensively by developers to build, maintain and deploy web applications on various platforms including browsers, desktops and operating systems.
One major aspect of Flex is that it can be integrated with a number of languages including ColdFusion, Java, .Net and PHP to develop simple and complex web applications.  While using other applications at the backend, it becomes more convenient with Flex to provide complex functionalities of web applications and database interactions. Also, Flex based web applications are dynamic and their interfaces are highly interactive.
A number of complex Rich Internet Applications are being developed using Oracle at the backend to serve as a database and Flex at the front-end. Most developers are using Oracle EPG (Oracle 10g) at the backend because it lets them have the data, security, business logic and application files in one centralized place, which reduces the complexities of maintenance. There are many other advantages of using Oracle with Flex to develop web applications. These are:
  • Oracle EPG provides XML web services between the client and the server.
  • The HTTP server can act as an embedded PL/SQL gateway to run PL/SQL applications via mod_plsql i.e. PL/SQL stored procedures can be invoked by the client using the embedded HTTP server. Mod_plsql is the Apache Extension module.
  • In Oracle, the stored procedures that are coded in PL/SQL are executed using URL referencing.
  • The administration of DADs (Database Access Descriptors) for the XML DB HTTP server is performed using the DBMS_EPG package, so there is no need to define the database instance.
  • There is a file repository inside the database and can be accessed using HTTP, WebDAV or FRP protocols. The file repository hosts the XML contents directly. The repository has many file controls as well, including ACL (Access Control List), check-in/check-out, version control, programmatic access to files through Java or PL/SQL commands and file events.
  • The advantage of having a file system within the database eases many complexities, such as accessing the file system lying in the operating system. Also, the deployment procedure is simplified because the need of the HTTP server is distinguished by having all of the files as part of the database.
It becomes easy with Oracle EPG to have the functionality of an efficient XML carrier along with having the database at the server side.  To call the procedure for Flex via the URL and get the results in HTTP, you are required to look for the port that listens to XMLDB. You can even set the port according to your requirement. The following chunk of code will be required to establish the connection with the database for Flex integration.
Begin
-- Set the HTTP port of the XML DB HTTP listener to the standard, 80.
   dbms_xdb.sethttpport(80);
-- Set the FTP port of the XML DB FTP server to the standard, 21.
   dbms_xdb.setftpport(21);
-- This creates the DAD "DAD" and assigns it to the path "dd".
   dbms_epg.create_dad('DAD','/dd/*');
-- Assign the DAD to a database user.
   dbms_epg.set_dad_attribute('DAD','database-username','TEST_USER');
-- This unlocks the "anonymous" internal account so public access to the repository and static authentication is allowed.
   execute immediate 'alter user anonymous account unlock';
end;
You will also be required to authorize the DAD to connect using a specific database user. Despite the fact that we provide a username - “DAD” – the default physically connects to anonymous.
Declare
   l_xml_config sys.xmltype;
   anonymous_already_set exception;
   pragma exception_init(anonymous_already_set,-30936);
   dbms_epg.authorize_dad('DAD','TEST_USER');
   begin
      select insertchildxml(xdburitype('/xdbconfig.xml').getxml(),'/xdbconfig/sysconfig/protocolconfig/httpconfig','allow-repository-anonymous-access',     xmltype('<allow-repository-anonymous-access xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd">true</allow-repository-anonymous-access>'),    'xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"') into l_xml_config from dual;     
dbms_xdb.cfg_update(l_xml_config);
 commit;
   exception
      when anonymous_already_set then
         null;
   end;
As far as data handling in Flex is concerned, Flex does not communicate with the database directly. However, it can consume data from a web service like XML and display it. Flex uses the functions of HTTPService Class for data handling and storing in XML format, as well as communicating with the database web service. You can take advantage of the functions of HTTPService Class for handling the data after using XML services of Oracle.
HTTPService Class
When using HTTPService, the client makes an HTTPService request to the server. The server serializes the data into a XML string and returns that XML string to the client. The HTTPService then de-serializes the data from XML to objects. The client updates the data grid with new objects. Many functions of HTTPService Class are important for correct handling of data either from a URL or from the database. These are:
Send() -- To retrieve data from a URL and store it in the memory, the send() method of HTTPService object should be used. The send() method makes an HTTP request to the specified URL and an HTTP response is returned.
resultFormat-- HTTPService also adds the capability of handling XML data in different formats including “E4X” by setting the resultFormat attribute. To work with the loaded data as XML, it is required to set the resultFormat of the HTTPService object to “E4X”. This way the data retrieved from the URL will be stored as XML string and results could be accessed easily in XML format using the ECMAScript.
resultHandler() – The function is an event handler that is called when the call returns successfully.
To see a practical demonstration of Flex and Oracle EPG integration, I would recommend reading Mauricio Pacheco’s article, Flex Developer Center where he has very keenly described the connectivity of Flex with Oracle database using XML services.

References:
Mauricio Pacheco, “Using Oracle EPG to provide XML services for Flex applications,” retrieved Jan 23, 2009 from http://www.adobe.com/devnet/flex/articles/flex_epg.html
Tim, “DBMS_EPG - The Embedded PL/SQL Gateway in Oracle 10g Database Release 2,” retrieved Jan 23, 2009 from http://www.oracle-base.com/articles/10g/dbms_epg_10gR2.php
“Loading External Data with HTTPService,” retrieved Jan 23, 2009 from http://www.adobe.com/devnet/flex/quickstart/httpservice/

No comments:

Post a Comment