Category Archives: ADF

Oracle Application Development Framework

Retrieve the Old Value of an Attribute in the View Object

Sometimes we need to access the old value of an attribute, this is explained well by Chris Muir in ADF Insider video Retrieving the previous value of an ADF BC attribute. This can be done by using getPostedAttribute method in the EntityImpl class, this method gets the value originally read value for  a specific attribute from the database and it has a protected access modifier. But what if you want to access the old value in the View object and you don’t have a control on the Entity object; such as if the entity objects are deployed as ADF library jar and you don’t have any privilege to access or modify these entities; is it possible to access the old value from the view object? in this post I will show how do this in an easy way. Suppose we want to access the old value of the department name attribute in the DepartmentsView  which is based on Departments table in HR schema.

  1.  Create a transient attribute, OldDepartmentName in the DepartmentsView.
  2. Generate the DepartmentsViewRowImpl class, check the include accessors check box, this will add getter/setter methods for all the attributes of this view object. by default the getDepartmentName method will look like this: 
    /**
    * Gets the attribute value for the calculated attribute OldDepartmentName.
    * @return the OldDepartmentName
    */
    public String getOldDepartmentName() {
    return (String) getAttributeInternal(OLDDEPARTMENTNAME);
    }
  3. Override the getOldDepartmentName() method to:
        /**
         * Gets the attribute value for the calculated attribute OldDepartmentName.
         * @return the OldDepartmentName
         */
        public String getOldDepartmentName() {
            byte status = EntityImpl.STATUS_UNMODIFIED;
            return (String) getAttributeInternal(DEPARTMENTNAME, status);
        }

Now, the getOldDepartmentName() will return the old value from the database.

Leave a comment

Filed under ADF

Two Ways to Evaluate a Memory Scope Attribute Using Expression Language

All of us know how to evaluate a memory scope attribute using a dot notation syntax of the expression language.

Example: to access a request scope attribute, we use this syntax.

#{requestScope.attributeKey}

However, this syntax fails if the key has a dot (.) in its name. To overcome this limitation, expression language offers the string argument notation.

Example: to access a request scope attribute which has a (.) in its name, we use this syntax.

#{requestScope['attributeKey']}

 

1 Comment

Filed under ADF

How to Disable Specific Dates in af:inputDate Component

Environment (JDeveloper 11.1.2.3.0,ADF Faces)

The inputDate component creates a text field for entering dates and (optionally) a glyph which opens a popup for picking dates from a calendar. However  in some cases we need to prevent the user from picking a specific days from the calender. In this post I will explain how to do this use case.

1- Create a simple JSF page and drag and drop af:inputDate component from the component palette into the newly created page.

2- Create a java class which implements org.apache.myfaces.trinidad.model.DateListProvider interface. This interface is used for providing a list of  individual dates within a given range.

3- Override the getDateList method, This method will generate a List of individual Date objects which will be rendered as disabled in a datePicker component.  An example is shown below.

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.myfaces.trinidad.model.DateListProvider;

public class DaysBean
  implements DateListProvider
{
  public DaysBean()
  {
    super();
  }

  @Override
  public List getDateList(FacesContext facesContext, Calendar calendar, Date date, Date date2)
  {
    ArrayList disabledDates= new ArrayList();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    try
    {
      disabledDates.add(sdf.parse("01/01/2013"));
      disabledDates.add(sdf.parse("25/12/2013"));
    }
    catch (ParseException e)
    {
      e.printStackTrace();
      return Collections.emptyList();
    }
    return disabledDates;
  }
}

4-Register the Java Class as Managed Bean in adfc-config.xml file.

       Register Java Class as Managed Bean in adfc-config.xml file

5- Go to the JSF page and select the inputDate component, from the property inspector select the DisabledDays property and bind it to the manged bean defined in the previous step.

<af:document title="Test.jsf" id="d1">
<af:form id="f1">
<af:inputDate label="Date" id="id1" disabledDays="#{daysBean}"/>
</af:form>
</af:document>

