Search code examples
javaexpressionognlspring-el

Java Expression Language Projection for each element in collection get new Object of other type


I have below given scenario and problem , looking for someone to provide solution/suggestion.

Scenario: There are different types of business-beans objects, these business-beans do not having common properties and cannot be generalized. For different task/job I have to deal with collection of business-bean of one type. Out this collection I want another collection of MyKey type. The MyKey constructor is: MyKey(String buzUnit, String id, String subId) Corresponding to different business-bean the value I need for MyKey's constructor (buzUnit, id and subId) , is going to be different properties of business-bean that again depends on executing task/job .

I like to make it configurable based on task/job and maintain the configuration in database.

Problem: I am trying to use any EL (OGNL, Unified EL, MVEL, SpEL) to achieve collection conversion. I tried using OGNL, but am not able to get desired result.

POC on OGNL:

List<Point> points = new ArrayList<Point>();
points.add(new Point(2,3));
points.add(new Point(3,4));
points.add(new Point(4,5));

Map<String, Object> nameCtx = new HashMap<String, Object>();
nameCtx.put("buzBeanCollection", points);

Object retCollection = Ognl.getValue("buzBeanCollection.{new org.my.MyKey('job12', #this.x, #this.y) }", nameCtx);

In the above code #this is resolving into root HashMap


Solution

  • Got it working using MVEL.

    List<Point> points = new ArrayList<Point>();
    points.add(new Point(2,3));
    points.add(new Point(3,4));
    points.add(new Point(4,5));
    
    Map<String, Object> nameCtx = new HashMap<String, Object>();
    nameCtx.put("buzBeanCollection", points);
    
    Object result = MVEL.eval("((new org.my.MyKey('job12', $.x * 2, $.y * 2)) in buzBeanCollection)", nameCtx);
    System.out.println(result);
    

    Other conclusion based in MVEL capabilities; I have found that OGNL's AST is created using ANTLR but MVEL has its own integrated Lexer-Parser, including Subparsers.

    OGNL was not able to do simillar projection was that a problem of OGNL or problem with ANTLR ?