6 Replies Latest reply on Apr 1, 2009 11:31 AM by ama1

    Smooks JavaBinding

    ama1

      Hi,
      I use the following configuration to bind my csv files:

      <csv:reader
       fields="FILE_ID,COMP_ID,ADDR_ID,CNTY_ID,ZIP_ID,CNTRY_ID,STATE_ID,LOC_ID,TWN_ID,ADDR_L1,ADDR_L2,ADDR_STR,ADDR_NUM,ADDR_BOX,DUMMY"
       separator=";" quote=""" skipLines="1" />
      
       <jb:bindings beanId="add" class="java.util.ArrayList" createOnElement="csv-set" extendLifecycle="true">
       <jb:wiring beanIdRef="addressRecord"/>
       </jb:bindings>
       <jb:bindings beanId="addressRecord" class="java.util.HashMap" createOnElement="csv-record" extendLifecycle="true">
       <jb:value property="ADDR_ID" data="ADDR_ID"/>
       <jb:value property="CNTY_ID" data="CNTY_ID"/>
       <jb:value property="ZIP_ID" data="ZIP_ID"/>
       <jb:value property="CNTRY_ID" data="CNTRY_ID"/>
       <jb:value property="CNTRY_ID" data="CNTRY_ID"/>
       <jb:value property="STATE_ID" data="STATE_ID"/>
       <jb:value property="LOC_ID" data="LOC_ID"/>
       <jb:value property="TWN_ID" data="TWN_ID"/>
       <jb:value property="ADDR_L1" data="ADDR_L1"/>
       <jb:value property="ADDR_L2" data="ADDR_L2"/>
       <jb:value property="ADDR_STR" data="ADDR_STR"/>
       <jb:value property="ADDR_NUM" data="ADDR_NUM"/>
       <jb:value property="ADDR_BOX" data="ADDR_BOX"/>
       </jb:bindings>



      The result will be a ArrayList<Map<String, Object>>. Is there a way to add an index to have this as result Map<String, Map<String, Object>>, where the Map.Key would be the index (ADDR_ID) and the Map.Value the Address entity-map?

      Thanks,
      Patrick

        • 1. Re: Smooks JavaBinding
          tfennelly

          I don't think you can do this directly, but it wouldn't be hard to get it to work with the help of a little Groovy scripting to manually wire the address records into the address Map.

          Also, you can probably abreviate the binding config for mapping the csv-record set data into the Map.

          Here's what it would look like (untested)...

          <jb:bindings beanId="addresses" class="java.util.HashMap" createOnElement="csv-set" />
          
          <jb:bindings beanId="addressRecord" class="java.util.HashMap" createOnElement="csv-record">
           <!-- Omit the property + use a wildcard selector => Smooks will use the element name as the Map key -->
           <jb:value data="csv-record/*" />
          </jb:bindings>
          
          <g:groovy executeOnElement="csv-record">
           <g:script>
           <!--
           // Manually wire each addressRecord into the address Map...
           Map addresses = BeanRepository.getInstance(executionContext).getBean("addresses");
           Map addressRecord = BeanRepository.getInstance(executionContext).getBean("addressRecord");
          
           addresses.put(addressRecord.ADDR_ID, addressRecord);
           -->
           </g:script>
          </g:groovy>
          


          • 2. Re: Smooks JavaBinding
            ama1

            Hi,
            thanks for the reply. Yes that would work I guess, if this bug would not occur http://jira.codehaus.org/browse/MILYN-252

            I think I have to wait for a fix here and use in the meantime a unperfomant iteration over all elements and compare the IDs with freemarker if statements :-/

            • 3. Re: Smooks JavaBinding
              tfennelly

              That's actually fixed in Smooks trunk. You can safely use the fix with Smooks v1.1.x. You can get the latest SNAPSHOT from here.

              • 4. Re: Smooks JavaBinding
                ama1

                Ok thanks a lot, I ll use the snapshot. Is there already a release planing for 1.2?

                • 5. Re: Smooks JavaBinding
                  tfennelly

                  We made a v1.1.2 patch release at the weekend, so you can use that too since it has this fix... http://www.smooks.org/downloads

                  • 6. Re: Smooks JavaBinding
                    ama1

                    Perfect! :-)