Class EvalScriptWithJsonDataModel

java.lang.Object
org.fugerit.java.script.helper.EvalScriptWithJsonDataModel
All Implemented Interfaces:
EvalScript

public class EvalScriptWithJsonDataModel extends Object implements EvalScript
EvalScript decorator. The Map dataModel will be transformed into a json styled LinkedHashMap.
  • Constructor Details

    • EvalScriptWithJsonDataModel

      public EvalScriptWithJsonDataModel(EvalScript evalScript)
      Add decoration to a give EvalScript
      Parameters:
      evalScript - the EvalScript instance to decorate
  • Method Details

    • defaultDataModelConversion

      public static Map<String,Object> defaultDataModelConversion(Map<String,Object> dataModel)
      Default data model conversion Only basic types will be used : String, Number, Boolean, Array, Object as Map. For instance the object :
       
                class Vehicle {
                   private String plate;
                   private int age;
                   public Vehicle(int age, String plate) {
                       this.age = age;
                       this.plate = plate;
                  }
                   public String getPlate() { return plate; }
                   public int getAge() { return age; }
                   @Override
                   public String toString() {
                      return "Vehicle{age="+age+", plate='"+plate+"'}";
                   }
               }
               Map<String, Object> dataModel = new HashMap<>();
               dataModel.put( "vehicle", new Vehicle( 10, "AA780BB" ) );
               Map<String, Object> jsonStyleDataModel = EvalScriptWithJsonDataModel.defaultDataModelConversion( dataModel );
               log.info( "originalDataModel : {}", dataModel );
               log.info( "jsonStyleDataModel : {}", jsonStyleDataModel );
             
       
      will result in : originalDataModel : {vehicle=Vehicle{age=10, plate='AA780BB'}} jsonStyleDataModel : {vehicle={plate=AA780BB, age=10}}
      Parameters:
      dataModel - the data model to convert
      Returns:
      the data model converted to map of simple types
    • handleEx

      public Object handleEx(Reader reader, Map<String,Object> dataModel) throws ScriptException
      Description copied from interface: EvalScript
      It will handle a script and bind a data model. Any class implementing EvalScript will need to provide at least this method.
      Specified by:
      handleEx in interface EvalScript
      Parameters:
      reader - the reader over script
      dataModel - the data model to bind
      Returns:
      the result of script processing
      Throws:
      ScriptException - in case of script handling issues
    • newEvalScriptWithJsonDataModel

      public static EvalScript newEvalScriptWithJsonDataModel(String scriptExtension, String dataModelBindingName)
      It will decorate a EvalScriptWithDataModel instance.
      Parameters:
      scriptExtension - the extension to be used for the ScriptEngine
      dataModelBindingName - the data model binding name
      Returns:
      the new EvalScript
    • newEvalScriptWithJsonDataModel

      public static EvalScript newEvalScriptWithJsonDataModel(String scriptExtension)
      It will decorate a EvalScriptWithDataModel instance with default data model binding name.
      Parameters:
      scriptExtension - the extension to be used for the ScriptEngine
      Returns:
      the new EvalScript