Version: 22.04
Form Version Selector
The Form Version Selector service is used to select the version of a form to render when a user starts accessing the form. For simple purposes, the current version of a form can be used; for example in A/B testing, a version of the form may be selected either completely at random or depending on certain characteristics of the user.
This service is configured via the Form Version Services tab.
Service Invoke Parameters
Parameters are not nullable except where otherwise indicated.
Parameter | Description |
---|---|
svcDef | SvcDef A service definition value object. |
form | Form A form value object. |
request | HttpServletRequest A HTTP servlet request. |
user | User Nullable. An authenticated user. |
Script Result
The script returns a String
object. If the returned object is null
, the current template version is resolved; otherwise, the returned String
object is resolved to the selected template version.
Example
The following example demonstrates region-based form version selection.
import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.vo.*
import javax.servlet.http.*
class FluentFormVersionSelector {
/*
* Perform form version selector service
*/
String invoke(SvcDef svcDef, Form form, HttpServletRequest request, User user) {
String country = request.getParameter('country')
if ('AU'.equals(country)) {
return '1.0'
}
if ('DE'.equals(country)) {
return '2.0'
}
if ('JP'.equals(country)) {
return '3.0'
}
return null
}
}
Templates
Unit Test
import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.svc.*
import com.avoka.tm.test.*
import com.avoka.tm.vo.*
import org.junit.Test
class UnitTest extends AbstractJUnitTest {
/*
* Perform service unit test
*
* throws: exception if unit test fails
*/
@Test
void testVersionAU() throws Exception {
Form form = new MockVoBuilder().createForm()
MockRequest request = new MockRequest()
request.setParameter('country','AU')
Map params = [
"svcDef": svcDef,
"form": form,
"request": request,
"user": null
]
try {
String result = new ServiceInvoker(svcDef).invoke(params)
assert result == '1.0'
} catch (Exception re) {
assert false : 'Test failed'
}
}
}