Note:

 this binding requires periodic roundtrips. If you just want to disable certain weekdays (e.g. Saturday and Sunday), use the disabledDaysOfWeek attribute!

6- Run the page, and check that the date is disabled.

01/01/2013 is Disabled

Moreover, if the user enters the date manually without using the date picker, an error message will be shown to the user.

Error Message When the date is Entered Manually

download the sample application TestDisableDays.rar

Please change the file extension to .zip or .rar after download.

Leave a comment

January 24, 2013 · 10:59 am

Application Development Framework Implementation specialist

Today, I took the Oracle Application Development Framework 11g Essentials exam (1Z0-554). Fortunately I passed it, and I am now an Application Development Framework Implementation specialist.

O_Certified Specialist_clr

4 Comments

Filed under ADF

How to Solve oracle.jbo.ConfigException: JBO-33001

Environment (JDeveloper, ADF BC, ADF Faces)

When you work with large application which contains more than one Model/one ViewController projects, you need to set the project dependency between the model and the view controller project., failing to do this you may face this exception.

oracle.jbo.ConfigException: JBO-33001: Configuration file /model/common/bc4j.xcfg is not found in the classpath.

To solve the problem, double click the viewController project to open project properties dialog, and select the the Dependencies node  and click the Edit Dependecies icon to add a dependency on the build output path or on one or more deployment archives.

Hope this will help someone faced the same problem.

3 Comments

Filed under ADF

Simple way to convert af:inputText to upperCase,lowerCase,capitalize

Environment (JDeveloper 11.1.2.2.0,ADF Faces)

Sometimes you need to force the user to enter upperCase or lowerCase or capitalize letters for input text component. fortunately, there is an easy way to do this by setting contentStyle of the input text.

  • To enforce the user to enter upperCase text use this style:
<af:inputText label="Label 1" id="it1" contentStyle="text-transform:uppercase;"/>
  • To enforce the user to enter lowerCase text use this style:
<af:inputText label="Label 1" id="it1" contentStyle="text-transform:lowercase;"/>
  • To enforce the user to enter capitalize (InitCap) text use this style:
<af:inputText label="Label 1" id="it1" contentStyle="text-transform:capitalize;"/>

5 Comments

Filed under ADF

How to make some LOV items non-selectable

Environment (JDeveloper 11.1.2.0.0,hr schema)

Lately we have a requirement to make some list of values (LOV) items non selectable. As an example suppose that the list of departments should show all departments but some of them mus be disabled or non-selectable as shown below.

LOV with some items being disabled

