Monthly Archives: March 2011

Reading the selected value of a select list in Java

Environment (JDeveloper 11.1.1.x, ADF BC, HR schema)

A frequent question on OTN is how to read the selected value not its index of    a select list in Java (This case exists in JDeveloper 11.1.1.x. because an expression like #{bindings.JobId.inputValue} would return the internal list index number when JobId was a list binding. In JDeveloper 11.1.2 this is no longer needed, the #{bindings.JobId.inputValue} expression will return the attribute value corresponding with the selected index in the choice list.) . in this post I will illustrate two methods to do this. I will assume that you already built your select list either model driven or dynamic select list.  Both of them are bound to the JUCtrlListBinding in the associated binding container.

Based on employee table in HR schema, where  the employee’s  DepartmentID attribute is bound as a list and populated from the DEPARTMENTS table,we need to read the department ID from the list in the valueChangeListener method using one of the follwing methods:

  • public void departmentIdChanged(ValueChangeEvent valueChangeEvent)
    {
    // Add event code here…
    BindingContext bctx = BindingContext.getCurrent();
    BindingContainer bindings = bctx.getCurrentBindingsEntry();
    JUCtrlListBinding list=(JUCtrlListBinding) bindings.get(“DepartmentId”);
    Row selectedRow = (Row)list.getSelectedValue();
    String selectedValue = list.getAttributeValue().toString();
    String deptName= selectedRow.getAttribute(“DepartmentName”).toString();
    System.out.println(“old department ID is ” + selectedValue );
    System.out.println(“old department name is ” + deptName);
    FacesContext contxt = FacesContext.getCurrentInstance();

    valueChangeEvent.getComponent().processUpdates(contxt);
    selectedRow = (Row)list.getSelectedValue();
    selectedValue = list.getAttributeValue().toString();
    deptName=  selectedRow.getAttribute(“DepartmentName”).toString();
    System.out.println(“new department ID is ” + selectedValue);
    System.out.println(“new department name is ” + deptName);

    }
  • The second method is by creating a secondary binding for DepartmentId and get its value as the follwings:
  1. From your page, right click and select Go to Page Definition.
  2. From the binding section, click the plus green icon to create a new control biding, and choose attributeValues from the list as shown in the  image below:

    create secondary attributeValues biding

  3. select your data source from the list, then select the DepartmentId attribute, as shown in the image below.After that change the id value to DepartmentIdValue.

    Data source and attribute selection

    DataSource and attribute selection

  4. Now you can access the DepartmentId value by using the following code:

public void departmentIdChanged(ValueChangeEvent valueChangeEvent)
{
// Add event code here…
BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
AttributeBinding deptIdBinding =   (AttributeBinding)bindings.getControlBinding(“DepartmentIdValue”);
String deptId = deptIdBinding.getInputValue().toString();
System.out.println(“old department ID ” + deptId );
FacesContext contxt = FacesContext.getCurrentInstance();

valueChangeEvent.getComponent().processUpdates(contxt);
deptId = deptIdBinding.getInputValue().toString();
System.out.println(“new department ID ” + deptId);

}

Note:

you can replace the bold cod, in the previous two methods

FacesContext contxt = FacesContext.getCurrentInstance();
valueChangeEvent.getComponent().processUpdates(contxt);

with this one, and both code segments lead to same result

int index;
index=Integer.parseInt(valueChangeEvent.getNewValue().toString());
list.setSelectedIndex(index);



BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
JUCtrlListBinding list =    (JUCtrlListBinding) bindings.get(“DepartmentId”);
Row selectedRow = (Row)list.getSelectedValue();
String selectedValue = list.getAttributeValue().toString();
System.out.println(“old department is ” + selectedValue );
list.setSelectedIndex(Integer.parseInt(valueChangeEvent.getNewValue().toString()));
selectedValue = list.getAttributeValue().toString();
System.out.println(“new department is ” + selectedValue);

16 Comments

Filed under ADF

Set an attribute’s default value with a sequence number

Environment (JDeveloper 11.1.1.3.0, ADF BC)

One of the common use case is to show the sequence Number of an entity’s attribute as a default value when we create a new record. In other words allocate a sequence number at entity row creation time so that the user can see its value and so that this value does not change when the data is saved.
This can be done easily through a customized EntityImpl class which
can read a database sequence and assign the value to the one of entity’s attributes.
The steps for doing this are:

  • Firstly,  set the type of your attribute to Number not to DBSequence. The DBSequence type is used when your attribute is set by a DB trigger, which is not our case. In our case we have a DB sequence but we don’t have a trigger to set a table column by before insert trigger.
  • Adding the following method to your EntityImpl class in my example CustomerEntityImpl
    protected Number nextVal(String sequenceName) {
    SequenceImpl s = new SequenceImpl(sequenceName, getDBTransaction());
    return s.getSequenceNumber();
    }
  • you will need to explicitly import oracle.jbo.server.SequenceImpl.
  • To call this method and make the sequence value as a default value for your attribute, you have two options, only apply one of them:
  1. You can call it from the create() method in the CustomerEntityImpl class  to set the value of your attribute in my example the CustomerId:
    protected void create(AttributeList attributeList) {
    super.create(attributeList);
    //Customer_Seq is the sequence name in DB
    setCustomerId(nextVal(“Customer_Seq”));
    }
  2. Go back to your entity object, click on the attribute dialog, choose your attribute (as mentioned before, in my case CustomerId), click edit selected attribute (pencil picture at the top), set ValueType to Expression and enter “adf.object.nextVal(“Customer_seq”);”

7 Comments

Filed under ADF