views:

282

answers:

2

Hi,

I'm faced to the following problem. I have a tree of java objects for which I have to export each field value into a CSV file. The result of the exportation must be similar to what we have in SQL left outer join (called cartesian product).

Class author

@DataField(pos = 1)
String firstName;

@DataField(pos = 2)
String lastName;

@OneToMany
List<Book> books;

@OneToMany
List<Editor> editors;

@DataField(pos = 7)
String Age;

Class Book

@DataField(pos = 3)
String title;

@DataField(pos = 4)
String year;

@OneToMany
List<Reference> references;

Class Reference

@DataField(pos = 5)
String type;

@DataField(pos = 6)
String code;

Class Editor

@DataField(pos = 8)
String name;

Remarks : - @DataField annotation indicates the position of the value in the CSV record - For this example we have one object Author (Charles, Moulliard) containing a list of 2 books ("Camel in action" and "Camel in action 2"). The first book has three Reference (ISBN 1234, ISBN 5678, ISBN 999) and the second one reference (ISBB 1111). Author contain also a list of two editors ("manning", "manning 2")

Here is an example and the result wanted

"firstName","lastName","age","title","year","type","code","name" "charles","moulliard","camel in action","2009","ISBN","1234","manning","43" "charles","moulliard","camel in action","2009","ISBN","1234","manning 2","43" "charles","moulliard","camel in action","2009","ISBN","5678","manning","43" "charles","moulliard","camel in action","2009","ISBN","5678","manning 2","43" "charles","moulliard","camel in action","2009","ISBN","9999","manning","43" "charles","moulliard","camel in action","2009","ISBN","9999","manning 2","43" "charles","moulliard","camel in action 2","2011","ISBB","1111","manning","43" "charles","moulliard","camel in action 2","2011","ISBB","1111","manning 2","43"

I have tried using a recursive function to put field values in Map of LinkedList : Map where Integer = position of the field in the CSV and Linkedlist = list of objects but I lost information about position of the element in the tree.

Regards,

Charles

+1  A: 

I probably don't understand your problem. Wouldn't something like this work?

ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
for (Author author:authors) {
    for (Book book:author.getBooks()) {
     for (Reference reference:book.getReferences()){
      for (Editor editor:editors) {
       table.add(new ArrayList<String>(Arrays.toList({author.getFirstName(), 
              author.getLastName(), 
              book.getTitle(), 
              book.getYear(), 
              reference.getType(), 
              reference.getCode(), 
              editor.getName()})
            );
       );
      }
     }
    }
}
HerdplattenToni
A: 
     Map<Integer, List> values = new HashMap<Integer, List>();
 values.put(1, Arrays.asList("Charles"));
 values.put(2, Arrays.asList("Moulliard"));
 values.put(3, Arrays.asList("Camel in Action", "Camel in Action 2"));
 values.put(4, Arrays.asList("2009", "2011"));
 values.put(5, Arrays.asList("ISBN", "ISBN", "ISBN"));
 values.put(6, Arrays.asList("1234", "9876", "7777"));
 for (List l : s.product(values)) {
  System.out.println(l);
 }



}

public List<List> product(Map<Integer, List> values) {

       List<List> product = new ArrayList<List>();
       Map<Integer, Integer> index = new HashMap<Integer, Integer>();
       boolean incIndex = false;

       while (!incIndex) {

        incIndex = true;
           List v = new ArrayList();

           for (int i = 1; ; i++) {
               List l = values.get(i);
               if (l == null) {
                   break;
               }
               int idx = 0;
               if (index.containsKey(i)) {
                   idx = index.get(i);
               }
               v.add(l.get(idx));
               if (incIndex) {
                   if (++idx >= l.size()) {
                       idx = 0;
                   } else {
                       incIndex = false;
                   }
                   index.put(i, idx);
               }
           }
           product.add(v);
       }
       return product;
}
cmoulliard