In this post I will show how to build this kind of LOVs. This example is based on hr schema, mainly on EMPLOYEES and DEPARTMENTS table.  Our goal is to create LOV for the DepartmentId attribute in Employees view but the departments: Administration, Marketing, Purchasing, and Human Resources should be disabled and non selectable.  I will assume that you have already built the business component for this example. The implemantation steps are:

  1. Right click on your page and select Go to page Definition from the menu.
  2. If the page definition file opens in the source view, select the Overview tab at the bottom of the editor. and click the green plus icon  to create a control binding as shown below.create control binding
  3. After click the green plus icon the Insert Item window will be shown, select the tree item and click OK.

    select tree binding from insert item window

  4. After you select the tree binding, the Create Tree Binding window will be shown. Click the Add button to create a new Root Data Source reference or select from a list of existing iterators.The root data source provides the list data and should point to a View Object that you created for the data lookup. Don’t use any View Object that you use for data input within your application. In our case I select DepartmentsView1 to be the root data source.

    select root data source from the data controls

  5. Still the Create Tree Binding window opened, click add rule (green plus icon) and choose the AddRule menu option. This creates a rule entry for the top-level View Object of the selected root data source.

    add rule menu option

  6. Still also in the Create Tree Binding window, move both DepartmentId and DepartmentName to the Display attribute area. we need the DepartmentId for the list value and the DepartmentName to be the display value.

    select display attributes

  7. Now return back to the page and open it it the Jdeveloper visual editor.
  8. From the data controls panel, drag and drop the departmentId attribute from the EmployeesView1 as selectOneChoice component. Press OK and do not do any configuration. Note that Jdeveloper shows an error message Indicates that a list data source is not selected, so we are enforced to select a data source and we will delete it later.

    Edit lis binding window

  9. Now the page source for the DepartmentId attribute will be something like this:
    <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" label="#{bindings.DepartmentId.label}"
    required="#{bindings.DepartmentId.hints.mandatory}"
    shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1">
    <f:selectItems value="#{bindings.DepartmentId.items}" id="si1"/>
    </af:selectOneChoice>
  10. Delete <f:selectItems> tag  inside <af:selectOneChoice> tag. The page source for the DepartmentId attribute should be:
    <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" label="#{bindings.DepartmentId.label}"
    required="#{bindings.DepartmentId.hints.mandatory}"
    shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1"/>

    You can also delete the f:selectItems child component from the structure window.

  11. From the structure window, select the af:selectOneChoice and Insert inside af:selectOneChoice |ADF Faces | For Each from the right mouse context menu as shown below.

    insert foreach component

  12. Select the af:forEach component and open the Property Inspector. Click the arrow icon next to the Items property and select the tree binding rangeSet method entry.The returned Expression Language expression should look like this: #{bindings.
    DepartmentsView1.rangeSet}. And set the var attribute to descriptive name. The Var property defines the name for the variable that at runtime is used to populate the list. In this example I chose list as a name.
  13. Now our DepartmentId attribute looks like:
     <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" label="#{bindings.DepartmentId.label}"
    required="#{bindings.DepartmentId.hints.mandatory}"
    shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1">
    <af:forEach items="#{bindings.DepartmentsView1.rangeSet}" var="list"/>
    </af:selectOneChoice>
  14. Select the af:forEach component in the Structure Window and choose Insert inside af:forEach | ADF Faces  and choose Select Item component as shown below.

    insert <af:selectItem> component

  15. For the af:selectItem component set the Value attribute to #{list.DepartmentId}, Label attribute to #{list.DepartmentName}, and Disabled attribute to #{list.DepartmentId eq 10 or list.DepartmentId eq 20 or list.DepartmentId eq 30 or list.DepartmentId eq 40} which represents the Ids of the departments we want to be disabled.
  16. With this configuration applied, the DepartmentId attribute look like this:
    <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" label="#{bindings.DepartmentId.label}"
    required="#{bindings.DepartmentId.hints.mandatory}"
    shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1">
    <af:forEach items="#{bindings.DepartmentsView1.rangeSet}" var="list">
    <af:selectItem label="#{list.DepartmentName}" id="si1" value="#{list.DepartmentId}"
    disabled="#{list.DepartmentId eq 10 or list.DepartmentId eq 20 or list.DepartmentId eq 30 or list.DepartmentId eq 40}"/>
    </af:forEach>
    </af:selectOneChoice>
  17. Run your page.

download the sample workspace:MakeSomeLOVItemsNonSelectable.rar

Please change the file extension to .zip or .rar after download.

 

Reference: Oracle Fusion Developer Guide Building Rich Internet Applications with Oracle ADF Business Components and Oracle ADF Faces. Frank Nimphius,Lynn Munsinger.

6 Comments

Filed under ADF

How to pass parameters to an entry defined in the resource bundle

Environment (JDeveloper 11.1.2.0.0)

A validator’s error message can contain embedded expressions that are resolved by the server at runtime. This can be done easily in the model layer using named tokens delimited by curly braces as explained in  7.7.4 How to Embed a Groovy Expression in an Error Message section in developer guide. However, sometimes we need to pass a parameter to a string defined in resource bundle in the view layer. In this post I will show How to do it.

