We have seen the basic theory behind the execution of entity and view objects in the first part of this article. In the second part , let us put the theory in to action.
You can download the sample workspace from here.
[Runs with Oracle JDeveloper 126.96.36.199.0 (11g R2) + HR Schema]
Please note this example does not use any API specific to 188.8.131.52 release, so you may be able to reuse the source classes in previous versions also.
A glance at the implementation
This example uses two model objects - Department and Employee. The 'Department' is based on DEPARTMENTS table and it follows usual entity and view object generation steps, whereas Employee is based on custom data source and the steps for generating programmatically managed Employee model object is given below.
Building base classes for your programmatic entity and view objects
While building a programmatic view or entity objects you may need to hook your code to perform CRUD (Create Read Update Delete) operations against the custom data source (as per your use case) by overriding the life cycle methods(by default CRUD is done against database). This example uses generic base classes for doing this job and they are copied below for your reference.
ProgrammaticallyManagedEntityObjectImpl : Abstract entity object which overrides EntityImpl::doDML(...) and related methods.
ProgrammaticallyManagedViewObjectImpl : Abstract view object which overrides ViewObjectImpl::executeQueryForCollection(...) and related methods.
GenericQueryModel : A generic query model class which holds the filtering criteria for third party services.
GenericRowCollection : A generic data structure to hold the result from the query(data retrieval) execution.
GenericRowData : A generic data structure representing a row in the result set.
Building programmatic entity object
Step 1: Right click the model project, select New->ADF Business Components-> Entity Objects. You can key in the entity name and leave the default values for the other fields as is, click Next.
Step 2: Add attributes and finish the wizard
Step 3: Select the generated entity object in the editor, go to Java tab and click on edit Java Options. Change the base class for the Entity Object as fmwk.extension.entity.ProgrammaticallyManagedEntityObjectImpl(what we generated initially)
You may need to override ProgrammaticallyManagedEntityObjectImpl::getService() to return GenericDataPersistanceService implementation. Please see Step 4 for the service implementation.
Step 4: Build the business service implementing fmwk.extension.view.GenericDataPersistanceService for updating the data. Please take a look at model.emp.service.MyEmpDataServiceImplementation to get a feel of the implementation.
Building programmatic view object
Step 1: Select the programmatic entity object, right click and then select New Default View Object option from the context menu.
Step 2: Switch to the source mode of the generated view object and set the CustomQuery="true" as shown in the following image. This is very important step as this flag influences the run time to avoid the query execution against data base(which is the default execution path)
Ste 3: Select the generated view object in the editor, go to Java tab and click on edit Java Options. Change the base class for the View Object as fmwk.extension.entity.ProgrammaticallyManagedViewObjectImpl(what we generated initially)
You may need to override ProgrammaticallyManagedViewObjectImpl::getService() to return GenericDataReadServiceInterface implementation. Please see Step 4 for the service implementation.
Step 4: Build the business service to read data by implementing fmwk.extension.view.GenericDataReadService. Please take a look at model.emp.service.MyEmpDataServiceImplementation to get a feel of the implementation.
Can I reuse this sample source?
Well, the classes what you see in package fmwk.extension.xxx are generic in nature and reusable. If you have similar use cases, then you may need to implement an adapter for your 'data source' calls as shown in model.emp.service.MyEmpDataServiceImplementation. The core run time classes what you see under fmwk.extension.xxx can be reused either as is or with some modifications.
How to run this sample?
This example is based on Department-Employee model. As I said earlier the Department is based on data base table and Employee is programmatically manged. This sample contains three pages illustrating search, update and master-child coordination on programmatic view object.
- search.jsf - Search page for programmatic view object.
- masterChild - Master child coordination(Department-Employee) through view link.
- edit.jsf - Edit page for programmatic entity object.
There are a lot more points like this. If you are curious to learn the internals of the ADF Business Components and ADF Binding Layer, the following book is for you - Oracle ADF Real World Developer’s Guide.
More details about this book can be found in this post- http://jobinesh.blogspot.in/2012/07/pre-order-your-copy-of-oracle-adf-real.html