// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/Talk.java
// public class Talk {
// 
//     public final String path;
//     public final String title;
//     public final List<String> speakers;
//     public final Date date;
//     public final String content;
//     public final List<String> categories;
//     public final String organizer;
// 
//     public Talk(String path, String title, List<String> speakers, Date date, String content, List<String> categories, String organizer) {
//         this.path = path;
//         this.title = title;
//         this.speakers = speakers;
//         this.date = date;
//         this.content = content;
//         this.categories = categories;
//         this.organizer = organizer;
//     }
// }
// 
// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/TalkFromFile.java
// public class TalkFromFile implements Function<File, Talk> {
// 
//     private DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
// 
//     @Override
//     public Talk apply(File input) {
//         Properties props = read(input);
//         String speakerValue = props.getProperty("speaker");
//         String title = props.getProperty("title");
//         String dateValue = props.getProperty("date");
//         String contents = props.getProperty("content");
//         String categoriesValue = props.getProperty("categories");
//         String organizer = props.getProperty("organizer", "");
// 
//         List<String> speakers = Arrays.asList(speakerValue.split(","));
//         List<String> categories = Collections.emptyList();
//         if (categoriesValue != null) {
//             categories = Arrays.asList(categoriesValue.split(","));    
//         }        
//         
// 
//         return new Talk(input.getAbsolutePath(), title, speakers, parseDate(dateValue), contents, categories, organizer);
//     }
// 
//     private Date parseDate(String dateValue) {
//         try {
//             return format.parse(dateValue);
//         } catch (ParseException e) {
//             throw new IllegalStateException(e);
//         }
//     }
// 
//     private Properties read(File input) {
//         Properties props = new Properties();
//         FileInputStream in = null;
//         try {
//             in = new FileInputStream(input);
//             props.load(in);
//         } catch (IOException e) {
//             throw new IllegalStateException(e);
//         } finally {
//             if (in != null) {
//                 try {
//                     in.close();
//                 } catch (IOException e) {
//                     e.printStackTrace();
//                 }
//             }
//         }
//         return props;
//     }
// }


import com.google.common.collect.Collections2;
import de.fhopf.lucenesolrtalk.Talk;
import de.fhopf.lucenesolrtalk.TalkFromFile;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
        }
        client.admin().indices().prepareCreate(INDEX).execute().actionGet();
        // TODO how to make the german analyzer the default?
        XContentBuilder builder = XContentFactory.jsonBuilder().
                startObject().
                    startObject(TYPE).
                        startObject("properties").
                            startObject("path").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("title").field("type", "string").field("store", "yes").field("analyzer", "german").endObject().
                            startObject("category").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("speaker").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("date").field("type", "date").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("content").field("type", "string").field("store", "yes").field("analyzer", "german").field("term_vector", "with_positions_offsets").endObject().
                            startObject("organizer").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                        endObject().
                    endObject().
                endObject();
        
        client.admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(builder).execute().actionGet();
    }

    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.out.println("Usage: java " + Indexer.class.getCanonicalName() + " <dataDir>");
            System.exit(-1);
        }

        List<File> files = Arrays.asList(new File(args[0]).listFiles());

// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/Talk.java
// public class Talk {
// 
//     public final String path;
//     public final String title;
//     public final List<String> speakers;
//     public final Date date;
//     public final String content;
//     public final List<String> categories;
//     public final String organizer;
// 
//     public Talk(String path, String title, List<String> speakers, Date date, String content, List<String> categories, String organizer) {
//         this.path = path;
//         this.title = title;
//         this.speakers = speakers;
//         this.date = date;
//         this.content = content;
//         this.categories = categories;
//         this.organizer = organizer;
//     }
// }
// 
// Path: lucene/src/main/java/de/fhopf/lucenesolrtalk/TalkFromFile.java
// public class TalkFromFile implements Function<File, Talk> {
// 
//     private DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
// 
//     @Override
//     public Talk apply(File input) {
//         Properties props = read(input);
//         String speakerValue = props.getProperty("speaker");
//         String title = props.getProperty("title");
//         String dateValue = props.getProperty("date");
//         String contents = props.getProperty("content");
//         String categoriesValue = props.getProperty("categories");
//         String organizer = props.getProperty("organizer", "");
// 
//         List<String> speakers = Arrays.asList(speakerValue.split(","));
//         List<String> categories = Collections.emptyList();
//         if (categoriesValue != null) {
//             categories = Arrays.asList(categoriesValue.split(","));    
//         }        
//         
// 
//         return new Talk(input.getAbsolutePath(), title, speakers, parseDate(dateValue), contents, categories, organizer);
//     }
// 
//     private Date parseDate(String dateValue) {
//         try {
//             return format.parse(dateValue);
//         } catch (ParseException e) {
//             throw new IllegalStateException(e);
//         }
//     }
// 
//     private Properties read(File input) {
//         Properties props = new Properties();
//         FileInputStream in = null;
//         try {
//             in = new FileInputStream(input);
//             props.load(in);
//         } catch (IOException e) {
//             throw new IllegalStateException(e);
//         } finally {
//             if (in != null) {
//                 try {
//                     in.close();
//                 } catch (IOException e) {
//                     e.printStackTrace();
//                 }
//             }
//         }
//         return props;
//     }
// }


// Path: elasticsearch-indexer/src/main/java/de/fhopf/lucenesolrtalk/elasticsearch/Indexer.java
import com.google.common.collect.Collections2;
import de.fhopf.lucenesolrtalk.Talk;
import de.fhopf.lucenesolrtalk.TalkFromFile;
import java.io.File;
import java.io.IOException;
import java.util.*;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;

        }
        client.admin().indices().prepareCreate(INDEX).execute().actionGet();
        // TODO how to make the german analyzer the default?
        XContentBuilder builder = XContentFactory.jsonBuilder().
                startObject().
                    startObject(TYPE).
                        startObject("properties").
                            startObject("path").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("title").field("type", "string").field("store", "yes").field("analyzer", "german").endObject().
                            startObject("category").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("speaker").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("date").field("type", "date").field("store", "yes").field("index", "not_analyzed").endObject().
                            startObject("content").field("type", "string").field("store", "yes").field("analyzer", "german").field("term_vector", "with_positions_offsets").endObject().
                            startObject("organizer").field("type", "string").field("store", "yes").field("index", "not_analyzed").endObject().
                        endObject().
                    endObject().
                endObject();
        
        client.admin().indices().preparePutMapping(INDEX).setType(TYPE).setSource(builder).execute().actionGet();
    }

    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.out.println("Usage: java " + Indexer.class.getCanonicalName() + " <dataDir>");
            System.exit(-1);
        }

        List<File> files = Arrays.asList(new File(args[0]).listFiles());

        Collection<Talk> talks = Collections2.transform(files, new TalkFromFile());