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.