Skip to main content

Making the Expander Conditional

Not all expanders and features should be applied to each element. Most expanders only run when some option is defined or a certain setting is used. For this, we can use the isApplicable condition.

Testing the condition

By default, {Expander}Test.java will contain a test method test_isApplicable to test this condition:

@Test
public void test_isApplicable() {
DataElementComposite model = baseModel();

tester.testIsNotApplicable(model);

tester.testIsApplicable(model);
}

Let's say that the expander should only run when the dataOption demo.includeSomeClass is added.

We then modify the test to test the 2 cases:

@Test
public void test_isApplicable() {
DataElementComposite model = baseModel();

tester.testIsNotApplicable(model);

specBuilder.extendModel(model, dataOption("demo.includeSomeClass"));

tester.testIsApplicable(model);
}

The extendModel() method will take the DataElementComposite and add a dataOption as defined in the second argument.

Implementing the isApplicable condition

Next, the isApplicable condition should be implemented. To do this, we implement the condition as an OGNL expression in the xml:

<isApplicable>dataElement.getOption('demo.includeSomeClass').defined</isApplicable>
note

Each element with options has the getOption(String optionTypeName) which will return an object with a value and isDefined()/isEmpty() methods.

Context Variables

The isApplicable condition shares most of its context variables with regular expander mapping, namely:

  • the currently expanded element as an instance of its composite projection
    • e.g. dataElement is instance of DataElementComposite
  • expansionContext for the current element type and expanded element,
    • e.g. expansionContext is instance of DataElementExpansionContext

And convenience shortcuts:

  • application
  • applicationInstance
  • businessLogicSettings
  • globalOptionSettings
  • presentationSettings
  • technicalInfrastructure

Note that you can access these elements by navigating through the model.

For example the following two conditions are the same (for a dataElement expander):

<isApplicable>globalOptionSettings.enforceHttpMethod</isApplicable>
<isApplicable>dataElement.component.application.applicationInstance.globalOptionSettings.enforceHttpMethod</isApplicable>