Filter a foreign key field depending on the current user session

Filter a foreign key field depending on the current user session

book

Article ID: KB0074664

calendar_today

Updated On:

Products Versions
TIBCO EBX All versions from 5.8.0.

Description

What is recommended for filtering a foreign field programmatically is to implement a TableRefFilter.
However, this class being a constraint, it is executed outside the context of an authenticated user session.
Thus we cannot retrieve a session object inside this class and so we cannot filter our combo box depending on it.

The solution to be able to filter a foreign key field depending on the current user session is to implement a custom widget.

Issue/Introduction

Filter a foreign key field depending on the current user session.

Resolution

In our example, we use a data model that has two tables and there is a foreign key from "table" to "table1".
In "table1", there is a "display" boolean field.

We want to filter a foreign key field depending on the following condition: the "display" target field is equal to true OR the current user is a built-in administrator.

Here is the implementation of the "write" method of the UISimpleCustomWidget:
 
public void write(WidgetWriter writer, WidgetDisplayContext context) {
 
// Get the session to know if the current user is an administrator
 
Session session = writer.getSession();
UserReference user = session.getUserReference();
boolean isAdmin = session.getDirectory().isUserInRole(user, Role.ADMINISTRATOR);
 
// Get the table targeted by the foreign key and create a request on it
 
ValueContext valueContext = context.getValueContext();
SchemaNode fkNode = context.getNode();
AdaptationTable targetTable = fkNode.getFacetOnTableReference().getTable(valueContext);
RequestResult result = targetTable.createRequestResult(null);
 
// Variables we will use in the loop
 
Adaptation aRecord = result.nextAdaptation();
 
boolean display;
String primaryKey;
String label;
 
// Create the nomenclature we will set to our widget
 
Nomenclature<String> nomenclature = new Nomenclature<String>();
 
while (aRecord != null){ // Loop through the request result
 
display = (boolean) aRecord.get(Path.parse("./display")); // Get the "display" boolean field
 
if (display || isAdmin){ // If the display field equals to true OR the current user is an administrator, we add the record to our nomenclature
 
primaryKey = aRecord.getOccurrencePrimaryKey().format();
label = fkNode.displayOccurrence(primaryKey, true, aRecord.createValueContext(), session.getLocale());
 
nomenclature.addItemValue(primaryKey, label);
 
}
 
aRecord = result.nextAdaptation();
 
}
 
// Create the widget, set its nomenclature and add it
 
UIComboBox box = writer.newComboBox(Path.SELF);
box.setSpecificNomenclature(nomenclature);
writer.addWidget(box);
 
}

Please see the data model and full code attached below.

Additional Information

https://docs.tibco.com/pub/ebx/5.9.10/doc/html/en/Java_API/com/orchestranetworks/ui/form/widget/UIWidgetFactory.html
https://docs.tibco.com/pub/ebx/5.9.10/doc/html/en/Java_API/com/orchestranetworks/ui/form/widget/UISimpleCustomWidget.html
https://docs.tibco.com/pub/ebx/5.9.10/doc/html/en/Java_API/com/orchestranetworks/ui/form/widget/UIAtomicWithEnumeration.html#setSpecificNomenclature-com.onwbp.base.text.Nomenclature-
https://docs.tibco.com/pub/ebx/5.9.10/doc/html/en/Java_API/com/orchestranetworks/ui/UISessionContext.html#getSession--

Attachments

Filter a foreign key field depending on the current user session get_app
Filter a foreign key field depending on the current user session get_app
Filter a foreign key field depending on the current user session get_app