Clarity

 View Only
  • 1.  How to set value of dynamic variable (associative array)?

    Posted Aug 14, 2014 04:30 PM

    I have an SQL query that returns several columns (and possibly several rows), some of which I want to loop through and under certain conditions, replace the value.

     

    <sql:query var="q_idea" escapeText="false"><![CDATA[
      SELECT col1,col2,col3 FROM niku.INV_INVESTMENTS INVI
      JOIN niku.ODF_CA_INV ODFI ON INVI.ID = ODFI.ID
      WHERE ODFI.ID = ?
      ]]>
      <sql:param value="${gel_objectInstanceId}"/>
    </sql:query>
    <core:set var="var_columns" value="col1,col3" />
    
    
    <core:forEach items="${q_idea.rows}" var="row" indexVar="vn_index">
      <core:forEach items="${var_columns}" var="var_column">
      <core:if test="${row.get(var_column) == 9}">
      <gel:log level="INFO" category="XOG">${var_column} value adjusted from 9 to 7</gel:log>
      <core:set var="row.get(var_column)">7</core:set>
      </core:if>
      <gel:log level="INFO" category="XOG">Value = ${row.get(var_column)}</gel:log>
      </core:forEach>
    </core:forEach>
    
    
    

    I can't get the inner set to work. The condition test is fine so I get in to that block since the log line gets output, but the value itself doesn't change. Anyone ever tried something like this?



  • 2.  Re: How to set value of dynamic variable (associative array)?
    Best Answer

    Posted Aug 15, 2014 03:43 AM

    The row is a Map<java.lang.String, java.lang.Object> where the string is the column name and the object is the value of row/column.

    So to update it I would guess it would be:

     

    <core:expr value="${row.put(var_column,7)}" />
    

     

    or

     

    <core:invoke method="put" on="${row}">
      <core:arg type="java.lang.String" value="${var_column}"/>
      <core:arg type="java.lang.Object" value="7"/>
    </core:invoke>
    

     

    V/r,

    Gene



  • 3.  Re: How to set value of dynamic variable (associative array)?

    Posted Aug 15, 2014 09:50 AM

    Thanks, this one worked perfectly

    <core:expr value="${row.put(var_column,7)}" />  
    
    

     

    Brings up a follow up question though, what is the best way to set up an array? In other languages I'm used to something like:

    $array = [
        "foo" => "bar",
        "bar" => "foo",
    ];
    

    Is there a similar construct for GEL or does it need to be multiple individual lines similar to above?

    <core:expr value="${array.put("foo","bar")}"  />
    

    ?



  • 4.  Re: How to set value of dynamic variable (associative array)?

    Posted Aug 15, 2014 04:10 PM

    I don't believe that Maps have constructors that provide this capability unless you pass in another Map.

     

    For Example: HashMap(Map<? extends K,? extends V> m) Constructs a new HashMap with the same mappings as the specified Map.

     

    so I think you are stuck with:

     

    <core:expr value="${array.put("foo","bar")}"  />  
    

     

    V/r,

    Gene