Friday, November 22, 2013

ADF Good Practice: Provide your own base classes

Hi all. I just wanted to share a really good practice when developing Oracle ADF applications. I have used this on several projects and it has always saved me time when I need to apply some new behavior to all my business components. Credits to Sten Vesterli for this best practice that I found on his book Oracle ADF Enterprise Application Development—Made Simple

When you are working with Business Components (Entities, ViewObjects, Application Modules, etc...) you are extending Oracle ADF base classes, the most common base classes are:
  • oracle.jbo.server.EntityImpl is used for your entities implementations
  • oracle.jbo.server.ViewObjectImpl and oracle.jbo.server.ViewRowImpl are used for your view object implementations
  • oracle.jbo.server.ApplicationModuleImpl is used for your application module implementations
So, what happens if after a few months of coding you need some new behavior in all your entities? If you are thinking in implementing a java class for every entity... you are making a bad decision for the future of your project.
What you need is to provide your own base classes and make sure that all your entities, view objects and application modules use your extended framework. And when changes arises, you can modify your base classes and then changes are applied to all your business components.

For example, when we create a new entity, by default we are getting:



But, when we are providing your own base classes, we will get something like this:



And all we need to do to apply a general change is to modify yourpackage.YourOwnBaseEnityImpl

At first, we will end up with empty classes, but don't worry, you are saving for the future ;-)

In order to create our own base classes framework, in JDeveloper we go to File-->New-->Application... Then, select Applications on the left panel and Custom Application on the right panel:



Select a name for the application and define an application package prefix (optional):



Then select a name for the project and make sure you select ADF Buiness Components as a project feature:




Check the source path and output directory and if everything is OK, press the Finish button:



Once the project has been created, make sure that the BC4J Runtime library has been added to the project properties:



Select the Libraries and Classpath option on the left pane. If there is no BC4J Runtime library already set in your project go to the Add Library option and look for it:



Now we can create our own base classes that extends from the Oracle ADF base classes. Go to: File-->New-->From Gallery...Then look for the General option on the left and select Java class on the right



Set the name for the base class and make sure to extend from Oracle ADF base classes, in the following example we are extending from oracle.jbo.server.EntityImpl:



Following are the most common base classes extended:

//change package according to your company 
package com.javanme.commons.framework;
/**
 * Base class for Applications Modules
 */
public class EntityImpl extends oracle.jbo.server.EntityImpl {
}


//change package according to your company 
package com.javanme.commons.framework;
/**
 * Base class for ViewObjects
 */
public class ViewObjectImpl extends oracle.jbo.server.ViewObjectImpl {
}


//change package according to your company 
package com.javanme.commons.framework;
/**
 * Base class for View Rows
 */
public class ViewRowImpl extends oracle.jbo.server.ViewRowImpl {
}


//change package according to your company 
package com.javanme.commons.framework;
/**
 * Base class for Applications Modules
 */
public class ApplicationModuleImpl extends oracle.jbo.server.ApplicationModuleImpl {
}


Once we have created all the base classes, we will need to create an ADF library in order to be able to use them in other projects. Right click on the project and select Project Properties... Then select the Deployment option on the left and click on the New button on the right. A dialog pops up, select ADF Library JAR File and set a name for this deployment:



Click OK twice since no more configurations are needed:



Then right click on your project and select Deploy-->YourADFLibDeployment from the pop up menu:



If this is the first time you are deploying the library, a new window pops up, just click on Finish. Then, go to the default deploy location of the project which is on {APP_DIR}/{PROJECT_DIR}/deploy, where APP_DIR and PROJECT_DIR are the paths defined when the application and the project were created:



Copy the library to a directory where you save all your libraries and then in JDeveloper go to Window menu and select Resources:



A new window, on the right,  appears on JDeveloper. Click on the folder icon and select IDE Connections then File System. We are going to define a new resource that points to the directory where we save all the libraries so we can easily add libraries to our projects:



Set a name for the resource and find the directory where the ADF libraries are saved, the one we created steps above. Finally, click on Test Connection in order to validate that everything is OK:



Once we have created the new resource, we can expand the tree below IDE Connections and look for the library that we want to import to the selected project. Right click and select Add to Project:



A confirm dialog pops up, just press Add Library:



You have to repeat the same steps for every library that you want to import into your project. Whenever we want the project to load changes on the imported libraries, right click on the project and select Refresh ADF ibrary Dependencies in {ProjectName}:



Last step is really important because we are telling JDeveloper that it needs to use our base classes instead of the Oracle ADF base classes. We can do this in two ways: the first one is at project level, so we need to configure every project where we want to use our base classes. The second approach is at JDeveloper level so every single project will make use of our base classes. It's up to you to decide which approach to take, but at the end we need to configure the following window:



Notice that we are using our own base classes which are at com.javanme.commons.framework package. If you want this at project level, then right click on the project and select Project Properties from the pop up menu. If you want this at JDeveloper level go to Tools-->Preferences. Whatever the approach is, look for the ADF Business Components/Base Classes in the pop up window.

This was a long post, hopefully it will save you time in your future ADF projects.

See ya!


References:

Vesterli E., Sten (2011). Oracle ADF Enterprise Application Development—Made Simple. United Kingdom: Packt Publishing Ltd.


No comments:

Post a Comment