Tuesday, October 13, 2015

ADF: Dynamic entity object attribute labels from database

In JDeveloper & ADF forums there were a question about how to set dynamic values to entity object attribute labels. Using this post of Amis I am going to show you the way we can achieve this.

By default entity objects attributes labels are set, as you can see, for example, in Departments table in HR schema.

The first step is to create in our database the structure and packages we are going to use to create and store the labels.

The table we have to create is "RESOURCE_BUNDLE" and is has 3 fields:

We will also add labels values for each field and locale.

We need a PL/SQL package to retrieve the labels of the selected locale.

Now that we have finished to create out database needs, we have to create a method in Application Moduel Implementation class that will call the package we have previously created.

The next step is to create the Resource Bundles in our application. We are going to use Amis' DBResourceBundle class as a start point because this class will call application module's method to get the labels from the database.

In getResourceBundle method we have to configure amDef and config variables so we can use createRootApplicationModule method and get our Application Module.

Now that we have created our base ResourceBundle, we have to create the resoruce bundles of out applications and make them extend out DBResourceBundle class. We are going to have English, Spanish and French locales.

In all the clases we are going to override getLocaleCode method where we are going to return the locale code.

The next step is to register our bundle in Model project. We have to open model project properties and select 'Resource Bundle' in the menu.

We have to select 'One Bundle per Project', fill in the bundle path without the extension and select Resource Bundle Type as 'List Resource Bundle'.

If we have more than one bundle we can add them in 'Bundle Search' tab.

The last step is to edit attribute labels in the entity object.
Para terminar hay que modificar las etiquetas de cada uno de los atributos.
We are going to create a label so out entity object is modified with the required tags we are going to use.

If we look at the entity source code, we can see that a couple of things has been added.

The first one is the Resource Bundle we are going to use.

And the second one is the id of the label in the ResourceBundle.

We will have to add Properties tag to every attribute and edit ResId property with the key registered in the database.

Now we are done and we have to test what we have implemented using ADF Model Tester, where we can change the locale if we activate it in JDeveloper properties.

When we run the Application Module we can see that by default English labels.

The change the locale now we have 'Locale' option in the menu.

If we change the locale to Spanish and close the view, when we open it again we can see that labels are now in Spanish.

1 comment: