Wednesday, November 16, 2011

Create new Record after Search Criteria (Using Search Form) returns no Results. ADF BC 11g

Hi,

In this post, we are going to immediately create a row when we have no search results.
This is usually the case in oracle forms. Forms have a different out of the box approach.
When a Form opens, it open in a mixed mode. This means that no data are fetched. The user is able to insert some data and press enter, if there are no data in the DB with a given primary key, then, Forms are entering insert mode. This means that they create a new row. If the data are there, the record is Queried.

We are going to do the same with the following example.

Download Sample Application.

We will use, as usual, the HR schema of Oracle XE and more specifically we are going to use the Regions table. We choose the Regions table because it is the simplest table regarding dependencies and fits the scope of this example.

First, we generate the Business Components from tables with the help of JDeveloper's wizard.
Next, we are going to use the Search Form  feature ADF BC provides.
We create a page called index.
So, we drag and drop the Regions iterator onto our page and



Ok. After dropping the Search Form, we can simply run and test our page. We will see that the default mode of the page is the fetch data mode. In other words, it queries and shows the first row.
If we press the find button, the page will enter into the find mode, where all fields are search criteria.
We can insert some data and press the execute button. Then the execute operation will be invoked and search for records that have those data we entered earlier.

Well, this out of the box functionality is not exactly what we want... So lets try to change it a bit.
First, let's try to open the page in find mode.
To do that, we are going to  create an invoke action, as mentioned in this post:

http://dstas.blogspot.com/2010/09/adf-11g-methodaction-and-invokeaction.html

Our invoke action is going to invoke the Find Operation.
Additionally we are going to change the refresh property. Our Refresh property is going to be: prepareModel
For further reading please check the following links:

http://groundside.com/resources/DuncanMills/S298736_bindings.pdf 
http://download.oracle.com/docs/html/B25947_01/bcdcpal005.htm#sthref837

Additionally, we are going to set a Refresh Condition. This is to avoid executing the invokeAction in every postBack.



If we now run and test our page, we will see that our page is entering in find mode! We are ready to set criteria and press execute!
Up to now, we are half way through. We have to enter in insert mode.
When? right after knowing that the execute operation returned no results.

So, at this point we can understand when we should invoke an insert operation. When our iterator has no rows.. No rows can be produced by many cases, one of them is when we enter non existing criteria and search.. Another reason might be the fact that there are not any records in the DB... What ever the case, we are fully covered.
Since we are opening the page in find mode.. we have to search..
Additionally, it would be great if we could enter in insert mode even if we issue a rollback and the DB has no rows..

Last but no least, is that we want to create a new row if the search has no results, but, we want to use those search criteria values in the new row.

For that, we are going to alter a bit the Execute operation. We are going to create our own actionListener method.
But First, we have to apply our logic. Our logic says that if we do not have any records fetched by the execute operation, we should immediately create a new Row, with initial values, those values we did the execute on the first place.
One thing at the time:

Getting the values from find mode:
We will override the create(AttributeList attributeList) method of the Regions View Row java class:



Then, we will create a method in our application module, that will check if there are any rows fetched (Since it will be invoked right after the Execute) and if not, it will create a new Row in the Regions view Object. We do not have to worry about the initial values since we have done some coding in the create method as mentioned above.



Then we expose this method to the client interface and add it as a methodAction in the pageDef of the



And that is it.
When we run the page, it will open in find mode. If we set criteria that will bring any data, the page will enter in insert mode with initial values those data.

Download Sample Application.

Regards.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...