A resource bundle contains a number of named resources, where the data type of the named resources is String, and it is used to internationalize the application. I will assume that you have already defined your resource bundle and register it in the faces-config.xml as explained in Internationalizing and Localizing Pages chapter from web user interface guide.

Now suppose our resource bundle file contains this entry

#
MESSAGE=Hello {0}. You are watching {1}. Enjoy it.

you want to pass parameters  to the tokens delimited by curly braces, this can be done by using StringManger class as shown in the following method

public String PassParameterToBundle() {
// Add event code here...
String message=  oracle.jbo.common.StringManager.getString("view.ViewControllerBundle", "MESSAGE", "No matching string found",new String[]{"ADF Developer","https://mjabr.wordpress.com/"});
System.out.println("message " + message);
return message;
}

view.ViewControllerBundle in the method is the resource bundle that is registered in the faces-config.xml file as shown below

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee">
<application>
<default-render-kit-id>oracle.adf.rich</default-render-kit-id>
<resource-bundle>
<base-name>view.ViewControllerBundle</base-name>
<var>res</var>
</resource-bundle>
</application>
</faces-config>

after executing the method you will see this message in the console: Hello ADF Developer. You are watching https://mjabr.wordpress.com/. Enjoy it.

Leave a comment

Filed under ADF

Using comma separated string as a bind variable for SQL query with IN clause

Environment (JDeveloper 11.1.2.0.0, hr schema)

Bind variables are place holders in the SQL string whose value  can be easily changed at runtime without altering the query itself. Since the query text doesn’t change from execution to execution, the database can efficiently reuse the same parsed statement each time (parse once and execute many times). But how can we define a bind variable for IN clause, as an example suppose that we want to pass a comma-separated string of department IDs and execute the query based on our IN clause, and here is the query we wish to execute look like:

select * from departments where department_id IN (:comma-separated-list)

Fortunately, I have found how to do this based on  Steve Muench’s example #126.Using Comma-Separated String Bind for Variable IN List and in this post I am only repeating what Steve has shown before for the sake of sharing. Our goal is to pass a comma-separated string of department IDs at run time and execute our query.

To support bind variable IN clause we need to add the function below to our schema, this function accepts a comma-separated string argument and returns a NUM_TABLE as its result and this allows us to make our bind variable of String type.

CREATE TYPE num_table AS TABLE OF NUMBER;
/
CREATE OR REPLACE FUNCTION in_number_list (p_in_list  IN  VARCHAR2)
RETURN num_table
AS
l_tab   num_table := num_table();
l_text  VARCHAR2(32767) := p_in_list || ',';
l_idx   NUMBER;
BEGIN
LOOP
l_idx := INSTR(l_text, ',');
EXIT WHEN NVL(l_idx, 0) = 0;
l_tab.extend;
l_tab(l_tab.last) := to_number(TRIM(SUBSTR(l_text, 1, l_idx - 1)));
l_text := SUBSTR(l_text, l_idx + 1);
END LOOP;
RETURN l_tab;
END;
/

our DepartmentsView should look like this

SELECT Departments.DEPARTMENT_ID,
Departments.DEPARTMENT_NAME,
Departments.MANAGER_ID,
Departments.LOCATION_ID
FROM DEPARTMENTS Departments
WHERE DEPARTMENT_ID IN (SELECT * FROM TABLE( CAST ( in_number_list(:CommaSeparatedListOfDeptId) as num_table)))

where CommaSeparatedListOfDeptId is a bind variable of type String.

DepartmentsView

you can test the view using the ADF Model Tester and pass a comma-separated string of departments IDs as shown below:

Oracle ADF Model Tester

2 Comments

Filed under ADF

Access an Instance of a Managed Bean from another Managed Bean

Environment (JDeveloper 11.1.2.0.0)

Lately we had a case in which we needed to access an instance of  pageFlowScope bean from a  requestScope bean, this is a common use case and many developers may need to do the same. In this post I will explain how to do this using two options.

