RESOURCES - Mvel
Definition of Mvel (from the Wiki):
​
Mvel stands for "MVFLEX Expression Language" and it's used to evaluate expressions written using Java syntax. In addition to the expression language, MVEL serves as a templating language for configuration and string construction.
​
I have used Mvel to transform and format numeric outputs in a very bespoke way.
I identified different initial situations in the numeric source data in the table and I could add on top of it the required transformation to push the exact output expected in the app depending on each case identified using Mvel in the API.
​
Here is the generic look of an mvel injection of java code in your app. It's done through an API call, using Visual Studio Code to keep some parameters predefined. It would result in a small change in the way the data is displayed, as we're playing on different cases in the values in different fields to see a percent or a unit from a specific float field.
​
​
PUT {{host}}/{{client}}/{{api}}/methodtype/etc
{{Content-Type}}
Authorization: Bearer {{token}}
​
{
"parameter1": "value1";
"parameter2": "value2";
"parameter3": "value3";
etc etc
"template": "@code{valueretrieved=parameter.?results.?arrayObject[0].?SomeResultingList; aStringOutput=''; for(int i = 0; i < valueretrieved.size(); i++){if(valueretrieved.get(i).?aNumericColumn==666 && valueretrieved.get(i).?aStringColumn1=='astringvalue1' && valueretrieved.get(i).?aStringColumn2=='astringvalue2'){if((valueretrieved.get(i).?aStringColumn3=='unitType1') && !((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){aStringOutput=valueretrieved.get(i).?aFloatColumn+' '+valueretrieved.get(i).?aStringColumn3;}if((valueretrieved.get(i).?aStringColumn3=='unitType1') && ((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){aStringOutput=String.format(\"%.0f\",valueretrieved.get(i).?aFloatColumn)+' '+valueretrieved.get(i).?aStringColumn3 ;}if((valueretrieved.get(i).?aStringColumn3=='unitType2') && !((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){aStringOutput=valueretrieved.get(i).?aFloatColumn+valueretrieved.get(i).?aStringColumn3;}if((valueretrieved.get(i).?aStringColumn3=='unitType1' ) && ((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){aStringOutput=String.format(\"%.0f%%\",valueretrieved.get(i).?aFloatColumn);}}};}@{aStringOutput}"
}
We can break down the code to make it clearer and have a look only at the template parameter, the final one where we push our data modification depending on different scenarios. The API call done with Visual Studio Code would need it in one single line with no break line to work fine.
​
"template": "
@code{
valueretrieved=parameter.?results.?arrayObject[0].?SomeResultingList;
aStringOutput='';
for(int i = 0; i < valueretrieved.size(); i++){
if(valueretrieved.get(i).?aNumericColumn==666 && valueretrieved.get(i).?aStringColumn1=='astringvalue1' && valueretrieved.get(i).?aStringColumn2=='astringvalue2'){
### Below we evaluate some fields and use the Java matches() function
if((valueretrieved.get(i).?aStringColumn3=='unitType1') && !((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){
### Below we simply output the raw data adding a space between the 2 columns "aFloatColumn" and "aStringColumn3"
aStringOutput=valueretrieved.get(i).?aFloatColumn+' '+valueretrieved.get(i).?aStringColumn3;
}
if((valueretrieved.get(i).?aStringColumn3=='unitType1') && ((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){
### Below we use the Java String.format() function to format the output as wished
aStringOutput=String.format(\"%.0f\",valueretrieved.get(i).?aFloatColumn)+' '+valueretrieved.get(i).?aStringColumn3 ;
}
if((valueretrieved.get(i).?aStringColumn3=='unitType2') && !((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){
aStringOutput=valueretrieved.get(i).?aFloatColumn+valueretrieved.get(i).?aStringColumn3;
}
if((valueretrieved.get(i).?aStringColumn3=='unitType1' ) && ((String.valueOf(valueretrieved.get(i).?aFloatColumn)).matches(\"(.*)(.0$)\"))){
aStringOutput=String.format(\"%.0f%%\",valueretrieved.get(i).?aFloatColumn);
}
}
};
}
@{aStringOutput}"
}
​
We see how the java code is used to reach some columns of a table depending on different conditions and then how we can on top add some transformations on other columns.
Add some tests to verify the output, and it's done.
​
​