Wednesday, July 9, 2014

Oracle ADF WebSerice - XMLGregorianCalendar to Util DateTime

In our Oracle ADF applicaiton, if we are consuming a web service by creating JAX-WS proxy client and exposing its methods as data control using our facade java class, then the xsd:dateTime attributes in web service input will be shown as collection attribute in data control palette and will be created as XMLGregorianCalendar object in generated java class getter's and setter's, as shown below




In order use the attribute as original date time, we can convert that to java util date, using the steps mentioned below:

1) Create a jax-ws-jaxb-customization xml file having mapping of xsd types to java types:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema"
                xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
                xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                wsdlLocation="http://localhost:9001/soa-infra/services/default/SyncService/syncprocess_client_ep?WSDL">
  <jaxb:globalBindings>
    <jaxb:serializable/>
    <jaxb:javaType name="java.util.Date" xmlType="xsd:dateTime"/>
    <jaxb:javaType name="java.util.Date" xmlType="xsd:date"/>
  </jaxb:globalBindings>
</jaxws:bindings>

2) Add the above customization file in the web service property wizard, as shown in below screen shot:
Open the properties window
Click on Add File and select the customization file created above.
3) Click "Ok" to close the wizards.

4) The web service proxy files will get regenerated. Also the XMLGregorianCalendar objects in the request or response java files, will get converted into java.util.date


4) Under the web service files, new adapter files will get created based on the number of mappings added in the customization file. The files can be modified to format the date time as per required format, as shown in the code below:



Code::
public class Adapter1 extends XmlAdapter<String, Date>
{
    private String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    public Date unmarshal(String value) throws ParseException {
        return new SimpleDateFormat(pattern).parse(value);
    }

    public String marshal(Date value) {
        String dateAsString = "";
        if (value == null) {
            return null;
        }
        try{
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            dateAsString = format.format(value);
        }catch(Exception e){
        //Handle
    }
        return dateAsString;
    }
}


5) Recreate the data control based on facade class. The attributes will now be shown as simple attributes:
Note: Each time the proxy service is regenerated, the Adapter files will get regenerated as well. So we need to keep the backup of adapter file code (if the default code was modified) before regenerating the proxy and update it again after regeneration.