Results 1 to 3 of 3

Thread: How to read collections in a comma separated flat file.

Threaded View

  1. #1
    Join Date
    Jan 2012
    Posts
    12

    Default How to read collections in a comma separated flat file.

    Hello techsters -

    I have the following domain class:
    Code:
       public class Book {
    
        private String name;
       
        private Set<Author> authors = new HashSet<Author>();
    
       // getters and setters for the above
      }
    
      public class Author {
        
        private name;
        private String address    
    
        // getters and setters for the above.
    }
    I receive a flat file like the following that has the following contents:
    Pro Spring Batch, Dave Syer, Michelle T Minella
    Spring Integration In Action, Dave Syer, John Fisher


    Code:
     <beans:bean id="lineMapper" 		    
                class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    		<beans:property name="lineTokenizer" ref="lineTokenizer" />
    		<beans:property name="fieldSetMapper" ref="fieldSetMapper" />
    	</beans:bean>
    
       <beans:bean id="lineTokenizer"     
              class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
       <beans:property name="names"  value="name,authors[0].name,authors[1].name"/>
        </beans:bean>
    	<beans:bean id="fieldSetMapper"
    		class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
    		<beans:property name="prototypeBeanName" value="book" />
    	</beans:bean>
    
     <beans:bean id="book" class="org.app.Book" scope="prototype" />
    Basically I would want the framework to create a Book Instance with a set of authors when it reads each line. However, it seems it does not understand authors[0].name,authors[1].name and cannot load it in the set. It keeps saying it cannot add items to the set with size 0, which is understandable since the bean when instantiated has a set with size 0, but in java we can add elements to a set initialized with zero size. I am unable to figure out what is the framework's understanding and how we can load collections of other instances like the one stated above.

    Here is the error that I receive:
    Invalid property 'authors[0]' of bean class [org.app.Book]: Cannot get element with index 0 from Set of size 0, accessed using property path 'authors[0]'

    Can you please guide how I can achieve the above functionality ?

    Further if someone can explain me the following text as written in the spring documentation, it would be of great help:
    http://static.springsource.org/sprin...SetMapper.html

    "Nested property paths, including indexed properties in maps and collections, can be referenced by the FieldSet names. They will be converted to nested bean properties inside the prototype. The FieldSet and the prototype are thus tightly coupled by the fields that are available and those that can be initialized. If some of the nested properties are optional (e.g. collection members) they need to be removed by a post processor."

    Apparently what seems to be happening by saying authors[0].name is that framework understands it to get a value from the set. But my intention is to tell the framework to make a bean of type Author and add it to the set.

    If anyone is aware of how to do this with respect to the above documentation please do let me know or please do let me know if I have not understood what is written in the documentation.
    Last edited by Tams; Sep 26th, 2012 at 10:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •