package de.tblsoft.solr.pipeline;

import de.tblsoft.solr.pipeline.bean.Document;
import de.tblsoft.solr.pipeline.bean.Field;
import de.tblsoft.solr.util.IOUtils;
import de.tblsoft.solr.util.OutputStreamStringBuilder;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by tblsoft on 23.01.16.
 */
public class FileLineWriter extends AbstractFilter {

    private String filename;

    private OutputStreamStringBuilder outputStreamStringBuilder;

    private OutputStream outputStream;

    @Override
    public void init(){

        String relativeFilename = getProperty("filename", null);
        filename = IOUtils.getAbsoluteFile(getBaseDir(), relativeFilename);

        verify(filename, "For the FileLineWriter a filname must be defined.");

        try {
            outputStream = IOUtils.getOutputStream(filename);
            outputStreamStringBuilder = new OutputStreamStringBuilder(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }



    }


    @Override
    public void document(Document document) {
        for(Field field: document.getFields()) {
            for(String value : field.getValues()) {
                field(field.getName(),value);
            }
        }
        super.document(document);
    }

    public void field(String name, String value) {
        outputStreamStringBuilder.append(value);
        outputStreamStringBuilder.append("\n");

    }

    @Override
    public void end() {
        try {
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -268,16 +268,39 @@
 tStream;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -526,16 +526,141 @@
 tream;%0A%0A
+    private List%3CString%3E fieldNames;%0A    private String fieldSeperator = %22,%22;%0A    private String documentSeperator = %22%5Cn%22;%0A%0A%0A
     @Ove
@@ -755,16 +755,76 @@
  null);%0A
+        fieldNames = getPropertyAsList(%22fieldNames%22, null);%0A
         
@@ -1221,16 +1221,37 @@
      %7D%0A%0A
+        super.init();
 %0A%0A    %7D%0A
@@ -1308,24 +1308,61 @@
 document) %7B%0A
+        if(fieldNames == null) %7B%0A    
         for(
@@ -1360,16 +1360,17 @@
      for
+ 
 (Field f
@@ -1373,16 +1373,17 @@
 ld field
+ 
 : docume
@@ -1412,19 +1412,24 @@
         
+  
+  
 for
+ 
 (String 
@@ -1469,24 +1469,28 @@
             
+ 
+   
 field(field.
@@ -1499,16 +1499,17 @@
 tName(),
+ 
 value);%0A
@@ -1512,32 +1512,36 @@
 e);%0A            
+ 
+   
 %7D%0A        %7D%0A    
@@ -1526,34 +1526,412 @@
       %7D%0A        
+ 
+   
 %7D%0A
+        %7D else %7B%0A            for(String fieldName: fieldNames) %7B%0A                Field field = document.getField(fieldName);%0A                if(field != null) %7B%0A                    for (String value : field.getValues()) %7B%0A                        field(field.getName(), value);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A%0A        endDocument(document);%0A%0A
         super.do
@@ -2100,15 +2100,141 @@
 end(
-%22%5Cn%22);%0A
+fieldSeperator);%0A    %7D%0A%0A    public void endDocument(Document document) %7B%0A        outputStreamStringBuilder.append(documentSeperator);
 %0A   
@@ -2406,21 +2406,42 @@
 ;%0A        %7D%0A
+        super.end();%0A
     %7D%0A%0A%7D%0A

add the possibility to write specific fields in the file line writer