package org.jasig.ssp.util.dataimport.job.csv;

import org.jasig.ssp.util.dataimport.job.domain.RawItem;
import org.jasig.ssp.util.dataimport.job.domain.RawItemAndBean;
import org.springframework.batch.item.ItemWriter;
import org.springframework.core.io.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RawItemCsvWriter implements ItemWriter<RawItem> {

    private Resource currentResource;
    private String[] orderedHeaders = null;

    @Override
    public void write(List<? extends RawItem> items) throws Exception {
        // TODO Extend FlatFileItemWriter instead. For now we're just writing to std out for prototyping.
        // As written, this only works if the bean is step-scoped. Might be a bit challenging even in a
        // FlatFileItemWriter subclass to write an item-based header row.
        if ( items == null ) {
            currentResource = null;
            orderedHeaders = null;
            return;
        }
        if ( currentResource == null || !(currentResource.equals(items.get(0).getResource())) ) {
            if ( currentResource != null ) {
                say("");
            }
            orderedHeaders = writeHeader(items);
        }
        for ( RawItem item : items ) {
            StringBuilder sb = new StringBuilder();
            final Map<String,String> record = item.getRecord();
            for ( String header : orderedHeaders ) {
                sb.append(record.get(header)).append(",");
            }
            sb.setLength(sb.length() - 1); // trim comma
            say(sb);
        }
    }

    private String[] writeHeader(List<? extends RawItem> items) {
        Map<String,String> firstRecord = items.get(0).getRecord();
        StringBuilder sb = new StringBuilder();
        List<String> headerColumns = new ArrayList<String>();
        for ( String key : firstRecord.keySet() ) {
            sb.append(key).append(",");
            headerColumns.add(key);
        }
        sb.setLength(sb.length() - 1); // trim comma
        say(sb);
        return headerColumns.toArray(new String[headerColumns.size()]);
    }

    private void say(Object message) {
        System.out.println(message);
    }
}

@@ -892,16 +892,21 @@
         
+this.
 currentR
@@ -925,32 +925,37 @@
 ll;%0A            
+this.
 orderedHeaders =
@@ -1032,13 +1032,90 @@
 ull 
-%7C%7C !(
+) %7B%0A            this.orderedHeaders = writeHeader(items.get(0));%0A            this.
 curr
@@ -1129,16 +1129,11 @@
 urce
-.equals(
+ = 
 item
@@ -1158,55 +1158,176 @@
 ce()
-)) ) %7B%0A            if ( currentResource != null
+;%0A        %7D%0A        for ( RawItem item : items ) %7B%0A            Resource itemResource = item.getResource();%0A            if ( !(this.currentResource.equals(itemResource))
  ) %7B
@@ -1351,10 +1351,8 @@
 say(
-%22%22
 );%0A 
@@ -1360,32 +1360,27 @@
         
-  %7D%0A
       
-      
+this.
 orderedH
@@ -1404,17 +1404,16 @@
 der(item
-s
 );%0A     
@@ -1411,34 +1411,32 @@
 m);%0A        
-%7D%0A
         
 for ( RawIte
@@ -1427,38 +1427,58 @@
     
-for ( RawItem item : items ) %7B
+this.currentResource = itemResource;%0A            %7D
 %0A   
@@ -1624,16 +1624,21 @@
 eader : 
+this.
 orderedH
@@ -1853,37 +1853,20 @@
 der(
-List%3C? extends 
 RawItem
-%3E
  item
-s
 ) %7B%0A
@@ -1906,32 +1906,24 @@
 ecord = item
-s.get(0)
 .getRecord()
@@ -2402,11 +2402,60 @@
 );%0A    %7D
+%0A%0A    private void say() %7B%0A        say(%22%22);%0A    %7D
 %0A%7D%0A

NOJIRA Fix multi-file output