Dealing with unstructured data in ADF BC using JSON type attribute
Sometimes you may need to deal with unstructured data in your business applications. Wondering what I'm talking about? Let me be more detailed. Consider a scenario where the structure of the data (and even the number of values and their types) that you may store in a column of a table changes for different records based on various business conditions. You cannot always have child table to deal with such cases and also simply keeping them comma separated string may not be really enough to process the data. You may want to store some extra metadata along with each item for processing the data later. One solution for such case is to sore the data as XML or JSON type in database table. This post discusses a possible solution for such use case using JSON.
There are few things you may want to do if you ware planing to use JSON in an ADF application.
1. Deciding how you want to persist JSON type in DB
2. Deciding how you want the user to edit the data in the UI
3. Binding the UI with the JSON type.
I'm sharing an example in this post which may help you to get a feel of the things that I'm talking about. This example is very basic and deals with simple flat JSON string, however you can enhance it the way you want.
Download
You can download the sample workspace from here.
[ Runs with Oracle JDeveloper 12.1.2.0.0 + a custom HOT_NEWS table ]
Prerequisite
Before try running the sample you may want to run the script for creating the HOT_NEWS table and HOT_NEWS_SEQ db sequence that is used in this example. Also add dependency to jackson-all-1.9.11.jar in the model project. These things are packaged along with the sample app that you downloaded.
Model project
This example uses CLOB type for storing JSON type data in database. To keep things more simpler on business component layer, I've created a custom domain called JsonDomain by extending oracle.jbo.domain.ClobDomain. This custom domain class has some utility methods for dealing JSON data, otherwise it just makes use of offerings from ClobDomain base class for reading and writing data to database. Under the cover JsonDomain uses Jackson JSON processor APIs for parsing JSON string. The Model project has dependency added to jackson-all-1.9.11.jar. In short, there is no complex things on server, it pretty simple and straightforward.
View Controller project
On the UI side, this example uses dynamic components(af:dynamicComponent) for presenting the JSON in more readable manner. See DynamicComponentBean class to get a feel of dynamic UI APIs used in the sample. You may also see few extra classes such DynamicAttributesModel and DynamicAttributeDescriptor which are used for building dynamic UI. To learn more about these classes, read 21 Determining Components at Runtime in Developing Web User Interfaces with Oracle ADF.
The af:inputText bound to to JsonDomain type has converter set as follows converter="oracle.genericDomain" . This converter comes with framework. To know more about this converter, read http://www.jobinesh.com/2011/04/converter-for-clobdomain.html
Once all the basic set ups are done, run the jsonCRUD.jsf.
You may get a page as shown above. Try creating a new row. You may want to try entering simple JSON string in the Details field. Now click on 'Show Details(JSON) in Dynamic UI' button to see the dynamic UI created from the JSON string you entered. The dynamic UI in the popup and the JSON value that you see in the parent page are in synch (in both ways). As I mentioned earlier, this sample deals with simple flat JSON string, you can enhance it the way you want.
There are few things you may want to do if you ware planing to use JSON in an ADF application.
1. Deciding how you want to persist JSON type in DB
2. Deciding how you want the user to edit the data in the UI
3. Binding the UI with the JSON type.
I'm sharing an example in this post which may help you to get a feel of the things that I'm talking about. This example is very basic and deals with simple flat JSON string, however you can enhance it the way you want.
Download
You can download the sample workspace from here.
[ Runs with Oracle JDeveloper 12.1.2.0.0 + a custom HOT_NEWS table ]
Prerequisite
Before try running the sample you may want to run the script for creating the HOT_NEWS table and HOT_NEWS_SEQ db sequence that is used in this example. Also add dependency to jackson-all-1.9.11.jar in the model project. These things are packaged along with the sample app that you downloaded.
- Run the <JSONTypeSample>/script/script.sql
- In the model project, add dependency to <JSONTypeSample>/jackson/jackson-all-1.9.11.jar. You can also download this file from http://wiki.fasterxml.com/JacksonDownload (if you want to use latest release)
Model project
This example uses CLOB type for storing JSON type data in database. To keep things more simpler on business component layer, I've created a custom domain called JsonDomain by extending oracle.jbo.domain.ClobDomain. This custom domain class has some utility methods for dealing JSON data, otherwise it just makes use of offerings from ClobDomain base class for reading and writing data to database. Under the cover JsonDomain uses Jackson JSON processor APIs for parsing JSON string. The Model project has dependency added to jackson-all-1.9.11.jar. In short, there is no complex things on server, it pretty simple and straightforward.
View Controller project
On the UI side, this example uses dynamic components(af:dynamicComponent) for presenting the JSON in more readable manner. See DynamicComponentBean class to get a feel of dynamic UI APIs used in the sample. You may also see few extra classes such DynamicAttributesModel and DynamicAttributeDescriptor which are used for building dynamic UI. To learn more about these classes, read 21 Determining Components at Runtime in Developing Web User Interfaces with Oracle ADF.
The af:inputText bound to to JsonDomain type has converter set as follows converter="oracle.genericDomain" . This converter comes with framework. To know more about this converter, read http://www.jobinesh.com/2011/04/converter-for-clobdomain.html
Once all the basic set ups are done, run the jsonCRUD.jsf.
You may get a page as shown above. Try creating a new row. You may want to try entering simple JSON string in the Details field. Now click on 'Show Details(JSON) in Dynamic UI' button to see the dynamic UI created from the JSON string you entered. The dynamic UI in the popup and the JSON value that you see in the parent page are in synch (in both ways). As I mentioned earlier, this sample deals with simple flat JSON string, you can enhance it the way you want.
Good post.
ReplyDeleteThanks Jobinesh...
Its a really useful topics.Thanks.
ReplyDelete