Sunday, November 16, 2014

Using xml clob data as data source

Suppose there is table that contains data, such as submitted user application, in xml format and their is a requirement to extract some info from that xml and display it on a web page. For example, the table below contains user application data as xml in AppData column. When a row is selected, the basic information of the user gets extracted from the xml and displayed on the page.


Here are the steps to achieve this:
1. Create JAXB content model from the xsd of xml data content. As a result, Java classes will be generated for each element type of  xsd


2. Create a Java bean class, which will be used later to create a data control. In this class add a property whose data type is the xsd root element type and a method which will populate data for generated Jaxb classes.

import generated.ApplicationType;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class UserAppDataDC
{
  // XSD top element type
  private ApplicationType userAppData;

  public UserAppDataDC()
  {
    super();
  }

  //Method to populate app data in generated Jaxb classes
  public void getUserAppData(String appData)
  {
    try
    {
      JAXBContext jc = JAXBContext.newInstance(ApplicationType.class);
      StreamSource xml = new StreamSource(new StringReader(appData));
      Unmarshaller unmarshaller = jc.createUnmarshaller();
      JAXBElement<ApplicationType> jaxE1 =
        unmarshaller.unmarshal(xml, ApplicationType.class);
      setUserAppData(jaxE1.getValue());
    }
    catch (PropertyException pe)
    {
      pe.printStackTrace();
    }
    catch (JAXBException jaxbe)
    {
      jaxbe.printStackTrace();
    }
  }

  public void setUserAppData(ApplicationType userAppData)
  {
    this.userAppData = userAppData;
  }
  public ApplicationType getUserAppData()
  {
    return userAppData;
  }
}

3. Right click on the Java bean class and select 'Create Data Control'. Now you can see a data control corresponding to this class in Data Controls panel
4. Start building UI by adding a table component on the page. For getting table data we can simply use a View Object. We do not necessarily need to display the app data colum but the tree attribute binding for it should be available on the page. So, for this example we are using two data controls, a bean data control, which contains data collections corresponding to Jaxb classes and another which contains a VO on the table.



5. Add a method binding for bean data control method.
6. Add a panel form layout with the required attributes from bean data control.
7. Create a custom selection listener for the table in a managed bean. Bind both table and form layout components to managed bean attributes.

public void tableRowSelectionListener(SelectionEvent selectionEvent)
{
// Execute default selection listener behavior
   resolveMethodExpression("#{bindings.UserAppVO.collectionModel.makeCurrent}",
                                         null, new Class[]
            { SelectionEvent.class }, new Object[]
            { selectionEvent });

// Find the method binnding for bean data control method
   OperationBinding op = (OperationBinding)findOperation("getUserAppData");
// Set method parameter with the application data value of the selected row 
 op.getParamsMap().put("appData", getSelectedRowAttribute(getAppDataTable(), "AppData"));
   op.execute();
// Refresh bean data control iterator
    DCIteratorBinding iter = findIterator("UserAppDataDCIterator");
          iter.executeQuery();
          iter.refresh(DCIteratorBinding.RANGESIZE_UNLIMITED);
// Refresh form layout, which displays data extracted from xml
   AdfFacesContext.getCurrentInstance().addPartialTarget(getFormLayout());
    
}

When a row is selected on the table, the selection listener gets the app data from table attribute binding and pass it to the method, which populates data in Jaxb classes. Iterator binding is refreshed subsequently to get the updated data from bean data control.

No comments:

Post a Comment