| Products | Versions |
|---|---|
| EBX | All versions from 5.8.0. |
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.
public void write(WidgetWriter writer, WidgetDisplayContext context) {// Get the session to know if the current user is an administratorSession 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 itValueContext valueContext = context.getValueContext();SchemaNode fkNode = context.getNode();AdaptationTable targetTable = fkNode.getFacetOnTableReference().getTable(valueContext);RequestResult result = targetTable.createRequestResult(null);// Variables we will use in the loopAdaptation aRecord = result.nextAdaptation();boolean display;String primaryKey;String label;// Create the nomenclature we will set to our widgetNomenclature<String> nomenclature = new Nomenclature<String>();while (aRecord != null){ // Loop through the request resultdisplay = (boolean) aRecord.get(Path.parse("./display")); // Get the "display" boolean fieldif (display || isAdmin){ // If the display field equals to true OR the current user is an administrator, we add the record to our nomenclatureprimaryKey = 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 itUIComboBox box = writer.newComboBox(Path.SELF);box.setSpecificNomenclature(nomenclature);writer.addWidget(box);} |
Feedback submitted!