Monday, 7 April 2014

Creates a BC4J client in JDeveloper


To create a test client program, create a new Java class using the Create Java Class wizard. This is available in the New Gallery under the General category. Enter a class name like TestClient, a package name like devguide.examples.client, and ensure the Extends field says java.lang.Object. In the Optional Attributes, deselect the Generate Default Constructor and select the Generate Main Method checkbox. Then click OK to create the TestClient.java file. The file opens in the source editor to show you the skeleton code:

Example 5-6 Skeleton Code for TestClient.java
package devguide.examples.client;
public class TestClient {
  public static void main(String[] args) {
       
  }
}

Place the cursor on a blank line inside the body of the main() method and use the bc4jclient code template to create the few lines of necessary code. To use this predefined code template, type the characters bc4jclient followed by a [Ctrl]+[Enter] to expands the code template so that the class now should look like this:

Example 5-7 Expanded Skeleton Code for TestClient.java
package devguide.examples.client;
import oracle.jbo.client.Configuration;
import oracle.jbo.*;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.*;
public class TestClient {
  public static void main(String[] args) {
    String        amDef = "test.TestModule";
    String        config = "TestModuleLocal";
    ApplicationModule am =
      Configuration.createRootApplicationModule(amDef,config);
    ViewObject vo = am.findViewObject("TestView");
    // Work with your appmodule and view object here
    Configuration.releaseRootApplicationModule(am,true);
  }
}

Adjust the values of the amDef andconfig variables to reflect the names of the application module definition and the configuration that you want to use, respectively. For the Example 5-7, you would change these two lines to read:
String amDef = "devguide.examples.UserService";
String config = "UserServiceLocal";

Finally, change view object instance name in the call to findViewObject() to be the one you want to work with. Specify the name exactly as it appears in the Data Model tree on the Data Model panel of the Application Module editor. Here, the view object instance is named UserList, so you need to change the line to:
ViewObject vo = am.findViewObject("UserList");

At this point, you have a working skeleton test client for the UserService application module whose source code looks like what you see in Example 5-8.

Wednesday, 2 April 2014

Read an HTTP streaming using only JavaScript


In one of the project where I've worked on, we had the requirement of  showing some live data graphs, where the input data were coming through a Streaming http.

The customer wanted to see the data real time with max 1 second delay, and doing some calculations we've came out that using a server process for this would  have not match the customer expectations, then we decided to go for a Client side approach using JavaScript.

This is why I discovered this easy and nice solution which worked very well and which met all the customer requirements. Here is the result:


The graphs were getting updated in real time thanks to:

  • On ready state change event, which was getting the streaming update and enqueue the values:

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

  • The Chart Library which has been used to show the Graph:

http://www.flotcharts.org/

Here is the snippet used to get the Streaming update:

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==3 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","http://<Some HOST and PORT>/<STREAMING URL>",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>