Firstly suppose we have two java classes one of them registered as  pageFlowScope bean and the other is registered as requestScope bean as shown below.

package view.managed;
public class PageFlowScopeClass {
public PageFlowScopeClass() {
super();
}
private String someData;
public void setSomeData(String someData) {
this.someData = someData;
}
public String getSomeData() {
return someData;
}
}
package view.managed;
public class RequestScopeClass {
    public RequestScopeClass() {
        super();
    }
}

both classes are registered in adfc-config.xml file

<?xml version="1.0" encoding="windows-1252" ?>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
  <managed-bean id="__1">
    <managed-bean-name>PageFlowScopeBean</managed-bean-name>
    <managed-bean-class>view.managed.PageFlowScopeClass</managed-bean-class>
    <managed-bean-scope>pageFlow</managed-bean-scope>
  </managed-bean>
  <managed-bean id="__2">
    <managed-bean-name>RequestScopeBean</managed-bean-name>
    <managed-bean-class>view.managed.RequestScopeClass</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
</adfc-config>

The first option to access an instance of PageFlowScopeBean from RequestScopeBean is by adding a managed property to the RequestScopeBean, and here are the steps to do:

  1.  open the adfc-config.xml file, click on Overview menu, select the Managed Beans node, select RequestScopeBean and click the add managed property icon as shown below.

    Add Managed Property

  2. enter the name,class,and the value  for the property in our case these field should be:
    Name: myPageFlowBean
    Class: view.managed.PageFlowScopeClass
    value: #{pageFlowScope.PageFlowScopeBean}
    Now adfc-config.xml will be something like this

    <?xml version="1.0" encoding="windows-1252" ?>
    <adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
    <managed-bean id="__1">
    <managed-bean-name>PageFlowScopeBean</managed-bean-name>
    <managed-bean-class>view.managed.PageFlowScopeClass</managed-bean-class>
    <managed-bean-scope>pageFlow</managed-bean-scope>
    </managed-bean>
    <managed-bean id="__2">
    <managed-bean-name>RequestScopeBean</managed-bean-name>
    <managed-bean-class>view.managed.RequestScopeClass</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property id="__5">
    <property-name>myPageFlowBean</property-name>
    <property-class>view.managed.PageFlowScopeClass</property-class>
    <value>#{pageFlowScope.PageFlowScopeBean}</value>
    </managed-property>
    </managed-bean>
    </adfc-config>
  3. in RequestScopeClass create an instance variable of type PageFlowScopeClass with the same name that exists in managed properties  and generate accessors; a getter and a setter for this variable. Now the RequestSCopeClass should be
    package view.managed;
    
    public class RequestScopeClass {
        private PageFlowScopeClass myPageFlowBean;
    
        public RequestScopeClass() {
            super();
        }
    
        public PageFlowScopeClass getMyPageFlowBean() {
            return myPageFlowBean;
        }
    
        public void setMyPageFlowBean(PageFlowScopeClass myPageFlowBean) {
            this.myPageFlowBean = myPageFlowBean;
        }
    }
  4. now you can get the PageFlowScopeBean instance by calling getMyPageFlowBean() method.

The other option to access an instance of PageFlowScopeBean is to use ExpressionFactory class  as shown below

package view.managed;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;

public class RequestScopeClass {
public RequestScopeClass() {
super();
}

public PageFlowScopeClass getInstanceOfPageFlowScopeBean() {
FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
ExpressionFactory expressionFactory = application.getExpressionFactory();
ELContext context = fctx.getELContext();
ValueExpression createValueExpression = expressionFactory.createValueExpression(context, "#{pageFlowScope.PageFlowScopeBean}",PageFlowScopeClass.class);
PageFlowScopeClass pageFlowScopeInstance =(PageFlowScopeClass) createValueExpression.getValue(context);
return pageFlowScopeInstance;
}
}

to get an instance of PageFlowScopeBean all you need is just to call  getInstanceOfPageFlowScopeBean() method.

8 Comments

Filed under ADF